Skip to content

Commit 5ebff21

Browse files
committed
Update adafruitio_02_pubsub_protected.ino
Switched to use io.run() for status checking. Comments and formatting
1 parent 57e0ba9 commit 5ebff21

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

examples/adafruitio_02_pubsub_protected/adafruitio_02_pubsub_protected.ino

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ int count = 0;
2828

2929
// set up the 'counter' feed
3030
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");
3431

3532
void setup() {
3633

@@ -44,6 +41,8 @@ void setup() {
4441
}
4542

4643
void setupIO() {
44+
// these operations are specific the AdafruitIO connection and may be repeated
45+
// if the connection fails. https://github.com/adafruit/Adafruit_IO_Arduino/issues/99
4746
Serial.print("Connecting to Adafruit IO");
4847

4948
// connect to io.adafruit.com -- must be called before any other io calls!
@@ -74,36 +73,35 @@ void handleMessage(AdafruitIO_Data *data) {
7473
}
7574

7675
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.
76+
// for this test we will disconnect the WiFi and watch how statuses change -- not normal operations
8277
if(millis()%60000 > 30000){
8378
WiFi.disconnect(); // Wifi will die in the top half of every minute
8479
delay(300); // wait a while for the disaster to show...
8580
}
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");
81+
Serial.print("\n\nTop of Loop!\n");
82+
Serial.print(" io.status(): "); Serial.print(io.status());
83+
Serial.print(" -- "); Serial.print(io.statusText()); Serial.print("\n");
84+
Serial.print("io.networkStatus(): "); Serial.print(io.networkStatus()); Serial.print("\n");
85+
Serial.print(" io.mqttStatus(): "); Serial.print(io.mqttStatus()); Serial.print("\n");
86+
Serial.print(" WiFi.status(): "); Serial.print(WiFi.status()); Serial.print("\n");
9187

88+
// io.run(); is required for all sketches.
89+
// it should always be present at the top of your loop
90+
// function. it keeps the client connected to
91+
// io.adafruit.com, and processes any incoming data.
9292
// 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-
9993
Serial.print("Calling io.run()..."); // io.run() will hang if network disconnected 2019-11-16
100-
io.run(); // process messages and keep connection alive
94+
if(io.run() < AIO_NET_CONNECTED){
95+
Serial.print("No Network Connection!!! Reconnect!!!\n");
96+
count++; // count network restarts
97+
setupIO(); // Start IO all over again
98+
}
10199
Serial.print("Done\n");
102100

103101
// save count to the 'counter' feed on Adafruit IO
104102
Serial.print("sending -> ");
105103
Serial.print(count);
106-
if(counter->save(count)) Serial.print(" sent!");
104+
if(counter->save(count)) Serial.print(" sent! (no proof of receipt)");
107105
else Serial.print(" failed to send!");
108106

109107
// Adafruit IO is rate limited for publishing, so a delay is required in

0 commit comments

Comments
 (0)