Skip to content

Commit

Permalink
MQTT reconnect delay based on last disconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
mcspr committed Dec 28, 2018
1 parent eb249dd commit 7e71c29
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions code/espurna/mqtt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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_callback_f> _mqtt_callbacks;

Expand All @@ -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;
Expand Down Expand Up @@ -414,6 +413,8 @@ void _mqttOnConnect() {

void _mqttOnDisconnect() {

_mqtt_disconnected_at = millis();

DEBUG_MSG_P(PSTR("[MQTT] Disconnected!\n"));

// Send disconnect event to subscribers
Expand Down

0 comments on commit 7e71c29

Please sign in to comment.