Random Nerd Tutorials
Shares

WiFiManager with ESP8266 – Autoconnect, Custom Parameter and Manage your SSID and Password

Shares

In this guide you’ll learn how to use WiFiManager with the ESP8266 board. WiFiManager allows you to connect your ESP8266 to different Access Points (AP) without having to hard-code and upload new code to your board. Additionally, you can also add custom parameters (variables) and manage multiple SSID connections with the WiFiManager library.

How WiFiManager Works with ESP8266

The WiFiManager is a great library do add to your ESP8266 projects, because using this library you no longer have to hard-code your network credentials (SSID and password). Your ESP will automatically join a known network or set up an Access Point that you can use to configure the network credentials. Here’s how this process works:

  • When your ESP8266 boots, it is set up in Station mode, and tries to connect to a previously saved Access Point (a known SSID and password combination);
  • If this process fails, it sets the ESP into Access Point mode;
  • Using any Wi-Fi enabled device with a browser, connect to the newly created Access Point (default name AutoConnectAP);
  • After establishing a connection with the AutoConnectAP, you can go to the default IP address 192.168.4.1 to open a web page that allows you to configure your SSID and password;
  • Once a new SSID and password is set, the ESP reboots and tries to connect;
  • If it establishes a connection, the process is completed successfully. Otherwise, it will be set up as an Access Point.

This blog post illustrates two different use cases for the WiFiManager library:

  • Example #1 – Autoconnect: Web Server Example
  • Example #2 – Adding Custom Parameters

Prerequisites

Before proceeding with this tutorial we recommend reading the following resources:

Installing WiFiManager and ArduinoJSON

You also need to install the WiFiManager Library and ArduinoJSON Library. Follow these next instructions.

Installing the WiFiManager Library

  1. Click here to download the WiFiManager library. You should have a .zip folder in your Downloads
  2. Unzip the .zip folder and you should get WiFiManager-master folder
  3. Rename your folder from WiFiManager-master to WiFiManager
  4. Move the WiFiManager folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Installing the ArduinoJSON Library

  1. Click here to download the ArduinoJSON library. You should have a .zip folder in your Downloads
  2. Unzip the .zip folder and you should get ArduinoJSON-master folder
  3. Rename your folder from ArduinoJSON-master to ArduinoJSON
  4. Move the ArduinoJSON folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Learn more how to Decode and Encode JSON with Arduino or ESP8266 using the Arduino JSON Library.


Example #1 – WiFiManager with ESP8266: Autoconnect Example

This first example is based on the ESP8266 Web Server post, where you build a web server with an ESP8266 to control two outputs (watch the video tutorial below).


For Example #1 we’ll use the previous project, but instead of hard-coding the SSID and password, you’ll be able to configure it with the WiFiManager library.

Code

Having the ESP8266 add-on for the Arduino IDE installed (How to Install the ESP8266 Board in Arduino IDE), go to Tools and select “ESP-12E” (or choose the development board that you’re using). Here’s the code that you need to upload to your ESP8266:

This code needs to include the following libraries for the WiFiManager:

#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>

You also need to create a WiFiManager object:

WiFiManager wifiManager;

And run the autoConnect() method:

wifiManager.autoConnect("AutoConnectAP");

That’s it! By adding these new lines of code to your ESP8266 projects, you’re able to configure Wi-Fi credentials using the WiFiManager.

Accessing the WiFiManager AP

If it’s your first time running the WiFiManager code with your ESP8266 board, you’ll see the next messages being printed in your Arduino IDE Serial Monitor.

You can either use your computer/laptop to connect to the AutoConnectAP Access point:

Then, open your browser and type the following IP address: 192.168.4.1. This loads the next web page, where you can set your Wi-Fi credentials:

Alternatively, you can use your smartphone, activate Wi-Fi and connect to AutoConnectAP as follows:

You should see a a window similar to the one shown in the figure below. Then, press the “SIGN IN” button:

Configuring the WiFi page

You’ll be redirected to a web page at 192.168.4.1 that allows you to configure your ESP’s WiFi credentials. Press the “Configure WiFi” button:

Choose your desired network by tapping its name and the SSID should be filled instantly (in my case “MEO-620B4B”):

After that, type your password and press “save“:

You’ll see a similar message saying “Credentials Saved“:

In the meanwhile, the Serial Monitor displays the scan results of the available Access Points and message stating that Wi-Fi credentials were saved.

Accessing your web server

Now, if you RESET your ESP board, it will print the IP address in the Serial Monitor (in my case it’s 192.168.1.132):

Open your browser and type the IP address. You should see the web server shown below, that allows you to control two GPIOs on and off:

