Skip to content

Commit 51d3096

Browse files
committed
io.run now returns status, doesn't hang
This walks back my more agreesive solution in the interest of backwards compatibility. It provides a new example that protects against dropped network connections.
1 parent 8edac23 commit 51d3096

File tree

4 files changed

+196
-3
lines changed

4 files changed

+196
-3
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Adafruit IO Publish/Subscribe Example with Protection
2+
//
3+
// Adafruit invests time and resources providing this open source code.
4+
// Please support Adafruit and open source hardware by purchasing
5+
// products from Adafruit!
6+
//
7+
// Written by Todd Treece for Adafruit Industries
8+
// Copyright (c) 2016 Adafruit Industries
9+
// Licensed under the MIT license.
10+
//
11+
// All text above must be included in any redistribution.
12+
13+
/************************** Configuration ***********************************/
14+
// Comment out if you do not maintain an ssid.h file for your personal credentials.
15+
// Then add your own credentials in the config.h tab.
16+
#include "ssid.h"
17+
18+
// edit the config.h tab and enter your Adafruit IO credentials
19+
// and any additional configuration needed for WiFi, cellular,
20+
// or ethernet clients.
21+
#include "config.h"
22+
23+
/************************ Example Starts Here *******************************/
24+
// This pub/sub example tests the status and reconnects if the network goes down.
25+
26+
// this int will hold the current count for our sketch
27+
int count = 0;
28+
29+
// set up the 'counter' feed
30+
AdafruitIO_Feed *counter = io.feed("counter");
31+
// https://io.adafruit.com/api/docs/mqtt.html#username-errors
32+
//AdafruitIO_Feed *throttles = io.feed("throttle");
33+
//AdafruitIO_Feed *errors = io.feed("errors");
34+
35+
void setup() {
36+
37+
// start the serial connection
38+
Serial.begin(115200);
39+
40+
// wait for serial monitor to open
41+
while(! Serial);
42+
43+
setupIO();
44+
}
45+
46+
void setupIO() {
47+
Serial.print("Connecting to Adafruit IO");
48+
49+
// connect to io.adafruit.com -- must be called before any other io calls!
50+
// calling io.status() first will make the subsequent connection attempt fail.
51+
io.connect();
52+
counter->onMessage(handleMessage);
53+
54+
// wait for a connection
55+
unsigned long started = millis();
56+
while(io.status() < AIO_CONNECTED) {
57+
if(millis() - started > 30000){
58+
started = millis();
59+
io.connect(); // try again if you run out of patience
60+
}
61+
Serial.print(".");
62+
delay(500);
63+
}
64+
65+
// we are connected
66+
Serial.println();
67+
Serial.println(io.statusText());
68+
counter->get(); // https://io.adafruit.com/api/docs/mqtt.html#using-the-get-topic
69+
}
70+
71+
void handleMessage(AdafruitIO_Data *data) {
72+
Serial.print("\nreceived <- ");
73+
Serial.println(data->value());
74+
}
75+
76+
void loop() {
77+
78+
// io.run(); is required for all sketches.
79+
// it should always be present at the top of your loop
80+
// function. it keeps the client connected to
81+
// io.adafruit.com, and processes any incoming data.
82+
if(millis()%60000 > 30000){
83+
WiFi.disconnect(); // Wifi will die in the top half of every minute
84+
delay(300); // wait a while for the disaster to show...
85+
}
86+
Serial.print("\n\nTop of Loop!\nio.status: ");Serial.print(io.status());
87+
Serial.print(": "); Serial.print(io.statusText()); Serial.print("\n");
88+
Serial.print("io.networkStatus: "); Serial.print(io.networkStatus()); Serial.print("\n");
89+
Serial.print("io.mqttStatus: "); Serial.print(io.mqttStatus()); Serial.print("\n");
90+
Serial.print("WiFi.status: "); Serial.print(WiFi.status()); Serial.print("\n");
91+
92+
// Here's where we check the network and start over if it has lost connection
93+
if(io.status() < AIO_NET_CONNECTED){
94+
Serial.print("Not Net Connected to IO!!! Reconnect!!!\n\n");
95+
count++;
96+
setupIO(); //Start IO all over again
97+
}
98+
99+
Serial.print("Calling io.run()..."); // io.run() will hang if network disconnected 2019-11-16
100+
io.run(); // process messages and keep connection alive
101+
Serial.print("Done\n");
102+
103+
// save count to the 'counter' feed on Adafruit IO
104+
Serial.print("sending -> ");
105+
Serial.print(count);
106+
if(counter->save(count)) Serial.print(" sent!");
107+
else Serial.print(" failed to send!");
108+
109+
// Adafruit IO is rate limited for publishing, so a delay is required in
110+
// between feed->save events. In this example, we will wait three seconds
111+
// (1000 milliseconds == 1 second) during each loop.
112+
// Adding the if statement allows you to generate a burst of faster saves
113+
// to generate some throttling events and see how the code responds.
114+
if(millis() < 90000 || millis() > 120000) delay(3000);
115+
else delay(5000);
116+
117+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/************************ Adafruit IO Config *******************************/
2+
3+
// visit io.adafruit.com if you need to create an account,
4+
// or if you need your Adafruit IO key.
5+
// Uncomment these lines and add your own credentials if needed
6+
//#define IO_USERNAME "your_username"
7+
//#define IO_KEY "your_key"
8+
9+
/******************************* WIFI **************************************/
10+
11+
// the AdafruitIO_WiFi client will work with the following boards:
12+
// - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471
13+
// - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821
14+
// - Feather HUZZAH ESP32 -> https://www.adafruit.com/product/3405
15+
// - Feather M0 WiFi -> https://www.adafruit.com/products/3010
16+
// - Feather WICED -> https://www.adafruit.com/products/3056
17+
// - Adafruit PyPortal -> https://www.adafruit.com/product/4116
18+
// - Adafruit Metro M4 Express AirLift Lite -> https://www.adafruit.com/product/4000
19+
// - Adafruit AirLift Breakout -> https://www.adafruit.com/product/4201
20+
// - Adafruit AirLift Shield -> https://www.adafruit.com/product/4285
21+
// - Adafruit AirLift FeatherWing -> https://www.adafruit.com/product/4264
22+
23+
//#define WIFI_SSID "your_ssid"
24+
//#define WIFI_PASS "your_pass"
25+
26+
// uncomment the following line if you are using airlift
27+
// #define USE_AIRLIFT
28+
29+
// uncomment the following line if you are using winc1500
30+
#define USE_WINC1500
31+
32+
// comment out the following lines if you are using fona or ethernet
33+
#include "AdafruitIO_WiFi.h"
34+
35+
#if defined(USE_AIRLIFT) || defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE)
36+
// Configure the pins used for the ESP32 connection
37+
#if !defined(SPIWIFI_SS) // if the wifi definition isnt in the board variant
38+
// Don't change the names of these #define's! they match the variant ones
39+
#define SPIWIFI SPI
40+
#define SPIWIFI_SS 10 // Chip select pin
41+
#define NINA_ACK 9 // a.k.a BUSY or READY pin
42+
#define NINA_RESETN 6 // Reset pin
43+
#define NINA_GPIO0 -1 // Not connected
44+
#endif
45+
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS, SPIWIFI_SS, NINA_ACK, NINA_RESETN, NINA_GPIO0, &SPIWIFI);
46+
#else
47+
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
48+
#endif
49+
/******************************* FONA **************************************/
50+
51+
// the AdafruitIO_FONA client will work with the following boards:
52+
// - Feather 32u4 FONA -> https://www.adafruit.com/product/3027
53+
54+
// uncomment the following two lines for 32u4 FONA,
55+
// and comment out the AdafruitIO_WiFi client in the WIFI section
56+
// #include "AdafruitIO_FONA.h"
57+
// AdafruitIO_FONA io(IO_USERNAME, IO_KEY);
58+
59+
/**************************** ETHERNET ************************************/
60+
61+
// the AdafruitIO_Ethernet client will work with the following boards:
62+
// - Ethernet FeatherWing -> https://www.adafruit.com/products/3201
63+
64+
// uncomment the following two lines for ethernet,
65+
// and comment out the AdafruitIO_WiFi client in the WIFI section
66+
// #include "AdafruitIO_Ethernet.h"
67+
// AdafruitIO_Ethernet io(IO_USERNAME, IO_KEY);

