Skip to content

Adding WiFiManager Example Sketches #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions examples/adafruitio_27_wifimanager/.pyportal.test.skip
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

80 changes: 80 additions & 0 deletions examples/adafruitio_27_wifimanager/adafruitio_27_wifimanager.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* Adafruit IO Example Using WiFiManager
*
* This is a simple Adafruit feed subscribe example that uses
* WiFiManager to handle setup of WiFi credentials and connecting
* to the network instead of defining the WiFI SSID and password
* explicitly in the code.
*
* To use this example, add your Adafruit IO Username and Key
* and setup a feed called "myfeed". When you manually add data
* to the feed on io.adafruit.com, you'll see that data written to
* the serial output.
*
* Brad Black - 2022
*
*/

#include <WiFiManager.h>
#include "AdafruitIO_WiFi.h"

char IO_USERNAME[64] = "my username";
char IO_KEY[64] = "my key";
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, "", "");
AdafruitIO_Feed *myfeed = io.feed("myfeed");

WiFiManager wifiManager;

void handleMessage(AdafruitIO_Data *data)
{

Serial.print("received <- ");
Serial.println(data->toString());

} // handleMessage

void setup()

{
Serial.begin(115200); // Initialize serial port for debugging.
delay(500);

// wifiManager.resetSettings(); //uncomment to reset the WiFi settings

wifiManager.setClass("invert"); // enable "dark mode" for the config portal
wifiManager.setConfigPortalTimeout(120); // auto close configportal after n seconds
wifiManager.setAPClientCheck(true); // avoid timeout if client connected to softap

if (!wifiManager.autoConnect("WiFi Setup")) // connect to wifi with existing setting or start config
{
Serial.println("failed to connect and hit timeout");
}
else
{
// if you get here you have connected to the WiFi
Serial.println("Connected to WiFi.");

Serial.printf("Connecting to Adafruit IO with User: %s Key: %s.\n", IO_USERNAME, IO_KEY);
io.connect();

myfeed->onMessage(handleMessage);

myfeed->get();

// wait for a connection

while ((io.status() < AIO_CONNECTED))
{
Serial.print(".");
delay(500);
}
Serial.println("Connected to Adafruit IO.");
}

} // setup()

void loop()
{

io.run();

} // loop()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/* Adafruit IO Example Using WiFiManager with Custom Adafruit IO parameters
*
* This is a simple Adafruit feed subscribe example that uses
* WiFiManager to handle setup of WiFi credentials and connecting
* to the network instead of defining the WiFI SSID and password
* explicitly in the code.
*
* In addition, this example allows you to enter your Adafruit IO username and key
* as customer parameters in WiFiManager so that they do not need to be coded into
* the sketch.
*
* This is useful if you want to create projects and share them with others that
* may use them on a different WiFi network and use a different Adafruit IO account
* for IOT integrations such as collecting sensor data or voice command integration via
* IFFT.
*
* To use this example, setup a feed called "myfeed". When the ESP8266 or ESP32
* microcontroller starts, join the "WiFi Setup" SSID and you should be presented
* with the config portal. If the config portal does not automatically start you
* can browse to http://192.168.4.1 to access it
*
* Select the SSID and enter the password for WiFi Access in the config portal.
* Enter your Adafruit IO username and key in the config portal and select "Save".
*
* When you manually add data to the feed on io.adafruit.com, you'll see
* that data written to the serial output.
*
* Brad Black - 2022
*
*/

#include <WiFiManager.h>
#include "AdafruitIO_WiFi.h"
#include <ArduinoJson.h>
#include <LittleFS.h>

char IO_USERNAME[64] = "";
char IO_KEY[64] = "";

static uint8_t objStorage[sizeof(AdafruitIO_WiFi)]; // RAM for the object
AdafruitIO_WiFi *io; // a pointer to the object, once it's constructed

// create WiFiManager object and define our custom parameters

WiFiManager wifiManager;
WiFiManagerParameter custom_IO_USERNAME("iouser", "Adafruit IO Username", IO_USERNAME, 60);
WiFiManagerParameter custom_IO_KEY("iokey", "Adafruit IO Key", IO_KEY, 60);

