From 4b301d6620c139226a47ffb04030203a9830ab31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Mon, 6 Feb 2017 10:09:16 +0100 Subject: [PATCH] Force WiFi reconnect after MQTT_MAX_TRIES --- code/espurna/config/general.h | 2 ++ code/espurna/mqtt.ino | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 63d53a38..1c1c29f9 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -102,6 +102,8 @@ #define MQTT_QOS 0 #define MQTT_KEEPALIVE 30 #define MQTT_RECONNECT_DELAY 10000 +#define MQTT_TRY_INTERVAL 30000 +#define MQTT_MAX_TRIES 5 #define MQTT_SKIP_RETAINED 1 #define MQTT_SKIP_TIME 1000 #define MQTT_ACTION_TOPIC "/action" diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index daaed144..417aa252 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -139,6 +139,22 @@ void mqttConnect() { if (!mqtt.connected()) { + // Last option: reconnect to wifi after MQTT_MAX_TRIES attemps in a row + #if MQTT_MAX_TRIES > 0 + static unsigned int tries = 0; + static unsigned long last_try = millis(); + if (millis() - last_try < MQTT_TRY_INTERVAL) { + if (++tries > MQTT_MAX_TRIES) { + wifiDisconnect(); + tries = 0; + return; + } + } else { + tries = 0; + } + last_try = millis(); + #endif + mqtt.disconnect(); char * host = strdup(getSetting("mqttServer", MQTT_SERVER).c_str());