Introduction: Managing Configurations on Your ESP8266 Using WiFiManager

Hello everyone!

In a previous Instructable, I talked about Avoiding hard-coding WiFi credentials on your ESP8266 using the great WifiManager Library.

You can also use the library to manage your Arduino sketch's configurations and in this instructable I'm going to show you how. There is a little more setup steps than the WiFi Credentials Instructable, but it's still very straight forward.

Check out the video or continue reading here to find out how to add it and how it works.

Step 1: What's the Advantage of This?

Especially with the addition of networking capabilities, sketches contain a lot of configuration values such as API keys or server addresses.

Normally you would need to re-program your device to change these values, but using WiFiManager you can configure them easily.

It also makes your sketches more shareable, as you really don't want to be checking in your API Keys, like I have many many times!

Step 2: What Your Sketch Needs

You sketch will need a couple of things to achieve this setup.

In normal behaviour, WiFiManager will launch config mode if it fails to connect to the WiFi network, which is fine for just WiFi Credentials, but if you are managing other configurations as well you will need to be able to launch the WiFiManager config mode on demand. The easiest way to do this is with a button.

The other thing your sketch will need is the ability to persist your configurations across restarts of your device, I think the best way of doing is using SPIFFS. The code in the example in the final step has methods for reading and saving your configurations to SPIFFS, but if you want to learn more about it Andreas Spiess (the guy with the Swiss accent) has a really good video on it.

Step 3: How It Looks With Configurations

When config mode is activated on the device, either by failing to connect to the previously known network or by the user pressing the button, it will create an adhoc network that you will connect to.

After you connect any requests on your browser will automatically redirect to the devices config portal.

From here you can enter the details of the WiFi Network you want to connect to, and also set the configurations.

Step 4: Adding the Additional Configurations to WiFiManager

Now all that's left to do is to show you add these additional configurations to the WiFiManager portal. I'm going to use an example from my Arduino-YouTube-Api library to show it working.

I'm not going to through all the sketch, but if you have any questions please do ask in the comments!

Note: Each step here corresponds with one of the images from above.

  1. The first thing you'll see is the "setSaveConfigCallback" which basically is just a method that sets the flag that lets the sketch know it needs to save the config.
  2. The second thing is WiFiManagerParameters, you need to initialize one for each additional configuration you want. These take 4 inputs:
    1. ID (not overly important, just make it unique)
    2. Placeholder (example shown in image 3)
    3. Default value (as shown in Image 4)
    4. Max Length (limits the length of the value the user can enter)
  3. You then can read the values from the WiFiManagerParameters using the "getValue()" method.
  4. The autoConnect line is where you set the name of the adhoc access point that you want config mode to broadcast, I've also added a password to it for additional security.

Hopefully you find this useful and as always if you have any questions please ask!

Thanks,

Brian

Comments

author
ludmina (author)2017-06-08

Hi Brian ,

I have the sketch like below :

#include <ESP8266WiFi.h>

#include <WiFiClient.h>

#include <ESP8266WebServer.h>

//#include <ESP8266mDNS.h>

// Fill in your WiFi router SSID and password

const char *ssid = "suzuki";

const char *password = "12345678";

//MDNSResponder mdns;

ESP8266WebServer server(80);

const char INDEX_HTML[] =

"<!DOCTYPE HTML>"

"<html>"

"<head>"

"<meta name = \"viewport\" content = \"width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable=0\">"

"<title>Suzuki Wagon R+</title>"

"<style>"

"\"body { background-color: #808080; font-family: Arial, Helvetica, Sans-Serif; Color: #000000; }\""

"</style>"

"</head>"

"<body>"

"<h1>Suzuki Wagon R+</h1>"

"<h3>Door Open Controller</h3>"

"<FORM action=\"/\" method=\"post\">"

"<P>"

"Username <input type=\"text\" name=\"username\" id=\"username\" />"

"<P>"

"Password <input type=\"password\" name=\"password\" id=\"password\" />"

"<P>"

"<P>"

"<INPUT type=\"radio\" name=\"LED\" value=\"1\">Open Now<BR>"

"<P>"

"<INPUT type=\"submit\" value=\"Send\"> <INPUT type=\"reset\">"

"</P>"

"</FORM>"

"<P>"

"Copied & Modified by Cuong Ngo Tan"