void handleMessage(AdafruitIO_Data *data)
{

Serial.print("received <- ");
Serial.println(data->toString());

} // handleMessage

// callback notifying us of the need to save config
void saveConfigCallback()
{

Serial.println("Saving new config");

strcpy(IO_USERNAME, custom_IO_USERNAME.getValue());
strcpy(IO_KEY, custom_IO_KEY.getValue());

DynamicJsonDocument json(256);

json["IO_KEY"] = IO_KEY;
json["IO_USERNAME"] = IO_USERNAME;

File configFile = LittleFS.open("/config.json", "w");
if (!configFile)
{
Serial.println("Failed to open config file for writing");
}

serializeJson(json, Serial);

serializeJson(json, configFile);

configFile.close();
} // end save

void readParamsFromFS()
{
if (LittleFS.begin())
{

if (LittleFS.exists("/config.json"))
{
// file exists, reading and loading
Serial.println("Reading config file");

File configFile = LittleFS.open("/config.json", "r");
if (configFile)
{
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);

configFile.readBytes(buf.get(), size);

DynamicJsonDocument json(256);
auto deserializeError = deserializeJson(json, buf.get());
serializeJson(json, Serial);
Serial.println();
if (!deserializeError)
{

if (json.containsKey("IO_USERNAME"))
strcpy(IO_USERNAME, json["IO_USERNAME"]);
if (json.containsKey("IO_KEY"))
strcpy(IO_KEY, json["IO_KEY"]);
}
else
{
Serial.println("Failed to load json config");
}
configFile.close();
}
}

else
{
Serial.println("Failed to mount FS");
}
}
}
void setup()

{
Serial.begin(115200); // Initialize serial port for debugging.
delay(500);
WiFi.begin();

readParamsFromFS(); // get parameters from file system

//wifiManager.resetSettings(); //uncomment to reset the WiFi settings

wifiManager.setClass("invert"); // enable "dark mode" for the config portal
wifiManager.setConfigPortalTimeout(120); // auto close configportal after n seconds
wifiManager.setAPClientCheck(true); // avoid timeout if client connected to softap

wifiManager.addParameter(&custom_IO_USERNAME); // set custom paraeter for IO username
wifiManager.addParameter(&custom_IO_KEY); // set custom parameter for IO key

custom_IO_KEY.setValue(IO_KEY, 64); // set custom parameter value
custom_IO_USERNAME.setValue(IO_USERNAME, 64); // set custom parameter value

wifiManager.setSaveConfigCallback(saveConfigCallback); // set config save notify callback

if (!wifiManager.autoConnect("WiFi Setup")) // connect to wifi with existing setting or start config
{
Serial.println("Failed to connect and hit timeout");
}
else
{
// if you get here you have connected to the WiFi
Serial.println("Connected to WiFi.");

// connect to Adafruit IO

io = new (objStorage) AdafruitIO_WiFi(IO_USERNAME, IO_KEY, "", "");

Serial.printf("Connecting to Adafruit IO with User: %s Key: %s.\n", IO_USERNAME, IO_KEY);

io->connect();

AdafruitIO_Feed *myfeed = io->feed("myfeed");

myfeed->onMessage(handleMessage);

myfeed->get();

// wait for a connection

while ((io->status() < AIO_CONNECTED))
{
Serial.print(".");
delay(500);
}
Serial.println("Connected to Adafruit IO.");
}

} // setup()

void loop()
{

io->run();

} // loop()
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name=Adafruit IO Arduino
version=4.2.2
version=4.2.3
author=Adafruit
maintainer=Adafruit <adafruitio@adafruit.com>
sentence=Arduino library to access Adafruit IO.
paragraph=Arduino library to access Adafruit IO using the Adafruit AirLift, ESP8266, ESP32, ESP32-S2, M0 WINC1500, WICED, MKR1000, Ethernet, or FONA hardware.
category=Communication
url=https://github.com/adafruit/Adafruit_IO_Arduino
architectures=*
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
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