src/AdafruitIO.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,18 @@ const __FlashStringHelper* AdafruitIO::statusText()
165165
}
166166
}
167167

168-
void AdafruitIO::run(uint16_t busywait_ms)
168+
aio_status_t AdafruitIO::run(uint16_t busywait_ms)
169169
{
170+
aio_status_t net_status = networkStatus();
171+
// If we aren't connected, return network status -- fail quickly
172+
// Previous version would just hang on a network error
173+
if(net_status != AIO_NET_CONNECTED) {
174+
_status = net_status;
175+
return _status;
176+
}
177+
170178
// loop until we have a connection
179+
// mqttStatus() will try to reconnect before returning
171180
while(mqttStatus() != AIO_CONNECTED){}
172181

173182
if(busywait_ms > 0)
@@ -241,7 +250,7 @@ aio_status_t AdafruitIO::mqttStatus()
241250

242251
if(_mqtt->connected())
243252
return AIO_CONNECTED;
244-
253+
245254
switch(_mqtt->connect(_username, _key)) {
246255
case 0:
247256
return AIO_CONNECTED;

src/AdafruitIO.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class AdafruitIO {
4545
virtual ~AdafruitIO();
4646

4747
void connect();
48-
void run(uint16_t busywait_ms = 0);
48+
aio_status_t run(uint16_t busywait_ms = 0);
4949

5050
AdafruitIO_Feed* feed(const char *name);
5151
AdafruitIO_Feed* feed(const char *name, const char *owner);

0 commit comments

Comments
 (0)