Skip to content

Commit

Permalink
Merge pull request #100 from sellensr/master
Browse files Browse the repository at this point in the history
Don't crash on network disconnect
  • Loading branch information
brentru committed Dec 18, 2019
2 parents 23985c6 + 3950f25 commit 1e70236
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Adafruit IO Arduino
version=3.2.3
version=3.3.0
author=Adafruit
maintainer=Adafruit <adafruitio@adafruit.com>
sentence=Arduino library to access Adafruit IO.
Expand Down
13 changes: 11 additions & 2 deletions src/AdafruitIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,18 @@ const __FlashStringHelper* AdafruitIO::statusText()
}
}

void AdafruitIO::run(uint16_t busywait_ms)
aio_status_t AdafruitIO::run(uint16_t busywait_ms)
{
uint32_t timeStart = millis();
// If we aren't network connected, return status -- fail quickly
if(status() < AIO_NET_CONNECTED) {
return status();
}

// loop until we have a connection
while(mqttStatus() != AIO_CONNECTED){}
// mqttStatus() will try to reconnect before returning
while(mqttStatus() != AIO_CONNECTED && millis() - timeStart < AIO_MQTT_CONNECTION_TIMEOUT){}
if(mqttStatus() != AIO_CONNECTED) return status();

if(busywait_ms > 0)
_packetread_timeout = busywait_ms;
Expand All @@ -180,6 +188,7 @@ void AdafruitIO::run(uint16_t busywait_ms)
_mqtt->ping();
_last_ping = millis();
}
return status();
}

aio_status_t AdafruitIO::status()
Expand Down
2 changes: 1 addition & 1 deletion src/AdafruitIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class AdafruitIO {
virtual ~AdafruitIO();

void connect();
void run(uint16_t busywait_ms = 0);
aio_status_t run(uint16_t busywait_ms = 0);

AdafruitIO_Feed* feed(const char *name);
AdafruitIO_Feed* feed(const char *name, const char *owner);
Expand Down
2 changes: 2 additions & 0 deletions src/AdafruitIO_Definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class AdafruitIOGroupCallback {
#define AIO_PING_INTERVAL 60000
// Time to wait between re-connecting to Adafruit IO after throttled
#define AIO_THROTTLE_RECONNECT_INTERVAL 60000
// Time to wait for a successful reconnection of MQTT
#define AIO_MQTT_CONNECTION_TIMEOUT 60000

#define AIO_ERROR_TOPIC "/errors"
#define AIO_THROTTLE_TOPIC "/throttle"
Expand Down

0 comments on commit 1e70236

Please sign in to comment.