From 7e71c29bf96658a509cbe7c8ae73b127666dd6c1 Mon Sep 17 00:00:00 2001 From: Max Prokhorov Date: Fri, 28 Dec 2018 05:15:54 +0300 Subject: [PATCH] MQTT reconnect delay based on last disconnection --- code/espurna/mqtt.ino | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 69f11a18..882f32fa 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -51,6 +51,7 @@ char *_mqtt_clientid; #if MQTT_SKIP_RETAINED unsigned long _mqtt_connected_at = 0; #endif +unsigned long _mqtt_disconnected_at = 0; std::vector _mqtt_callbacks; @@ -75,9 +76,7 @@ void _mqttConnect() { if (_mqtt.connected()) return; // Check reconnect interval - static unsigned long last = 0; - if (millis() - last < _mqtt_reconnect_delay) return; - last = millis(); + if (millis() - _mqtt_disconnected_at < _mqtt_reconnect_delay) return; // Increase the reconnect delay _mqtt_reconnect_delay += MQTT_RECONNECT_DELAY_STEP; @@ -414,6 +413,8 @@ void _mqttOnConnect() { void _mqttOnDisconnect() { + _mqtt_disconnected_at = millis(); + DEBUG_MSG_P(PSTR("[MQTT] Disconnected!\n")); // Send disconnect event to subscribers