Skip to content

Commit f3d1eb6

Browse files
committed
allow mqttStatus() to return without waiting for connection
Old version would delay(AIO_THROTTLE_RECONNECT_INTERVAL) before returning. This could leave the sketch stuck in limbo for 60 seconds. Modification remembers last try at connection and doesn't try again until the interval has passed.
1 parent 4639a9f commit f3d1eb6

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

src/AdafruitIO.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ char* AdafruitIO::userAgent()
230230

231231
aio_status_t AdafruitIO::mqttStatus()
232232
{
233-
// if the connection failed,
233+
static uint32_t lastTried = 0; // remember last attempt to connect
234+
// if the connection irretrievably failed,
234235
// return so we don't hammer IO
235236
if(_status == AIO_CONNECT_FAILED)
236237
{
@@ -248,7 +249,12 @@ aio_status_t AdafruitIO::mqttStatus()
248249

249250
if(_mqtt->connected())
250251
return AIO_CONNECTED;
252+
253+
// don't try to connect more often than the throttle interval
254+
if(lastTried != 0 && millis() < (lastTried + AIO_THROTTLE_RECONNECT_INTERVAL))
255+
return AIO_DISCONNECTED;
251256

257+
lastTried = millis();
252258
switch(_mqtt->connect(_username, _key)) {
253259
case 0:
254260
return AIO_CONNECTED;
@@ -260,8 +266,6 @@ aio_status_t AdafruitIO::mqttStatus()
260266
case 3: // mqtt service unavailable
261267
case 6: // throttled
262268
case 7: // banned -> all MQTT bans are temporary, so eventual retry is permitted
263-
// delay to prevent fast reconnects
264-
delay(AIO_THROTTLE_RECONNECT_INTERVAL);
265269
return AIO_DISCONNECTED;
266270
default:
267271
return AIO_DISCONNECTED;

0 commit comments

Comments
 (0)