Skip to content

Commit 4639a9f

Browse files
committed
Detect dropped network connection
Check networkStatus() in mqttStatus() before attempting to reconnect MQTT. Fail quickly to avoid the ~20 seconds for MQTT connect attempts to timeout. Allow run() to return if the MQTT connection is down, without any other action. The calling sketch can then check network status, etc. to fix the problem. Checking mqttStatus() once per loop() is enough to allow it to retry the MQTT connection when needed. #99
1 parent 23985c6 commit 4639a9f

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

src/AdafruitIO.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ const __FlashStringHelper* AdafruitIO::statusText()
167167

168168
void AdafruitIO::run(uint16_t busywait_ms)
169169
{
170-
// loop until we have a connection
171-
while(mqttStatus() != AIO_CONNECTED){}
170+
// mqttStatus() will try to reconnect before returning
171+
if(mqttStatus() != AIO_CONNECTED) return;
172172

173173
if(busywait_ms > 0)
174174
_packetread_timeout = busywait_ms;
@@ -239,6 +239,13 @@ aio_status_t AdafruitIO::mqttStatus()
239239
return _status;
240240
}
241241

242+
aio_status_t net_status = networkStatus();
243+
// if we aren't connected, return network status -- fail quickly
244+
if(net_status != AIO_NET_CONNECTED) {
245+
_status = net_status;
246+
return _status;
247+
}
248+
242249
if(_mqtt->connected())
243250
return AIO_CONNECTED;
244251

0 commit comments

Comments
 (0)