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());