Skip to content

Add feature to disable/enable buttons by MQTT #1322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added ability to disable/enable buttons by MQTT.
For example it useful when automation enable light on door opening and you trying to turn on light manually. And you touch switch exactly after light turned on by automation and you mistakenly turn off lights.
  • Loading branch information
vtochq authored Nov 7, 2018
commit 8cc77d39006ee9d6cb4688a7242c7abb5ece0c44
55 changes: 55 additions & 0 deletions code/espurna/button.ino
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,56 @@ void buttonMQTT(unsigned char id, uint8_t event) {
mqttSend(MQTT_TOPIC_BUTTON, id, payload, false, false); // 1st bool = force, 2nd = retain
}

// You can disable/enable buttons by MQTT. Just public message "0" or "1" to topic <hostname>/button/#/set
void _buttonMqttCallback(unsigned int type, const char * topic, const char * payload) {
if (type == MQTT_CONNECT_EVENT) {
// Subscribe to own /set topic
char button_topic[strlen(MQTT_TOPIC_BUTTON) + 3];
snprintf_P(button_topic, sizeof(button_topic), PSTR("%s/+"), MQTT_TOPIC_BUTTON);
mqttSubscribe(button_topic);
DEBUG_MSG_P(PSTR("[Button] subscribing to MQTT topic %s \n"), MQTT_TOPIC_BUTTON);
}
if (type == MQTT_MESSAGE_EVENT) {
String t = mqttMagnitude((char *) topic);
// Match topic
if (t.startsWith(MQTT_TOPIC_BUTTON)) {
// Get button ID
unsigned int id = t.substring(strlen(MQTT_TOPIC_BUTTON)+1).toInt();
char mode = payload[0]-48; // button mode. currently enabled or disablesd. ASCII "0" is a 48.
if (mode == 0) {
_buttons[id].relayID = 0; // disable button-realy mapping
} else {
switch (id) { // restoring button-relay mapping from hardcoded preprocessor constants
case 0:
_buttons[id].relayID = BUTTON1_RELAY;
break;
case 1:
_buttons[id].relayID = BUTTON2_RELAY;
break;
case 2:
_buttons[id].relayID = BUTTON3_RELAY;
break;
case 3:
_buttons[id].relayID = BUTTON4_RELAY;
break;
case 4:
_buttons[id].relayID = BUTTON5_RELAY;
break;
case 5:
_buttons[id].relayID = BUTTON6_RELAY;
break;
case 6:
_buttons[id].relayID = BUTTON7_RELAY;
break;
case 7:
_buttons[id].relayID = BUTTON8_RELAY;
break;
}
}
DEBUG_MSG_P(PSTR("[Button] Button id %d, mode %d, relayID now is %u\n"), id, mode, _buttons[id].relayID);
}
}
}
#endif

#if WEB_SUPPORT
Expand Down Expand Up @@ -212,6 +262,11 @@ void buttonSetup() {
// Register loop
espurnaRegisterLoop(buttonLoop);

// Register MQTT loop for set button mode by MQTT
#if MQTT_SUPPORT
mqttRegister(_buttonMqttCallback);
#endif

}

void buttonLoop() {
Expand Down