Skip to content

Commit eb3379d

Browse files
committed
Adding new example with custom AIO parameters
1 parent 97d093d commit eb3379d

File tree

1 file changed

+191
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)