|
| 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 | +} |
0 commit comments