Skip to content

Commit 57e0ba9

Browse files
committed
add timeout in io.run()
io.run will now return after a very long timeout instead of just hanging
1 parent 246a482 commit 57e0ba9

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/AdafruitIO.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ const __FlashStringHelper* AdafruitIO::statusText()
167167

168168
aio_status_t AdafruitIO::run(uint16_t busywait_ms)
169169
{
170+
uint32_t timeStart = millis();
170171
aio_status_t net_status = networkStatus();
171172
// If we aren't connected, return network status -- fail quickly
172173
// Previous version would just hang on a network error
@@ -177,7 +178,8 @@ aio_status_t AdafruitIO::run(uint16_t busywait_ms)
177178

178179
// loop until we have a connection
179180
// mqttStatus() will try to reconnect before returning
180-
while(mqttStatus() != AIO_CONNECTED){}
181+
while(mqttStatus() != AIO_CONNECTED && millis() - timeStart < ADAFRUITIO_RUN_TIMEOUT){}
182+
if(mqttStatus() != AIO_CONNECTED) return _status;
181183

182184
if(busywait_ms > 0)
183185
_packetread_timeout = busywait_ms;

src/AdafruitIO.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
#ifndef ADAFRUITIO_H
1313
#define ADAFRUITIO_H
1414

15+
#ifndef ADAFRUITIO_RUN_TIMEOUT
16+
#define ADAFRUITIO_RUN_TIMEOUT 60000
17+
#endif
18+
1519
#include "Arduino.h"
1620
#include "Adafruit_MQTT.h"
1721
#include "AdafruitIO_Definitions.h"

0 commit comments

Comments
 (0)