From 4c3920284837e19d1931307154f803945be40014 Mon Sep 17 00:00:00 2001 From: Thomas Staerk Date: Tue, 27 Feb 2018 20:58:21 +0100 Subject: [PATCH] Reset relays on MQTT disconnection With option #define MQTT_DISCONNECT_RELAY_DEFAULT set to 1, all relays will be reset in case of MQTT disconnection to avoid a connected device being switched on while MQTT is not under broker control any more. --- code/espurna/config/general.h | 2 ++ code/espurna/relay.ino | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 81c4d749..78aaa379 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -513,6 +513,8 @@ PROGMEM const char* const custom_reset_string[] = { #define MQTT_USE_JSON_DELAY 100 // Wait this many ms before grouping messages #define MQTT_QUEUE_MAX_SIZE 10 // Size of the MQTT queue when MQTT_USE_JSON is enabled +#define MQTT_DISCONNECT_RELAY_DEFAULT 1 // 0 - do nothing, 1 - reset relays to OFF in case of MQTT disconnection + // These are the properties that will be sent when useJson is true #ifndef MQTT_ENQUEUE_IP #define MQTT_ENQUEUE_IP 1 diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index 5fa9af7e..5723b56e 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -654,6 +654,17 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo } + if (type == MQTT_DISCONNECT_EVENT) + { + if (MQTT_DISCONNECT_RELAY_DEFAULT == 1){ + for (unsigned int i=0; i < _relays.size(); i++){ + DEBUG_MSG_P(PSTR("[RELAY] Reset relay (%d) due to MQTT disconnection\n"), i); + relayStatusWrap(i, false, false); + } + } + + } + } void relaySetupMQTT() {