"</body>"

"</html>";

// GPIO#0 is for Adafruit ESP8266 HUZZAH board. Your board LED might be on 13.

const int LEDPIN1 = 4;

const int LEDPIN2 = 5;

void handleRoot()

{

if (server.hasArg("LED")) {

handleSubmit();

}

else {

server.send(200, "text/html", INDEX_HTML);

}

}

void returnFail(String msg)

{

server.sendHeader("Connection", "close");

server.sendHeader("Access-Control-Allow-Origin", "*");

server.send(500, "text/plain", msg + "\r\n");

}

void handleSubmit()

{

String LEDvalue;

String user;

String pass;

if (!server.hasArg("LED")) return returnFail("BAD ARGS");

LEDvalue = server.arg("LED");

user = server.arg("username");

pass = server.arg("password");

if ((LEDvalue == "1") && (pass == "xxxxx")) {

Serial.println(LEDvalue);

Serial.println(user);

Serial.println(pass);

// writeLED(true);

opendoor();

server.send(200, "text/html", INDEX_HTML);

}

else if ((LEDvalue == "0") && (pass == "xxxxx")) {

Serial.println(LEDvalue);

Serial.println(user);

Serial.println(pass);

writeLED(false);

server.send(200, "text/html", INDEX_HTML);

}

else {

returnFail("Bad LED value");

}

}

void returnOK()

{

server.sendHeader("Connection", "close");

server.sendHeader("Access-Control-Allow-Origin", "*");

server.send(200, "text/plain", "OK\r\n");

}

/*

* Imperative to turn the LED on using a non-browser http client.

* For example, using wget.

* $ wget http://esp8266webform/ledon

*/

void handleLEDon()

{

writeLED(true);

returnOK();

}

/*

* Imperative to turn the LED off using a non-browser http client.

* For example, using wget.

* $ wget http://esp8266webform/ledoff

*/

void handleLEDoff()

{

writeLED(false);

returnOK();

}

void handleNotFound()

{

String message = "File Not Found\n\n";

message += "URI: ";

message += server.uri();

message += "\nMethod: ";

message += (server.method() == HTTP_GET)?"GET":"POST";

message += "\nArguments: ";

message += server.args();

message += "\n";

for (uint8_t i=0; i<server.args(); i++){

message += " " + server.argName(i) + ": " + server.arg(i) + "\n";

}

server.send(404, "text/plain", message);

}

void opendoor()

{

digitalWrite(LEDPIN1, 1);

digitalWrite(LEDPIN2, 1);

delay(700);

digitalWrite(LEDPIN1, 0);

digitalWrite(LEDPIN2, 0);

}

void writeLED(bool LEDon)

{

// Note inverted logic for Adafruit HUZZAH board

if (LEDon) {

digitalWrite(LEDPIN1, 1);

digitalWrite(LEDPIN2, 1);

}

else {

digitalWrite(LEDPIN1, 0);

digitalWrite(LEDPIN2, 0);

}

}

void setup(void)

{

pinMode(LEDPIN1, OUTPUT);

pinMode(LEDPIN2, OUTPUT);

writeLED(false);

Serial.begin(115200);

Serial.println("");

WiFi.softAP(ssid, password);

IPAddress apip = WiFi.softAPIP();

Serial.print("visit: \n");

Serial.println(apip);

server.on("/", handleRoot);

//server.on("/ledon", handleLEDon);

//server.on("/ledoff", handleLEDoff);

server.onNotFound(handleNotFound);

server.begin();

}

void loop(void)

{

server.handleClient();

}

===================

Look the code will make ESP8266 work in AP mode and hard code with SSID : suzuki and password : 12345678 . When user connect to this SSID and access webserver 192.168.4.1 it will show the submit form that help to control LedPin 4 , 5

Now I want to modify my sketch that users can change SSID and password of wifi follow their demand but after they changed they still need to submit the form (with any username and password "xxxxx" ) that I designed in my sketch above . Can you advise what will I do ? Tks

About This Instructable

1,125views

34favorites

License:

Bio: Hey everyone! My name is Brian and thanks for checking my Instructables. I'm a software developer by trade but I've recently gotten into ... More »
More by witnessmenow:Using Witnessmenow's ESP8266/Arduino LibrariesArduino Commute CheckerSocial Stats Counter
Add instructable to: