Skip to content

Commit a21c9a3

Browse files
authored
Merge pull request #161 from bradrblack/WiFiManager-Example
Adding WiFiManager Example Sketches
2 parents 848e3ea + 5b33739 commit a21c9a3

File tree

7 files changed

+278
-2
lines changed

7 files changed

+278
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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()

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name=Adafruit IO Arduino
2-
version=4.2.2
2+
version=4.2.3
33
author=Adafruit
44
maintainer=Adafruit <[email protected]>
55
sentence=Arduino library to access Adafruit IO.
66
paragraph=Arduino library to access Adafruit IO using the Adafruit AirLift, ESP8266, ESP32, ESP32-S2, M0 WINC1500, WICED, MKR1000, Ethernet, or FONA hardware.
77
category=Communication
88
url=https://github.com/adafruit/Adafruit_IO_Arduino
99
architectures=*
10-
depends=Adafruit MQTT Library, ArduinoHttpClient, Adafruit Unified Sensor, Adafruit NeoPixel, DHT sensor library, Ethernet, Adafruit Si7021 Library, Adafruit SGP30 Sensor, Adafruit BME280 Library, Adafruit LIS3DH, Adafruit VEML6070 Library, ESP32Servo
10+
depends=Adafruit MQTT Library, ArduinoHttpClient, Adafruit Unified Sensor, Adafruit NeoPixel, DHT sensor library, Ethernet, Adafruit Si7021 Library, Adafruit SGP30 Sensor, Adafruit BME280 Library, Adafruit LIS3DH, Adafruit VEML6070 Library, ESP32Servo, WiFiManager, ArduinoJson

0 commit comments

Comments
 (0)