|
| 1 | +/* Adafruit IO Example Using WiFiManager with Custom Adafruit IO parameters |
| 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 | + * In addition, this example allows you to enter your Adafruit IO username and key |
| 9 | + * as customer parameters in WiFiManager so that they do not need to be coded into |
| 10 | + * the sketch. |
| 11 | + * |
| 12 | + * This is useful if you want to create projects and share them with others that |
| 13 | + * may use them on a different WiFi network and use a different Adafruit IO account |
| 14 | + * for IOT integrations such as collecting sensor data or voice command integration via |
| 15 | + * IFFT. |
| 16 | + * |
| 17 | + * To use this example, setup a feed called "myfeed". When the ESP8266 or ESP32 |
| 18 | + * microcontroller starts, join the "WiFi Setup" SSID and you should be presented |
| 19 | + * with the config portal. If the config portal does not automatically start you |
| 20 | + * can browse to http://192.168.4.1 to access it |
| 21 | + * |
| 22 | + * Select the SSID and enter the password for WiFi Access in the config portal. |
| 23 | + * Enter your Adafruit IO username and key in the config portal and select "Save". |
| 24 | + * |
| 25 | + * When you manually add data to the feed on io.adafruit.com, you'll see |
| 26 | + * that data written to the serial output. |
| 27 | + * |
| 28 | + * Brad Black - 2022 |
| 29 | + * |
| 30 | + */ |
| 31 | + |
| 32 | +#include <WiFiManager.h> |
| 33 | +#include "AdafruitIO_WiFi.h" |
| 34 | +#include <ArduinoJson.h> |
| 35 | +#include <LittleFS.h> |
| 36 | + |
| 37 | +char IO_USERNAME[64] = ""; |
| 38 | +char IO_KEY[64] = ""; |
| 39 | + |
| 40 | +static uint8_t objStorage[sizeof(AdafruitIO_WiFi)]; // RAM for the object |
| 41 | +AdafruitIO_WiFi *io; // a pointer to the object, once it's constructed |
| 42 | + |
| 43 | +// create WiFiManager object and define our custom parameters |
| 44 | + |
| 45 | +WiFiManager wifiManager; |
| 46 | +WiFiManagerParameter custom_IO_USERNAME("iouser", "Adafruit IO Username", IO_USERNAME, 60); |
| 47 | +WiFiManagerParameter custom_IO_KEY("iokey", "Adafruit IO Key", IO_KEY, 60); |
| 48 | + |
| 49 | +void handleMessage(AdafruitIO_Data *data) |
| 50 | +{ |
| 51 | + |
| 52 | + Serial.print("received <- "); |
| 53 | + Serial.println(data->toString()); |
| 54 | + |
| 55 | +} // handleMessage |
| 56 | + |
| 57 | +// callback notifying us of the need to save config |
| 58 | +void saveConfigCallback() |
| 59 | +{ |
| 60 | + |
| 61 | + Serial.println("Saving new config"); |
| 62 | + |
| 63 | + strcpy(IO_USERNAME, custom_IO_USERNAME.getValue()); |
| 64 | + strcpy(IO_KEY, custom_IO_KEY.getValue()); |
| 65 | + |
| 66 | + DynamicJsonDocument json(256); |
| 67 | + |
| 68 | + json["IO_KEY"] = IO_KEY; |
| 69 | + json["IO_USERNAME"] = IO_USERNAME; |
| 70 | + |
| 71 | + File configFile = LittleFS.open("/config.json", "w"); |
| 72 | + if (!configFile) |
| 73 | + { |
| 74 | + Serial.println("Failed to open config file for writing"); |
| 75 | + } |
| 76 | + |
| 77 | + serializeJson(json, Serial); |
| 78 | + |
| 79 | + serializeJson(json, configFile); |
| 80 | + |
| 81 | + configFile.close(); |
| 82 | +} // end save |
| 83 | + |
| 84 | +void readParamsFromFS() |
| 85 | +{ |
| 86 | + if (LittleFS.begin()) |
| 87 | + { |
| 88 | + |
| 89 | + if (LittleFS.exists("/config.json")) |
| 90 | + { |
| 91 | + // file exists, reading and loading |
| 92 | + Serial.println("Reading config file"); |
| 93 | + |
| 94 | + File configFile = LittleFS.open("/config.json", "r"); |
| 95 | + if (configFile) |
| 96 | + { |
| 97 | + size_t size = configFile.size(); |
| 98 | + // Allocate a buffer to store contents of the file. |
| 99 | + std::unique_ptr<char[]> buf(new char[size]); |
| 100 | + |
| 101 | + configFile.readBytes(buf.get(), size); |
| 102 | + |
| 103 | + DynamicJsonDocument json(256); |
| 104 | + auto deserializeError = deserializeJson(json, buf.get()); |
| 105 | + serializeJson(json, Serial); |
| 106 | + Serial.println(); |
| 107 | + if (!deserializeError) |
| 108 | + { |
| 109 | + |
| 110 | + if (json.containsKey("IO_USERNAME")) |
| 111 | + strcpy(IO_USERNAME, json["IO_USERNAME"]); |
| 112 | + if (json.containsKey("IO_KEY")) |
| 113 | + strcpy(IO_KEY, json["IO_KEY"]); |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + Serial.println("Failed to load json config"); |
| 118 | + } |
| 119 | + configFile.close(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + else |
| 124 | + { |
| 125 | + Serial.println("Failed to mount FS"); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | +void setup() |
| 130 | + |
| 131 | +{ |
| 132 | + Serial.begin(115200); // Initialize serial port for debugging. |
| 133 | + delay(500); |
| 134 | + WiFi.begin(); |
| 135 | + |
| 136 | + readParamsFromFS(); // get parameters from file system |
| 137 | + |
| 138 | + //wifiManager.resetSettings(); //uncomment to reset the WiFi settings |
| 139 | + |
| 140 | + wifiManager.setClass("invert"); // enable "dark mode" for the config portal |
| 141 | + wifiManager.setConfigPortalTimeout(120); // auto close configportal after n seconds |
| 142 | + wifiManager.setAPClientCheck(true); // avoid timeout if client connected to softap |
| 143 | + |
| 144 | + wifiManager.addParameter(&custom_IO_USERNAME); // set custom paraeter for IO username |
| 145 | + wifiManager.addParameter(&custom_IO_KEY); // set custom parameter for IO key |
| 146 | + |
| 147 | + custom_IO_KEY.setValue(IO_KEY, 64); // set custom parameter value |
| 148 | + custom_IO_USERNAME.setValue(IO_USERNAME, 64); // set custom parameter value |
| 149 | + |
| 150 | + wifiManager.setSaveConfigCallback(saveConfigCallback); // set config save notify callback |
| 151 | + |
| 152 | + if (!wifiManager.autoConnect("WiFi Setup")) // connect to wifi with existing setting or start config |
| 153 | + { |
| 154 | + Serial.println("Failed to connect and hit timeout"); |
| 155 | + } |
| 156 | + else |
| 157 | + { |
| 158 | + // if you get here you have connected to the WiFi |
| 159 | + Serial.println("Connected to WiFi."); |
| 160 | + |
| 161 | + // connect to Adafruit IO |
| 162 | + |
| 163 | + io = new (objStorage) AdafruitIO_WiFi(IO_USERNAME, IO_KEY, "", ""); |
| 164 | + |
| 165 | + Serial.printf("Connecting to Adafruit IO with User: %s Key: %s.\n", IO_USERNAME, IO_KEY); |
| 166 | + |
| 167 | + io->connect(); |
| 168 | + |
| 169 | + AdafruitIO_Feed *myfeed = io->feed("myfeed"); |
| 170 | + |
| 171 | + myfeed->onMessage(handleMessage); |
| 172 | + |
| 173 | + myfeed->get(); |
| 174 | + |
| 175 | + // wait for a connection |
| 176 | + |
| 177 | + while ((io->status() < AIO_CONNECTED)) |
| 178 | + { |
| 179 | + Serial.print("."); |
| 180 | + delay(500); |
| 181 | + } |
| 182 | + Serial.println("Connected to Adafruit IO."); |
| 183 | + } |
| 184 | + |
| 185 | +} // setup() |
| 186 | + |
| 187 | +void loop() |
| 188 | +{ |
| 189 | + |
| 190 | + io->run(); |
| 191 | + |
| 192 | +} // loop() |
0 commit comments