|
@ -22,8 +22,9 @@ bool recursive = false; |
|
|
// -----------------------------------------------------------------------------
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
void relayMQTT(unsigned char id) { |
|
|
void relayMQTT(unsigned char id) { |
|
|
char buffer[10]; |
|
|
|
|
|
sprintf(buffer, MQTT_RELAY_TOPIC, id); |
|
|
|
|
|
|
|
|
String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER); |
|
|
|
|
|
char buffer[strlen(MQTT_RELAY_TOPIC) + mqttGetter.length() + 3]; |
|
|
|
|
|
sprintf(buffer, "%s/%d%s", MQTT_RELAY_TOPIC, id, mqttGetter.c_str()); |
|
|
mqttSend(buffer, (char *) (relayStatus(id) ? "1" : "0")); |
|
|
mqttSend(buffer, (char *) (relayStatus(id) ? "1" : "0")); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -169,36 +170,41 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo |
|
|
|
|
|
|
|
|
static bool isFirstMessage = true; |
|
|
static bool isFirstMessage = true; |
|
|
|
|
|
|
|
|
|
|
|
String mqttSetter = getSetting("mqttSetter", MQTT_USE_SETTER); |
|
|
|
|
|
String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER); |
|
|
|
|
|
bool sameSetGet = mqttGetter.compareTo(mqttSetter) == 0; |
|
|
|
|
|
|
|
|
if (type == MQTT_CONNECT_EVENT) { |
|
|
if (type == MQTT_CONNECT_EVENT) { |
|
|
relayMQTT(); |
|
|
relayMQTT(); |
|
|
mqttSubscribe("/relay/#"); |
|
|
|
|
|
|
|
|
char buffer[strlen(MQTT_RELAY_TOPIC) + mqttSetter.length() + 3]; |
|
|
|
|
|
sprintf(buffer, "%s/+%s", MQTT_RELAY_TOPIC, mqttSetter.c_str()); |
|
|
|
|
|
mqttSubscribe(buffer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (type == MQTT_MESSAGE_EVENT) { |
|
|
if (type == MQTT_MESSAGE_EVENT) { |
|
|
|
|
|
|
|
|
// Match topic
|
|
|
// Match topic
|
|
|
if (memcmp("/relay/", topic, 7) != 0) return; |
|
|
|
|
|
|
|
|
String t = String(topic); |
|
|
|
|
|
if (!t.startsWith(MQTT_RELAY_TOPIC)) return; |
|
|
|
|
|
if (!t.endsWith(mqttSetter)) return; |
|
|
|
|
|
|
|
|
// If relayMode is not SAME avoid responding to a retained message
|
|
|
// If relayMode is not SAME avoid responding to a retained message
|
|
|
if (isFirstMessage) { |
|
|
|
|
|
|
|
|
if (sameSetGet && isFirstMessage) { |
|
|
isFirstMessage = false; |
|
|
isFirstMessage = false; |
|
|
byte relayMode = getSetting("relayMode", RELAY_MODE).toInt(); |
|
|
byte relayMode = getSetting("relayMode", RELAY_MODE).toInt(); |
|
|
if (relayMode != RELAY_MODE_SAME) return; |
|
|
if (relayMode != RELAY_MODE_SAME) return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Get relay ID
|
|
|
// Get relay ID
|
|
|
unsigned int relayID = topic[strlen(topic)-1] - '0'; |
|
|
|
|
|
|
|
|
unsigned int relayID = topic[strlen(MQTT_RELAY_TOPIC)+1] - '0'; |
|
|
if (relayID >= relayCount()) relayID = 0; |
|
|
if (relayID >= relayCount()) relayID = 0; |
|
|
|
|
|
|
|
|
// Action to perform
|
|
|
// Action to perform
|
|
|
if ((char)payload[0] == '0') { |
|
|
|
|
|
relayStatus(relayID, false, false); |
|
|
|
|
|
} |
|
|
|
|
|
if ((char)payload[0] == '1') { |
|
|
|
|
|
relayStatus(relayID, true, false); |
|
|
|
|
|
} |
|
|
|
|
|
if ((char)payload[0] == '2') { |
|
|
|
|
|
|
|
|
unsigned int value = (char)payload[0] - '0'; |
|
|
|
|
|
if (value == 2) { |
|
|
relayToggle(relayID); |
|
|
relayToggle(relayID); |
|
|
|
|
|
} else { |
|
|
|
|
|
relayStatus(relayID, value > 0, !sameSetGet); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|