Parts required and schematic

If you want to make this project work, here’s the parts that you need:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Follow this schematic:


How to Erase Credentials From Your ESP8266

This next line is commented by default, otherwise you would need to configure your ESP8266 every time it boots up.

// Uncomment and run it once, if you want to erase all the stored information
//wifiManager.resetSettings();

If for some reason you want to erase all the saved credentials:

  1. Uncomment the preceding line;
  2. Upload the code to the ESP8266;
  3. Let it run once (reset your board);
  4. Comment that line again;
  5. Upload the code to the ESP8266 with the line commented.

Example #2 – WiFiManager with ESP8266 and Custom Parameters

The WiFiManager has a useful feature that allows you to add custom parameters to the “Configure WiFi” web page. This is extremely useful, because in some applications you might want to add a different API Key, an MQTT broker IP Address, assign a different GPIO, activate a sensor, etc..

In Example #2, you’ll make a web server to control an ESP8266 GPIO pin that is defined with a custom parameter set through the WiFiManager.

Code

Having the ESP8266 add-on for the Arduino IDE installed (How to Install the ESP8266 Board in Arduino IDE), go to Tools and select “ESP-12E” (or choose the development board that you’re using). Here’s the code that you need to upload to your ESP8266:

Adding custom parameters

To add a custom parameter, you need to add a bunch of code (see the preceding sketch) that allows you to manipulate the /config.json file stored in the ESP. For this tutorial we won’t explain how the custom parameters feature works, but basically if you wanted to create another custom parameter follow these next steps.

In this example, we’ll create a variable to store an MQTT broker server IP address. First, create a char variable:

char output[2];
char mqtt_server[40];

Then, if it’s already saved in the /config.json, you can copy it:

strcpy(output, json["output"]);
strcpy(mqtt_server, json["mqtt_server"]);

Create a WiFiManagerParameter (so, the parameter is displayed in the “Configure WiFi” web page:

WiFiManagerParameter custom_output("output", "output", output, 2);
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);

Add the variable as a parameter:

wifiManager.addParameter(&custom_output);
wifiManager.addParameter(&custom_mqtt_server);

Check and update the char variables with the latest value:

strcpy(output, custom_output.getValue());
strcpy(mqtt_server, custom_mqtt_server.getValue());

Finally, if the user submits a new value to one of the parameters, this line updates the /config.json file:

json["output"] = output;
json["mqtt_server"] = mqtt_server;

You could repeat this process to add more custom parameters.

Accessing the WiFiManager AP

Use your smartphone, computer or tablet and connect to the AutoConnectAP Access Point:

You should see a a window similar to the one shown in the figure below. Then, press the “SIGN IN” button:

Configuring the WiFi page

You’ll be redirected to a web page at 192.168.4.1 that allows you to configure your ESP’s WiFi credentials. Press the “Configure WiFi” button:

Choose your desired network by tapping its name and the SSID should be filled instantly (in my case “MEO-620B4B”):

After that, type your password, your desired GPIO number (in my case it’s GPIO 5, so I’ve entered 5) and press “save“:

In the meanwhile, the Serial Monitor displays:

  • Scan results of the available Access Points;
  • Message stating that Wi-Fi credentials were saved;
  • Confirmation that the output parameter (that refers to the GPIO) was set to 5: {“output”:”5″}.

Accessing your web server

Now, if you RESET your ESP board, it will print the IP address in the Serial Monitor (in my case it’s 192.168.1.132):

Open your browser and type the IP address. You should see the web server shown below, that allows you to control the GPIO you’ve defined on and off:

Parts required and schematic

If you want to make this project work, here’s the parts that you need:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Note: if you’ve defined a GPIO number different than GPIO 5 (which is D1 with the NodeMCU board), you need to make a different circuit.

Wrapping Up

That’s it for now, I hope you found this project useful and you can use the WiFiManager library in your projects!

If you like the ESP8266, you may also like:

Do you have any questions? Leave a comment down below!

Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog.


20 Easy Raspberry Pi Projects

Our new book is a beginner-friendly collection of electronics projects, perfectly suited for hobbyists, parents, kids, parents, and educators looking to level up their hardware skills - GET THE BOOK »

Recommended Resources

Home Automation using ESP8266 »
Build IoT projects and home automation gadgets with the ESP8266 Wi-Fi module.

Build a Home Automation System »
Learn how to build a automation system using open-source hardware and software from scratch.

Arduino Step-by-Step Projects »
Build 25 cool Arduino projects with our couse even with no prior experience!

Leave a Comment:

Add Your Reply