|
| 1 | +/* Adafruit IO Example Using WiFiManager |
| 2 | + * |
| 3 | + * This is a simple Adafruit feed subscribe example that uses |
| 4 | + * WiFiManager to handle setup of WiFi credentials and connecting |
| 5 | + * to the network instead of defining the WiFI SSID and password |
| 6 | + * explicitly in the code. |
| 7 | + * |
| 8 | + * To use this example, add your Adafruit IO Username and Key |
| 9 | + * and setup a feed called "myfeed". When you manually add data |
| 10 | + * to the feed on io.adafruit.com, you'll see that data written to |
| 11 | + * the serial output. |
| 12 | + * |
| 13 | + * Brad Black - 2022 |
| 14 | + * |
| 15 | + */ |
| 16 | + |
| 17 | +#include <WiFiManager.h> |
| 18 | +#include "AdafruitIO_WiFi.h" |
| 19 | + |
| 20 | +char IO_USERNAME[64] = "my username"; |
| 21 | +char IO_KEY[64] = "my key"; |
| 22 | +AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, "", ""); |
| 23 | +AdafruitIO_Feed *myfeed = io.feed("myfeed"); |
| 24 | + |
| 25 | +WiFiManager wifiManager; |
| 26 | + |
| 27 | +void handleMessage(AdafruitIO_Data *data) |
| 28 | +{ |
| 29 | + |
| 30 | + Serial.print("received <- "); |
| 31 | + Serial.println(data->toString()); |
| 32 | + |
| 33 | +} // handleMessage |
| 34 | + |
| 35 | +void setup() |
| 36 | + |
| 37 | +{ |
| 38 | + Serial.begin(115200); // Initialize serial port for debugging. |
| 39 | + delay(500); |
| 40 | + |
| 41 | + // wifiManager.resetSettings(); //uncomment to reset the WiFi settings |
| 42 | + |
| 43 | + wifiManager.setClass("invert"); // enable "dark mode" for the config portal |
| 44 | + wifiManager.setConfigPortalTimeout(120); // auto close configportal after n seconds |
| 45 | + wifiManager.setAPClientCheck(true); // avoid timeout if client connected to softap |
| 46 | + |
| 47 | + if (!wifiManager.autoConnect("WiFi Setup")) // connect to wifi with existing setting or start config |
| 48 | + { |
| 49 | + Serial.println("failed to connect and hit timeout"); |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + // if you get here you have connected to the WiFi |
| 54 | + Serial.println("Connected to WiFi."); |
| 55 | + |
| 56 | + Serial.printf("Connecting to Adafruit IO with User: %s Key: %s.\n", IO_USERNAME, IO_KEY); |
| 57 | + io.connect(); |
| 58 | + |
| 59 | + myfeed->onMessage(handleMessage); |
| 60 | + |
| 61 | + myfeed->get(); |
| 62 | + |
| 63 | + // wait for a connection |
| 64 | + |
| 65 | + while ((io.status() < AIO_CONNECTED)) |
| 66 | + { |
| 67 | + Serial.print("."); |
| 68 | + delay(500); |
| 69 | + } |
| 70 | + Serial.println("Connected to Adafruit IO."); |
| 71 | + } |
| 72 | + |
| 73 | +} // setup() |
| 74 | + |
| 75 | +void loop() |
| 76 | +{ |
| 77 | + |
| 78 | + io.run(); |
| 79 | + |
| 80 | +} // loop() |
0 commit comments