Better WiFi management on ESP8266 with WiFiManager

As you can see in my previous posts on ESP8266, I tend to hardcode the WiFi SSID and password in my project files. This is a typical thing for a beginner as I am. This is not handy nor reasonable – this way I’m tied to the network I hardcoded and on each change in my infrastructure, I have to recompile my projects and upload them to the board again.

For my latest creations, I started to use WiFiManager by Tzapu. It is really easy to use and gives me an opportunity to change my WiFi network without recompiling and reuploading the script.

The library installation

I’m using the Arduino IDE, hence the installation method: go to the Tools menu and select Manage Libraries. In the management window, type “wifimanager” in the filter box and wait a while. Once the list refreshes showing various WiFi Managers, you can scroll down to the one provided by Tzapu:

Now you can hover your mouse over this entry and click Install button that appears. Note that there are multiple WiFi Managers available. The one I’m describing here is simply my chosen one. The one I’m used to.

The sample script

So, how the sample script looks like? You can be surprised how short it is:

#include <WiFiManager.h>

WiFiManager wifiManager;

void setup() {
  Serial.begin(115200);
  wifiManager.autoConnect();
}

void loop() {
}

As you can see, there is only one library imported – the WiFi Manager itself. There is also an instance of WiFi Manager created and all I need to do is to place “wifiManager.autoConnect();” in the setup part of the script.

How does it work?

Assuming that there are no past WiFi details stored in the module memory, the WiFi manager is not able to connect to any WiFi network. Here is what you can find in the serial monitor:

*WM: [2] AP has anonymous access! 
*WM: [1] SoftAP Configuration 
*WM: [1] -------------------- 
*WM: [1] ssid:             ESP_7FF7CC
*WM: [1] password:         
*WM: [1] ssid_len:         10
*WM: [1] channel:          1
*WM: [1] authmode:        
*WM: [1] ssid_hidden:     
*WM: [1] max_connection:   4
*WM: [1] country:          CN
*WM: [1] beacon_interval:  100(ms)
*WM: [1] -------------------- 
*WM: [1] AP IP address: 192.168.4.1
*WM: [3] setupConfigPortal 
*WM: [1] Starting Web Portal 
*WM: [3] dns server started with ip:  192.168.4.1
*WM: [2] HTTP server started 
*WM: [2] WiFi Scan completed in 2182 ms
*WM: [2] Config Portal Running, blocking, waiting for clients... 

The WiFi monitor created his own WiFi network of the name ESP_7FF7CC – the end of the network name is the end of the MAC address of the module. Not only the WiFi network was launched, but also a web server, DNS server, and in the meantime it also scanned available networks. In order to configure the connection, you should connect to the ESP_7FF7CC network (the name will be different in your case) and try to open the website of your choice. No matter what you will try, you will end up on the manager’s website, because of the DNS server provided by the manager. Let’s try to do this on the phone. First, I connected to the network:

Then, switched to my browser and entered “test.com” as the URL. One way or another, I will end up on the manager’s page:

Next, I clicked on the Configure WiFi, which leads me to the list of nearby WiFi networks:

You can enter the network SSID and password by hand (if the SSID is not broadcasted) or you can click on one of the networks visible on the list. I selected my favorite network and provided password:

Once I clicked Save, there was notification displayed:

And here is what I saw on the Serial Monitor:

*WM: [2] Connecting as wifi client... 
*WM: [3] STA static IP:
*WM: [2] setSTAConfig static ip not set, skipping 
*WM: [1] CONNECTED:
*WM: [1] Connecting to NEW AP: dularemtk
*WM: [3] Using Password: my_network_password_:)_was_here
*WM: [3] WiFi station enable 
*WM: [3] enableSTA PERSISTENT ON 
*WM: [1] connectTimeout not set, ESP waitForConnectResult... 
*WM: [2] Connection result: WL_CONNECTED
*WM: [3] lastconxresult: WL_CONNECTED
*WM: [1] Connect to new AP [SUCCESS] 
*WM: [1] Got IP Address: 
*WM: [1] 192.168.1.181 
*WM: [2] disconnect configportal 
*WM: [2] restoring usermode STA
*WM: [2] wifi status: WL_CONNECTED
*WM: [2] wifi mode: STA
*WM: [1] config portal exiting

Great! The module is now connected to my WiFi network. In the future, it will first try to connect to this network and will launch Web Server and the rest of the tools only if the connection will not be available. So, in the typical situation, the manager will simply reconnect to the last known network. Once I move my module to the other place, I can re-enter the configuration and configure the new WiFi network of my choice.

Is it all?

The above is only a basic path you can choose in most of your projects. Simple enough to get you going. But the WiFi Manger is more powerful. As of February 2021, the documentation is a little bit outdated, but it shows how many possibilities you have.

Starting from custom Access Point names, custom IP addresses, the configuration of the default WiFi network, through the manual request of the Config Portal start (the website on which you can configure networks), till the configuration of MQTT or other parameters you want to use in your projects.

Leave a Reply

Your email address will not be published. Required fields are marked *