Home | Difficulty level | Advanced | WiFiManager library: easily manage the Wi-Fi connection of projects ESP8266

WiFiManager library: easily manage the Wi-Fi connection of projects ESP8266

The WiFiManager library allows you to easily manage the Wi-Fi connection in your ESP8266 projects. The WiFi connection is very well supported by many libraries, for example Blynk, Cayenne, Homie, ESP Easy. For more simple projects or outside the framework of these libraries, you must manage the connection to the Wi-Fi network yourself. The WiFiManager library starts the ESP8266 in access point mode (AP mode) Configuration window to a WiFi network. It is also possible to enter other parameters (MQTT server, Token Blynk …) at the same time as the WiFi parameters.

Advantages of the WiFiManager library

It can also be very interesting not to have a username and password on a Wi-Fi network in the Arduino code of the project. For example, the object should connect to a network whose IDs you do not know at the time of development. IDs can change. For example, in a school, the identifiers and the password are changed at each school year. The access point has been replaced and the new password is different. In short, you have understood, by avoiding coding “hard” ID and password, you will not need to go back through the Arduino IDE in case of change of password, hardware or Access point.

Install the WiFiManager library in the Arduino IDE

The WiFiManager library is directly available from the Arduino IDE library manager.The official page of the project can be found on GitHub here. The project has been developed and is maintained by tzapu (its personal page). It is a very popular project (1500 stars) which is maintained very regularly.

Open the Library Manager (Sketch / Include Library / Manage Libraries)

wifimanager 0 add library ide arduino
Search for WiFiManager in the search box and then install.
wifimanager 00 librarie

That’s all.

How to use the library

The WiFiManager library runs when the ESP8266 is started. When the ESP starts, the library tries to connect to the last known network or to the network indicated in the code (yes it is also possible).

Here’s how. At the beginning of the program, the necessary libraries are declared

In the setup loop, the library is initialized

Then connect to the Wi-Fi network with the autoConnect () function. Several configurations are possible:

  • AutoConnect(): the library is left to define an identifier composed of the prefix ESP associated with the serial number of the ESP8266. It is not very wise if you have several devices
  • AutoConnect(“AP-NAME”): indicates the name that the access point will carry.
  • AutoConnect(“AP-NAME”, “AP-PASSWORD): and a password is defined to protect the WiFi network connection page.

Therefore, if no WiFi network has been configured or if the WiFi network is inaccessible, an access point is created. Here is a basic program that you can use to test the library.

As you can see, there are several complementary methods:

  • ResetSettings: allows you to re-initialize the connection parameters. This is useful for debugging or forcing the ESP to start in AP mode. Useful for testing different networks
  • SetAPStaticIPConfig: Specifies the IP address of the access point.

After uploading the program to the ESP8266, use a computer, tablet, or smartphone to connect to the access point created by the computer.

wifimanager 1 apmode connexion point acces

Choose the network created by the ESP8266. A login window opens automatically. For example, choose the Configure WiFi method that will list wi-Fi networks within range.

wifimanager 2 fonctions connexion

Choose the desired network and enter the password. Finish with Save. The connection parameters are saved for the next reboot. The ESP is restarted immediately with these new settings and your program starts immediately.

wifimanager 3 connection reseau wifi

You can easily track the progress of the connection process (and identify problems) by opening the serial monitor. The paper is very detailed.

Other connection strategies are available in the example folder here

  • AutoConnect: the basic connection
  • AutoConnectWithFSParameters: parameters are stored in the SPIFFS memory area of ​​the ESP8266 and reloaded at startup
  • AutoConnectWithFSParametersAndCustomIP: idem but with a fixed IP and connection parameters to u MQTT server. The parameters are stored in JSON format using the ArduinoJson library presented in this tutorial
  • AutoConnectWithFeedback:
  • AutoConnectWithFeedbackLED
  • AutoConnectWithReset
  • AutoConnectWithStaticIP: configuration with static IP saved in the SPIFFS memory area
  • AutoConnectWithTimeout
  • OnDemandConfigPortal: Allows you to call the AP mode to configure a new connection using a physical control button connected to a pin on the ESP8266

Can we combine with other bookstores: pubsubclient, ArduinoOTA …?

Yes, the WiFiManager library is designed to be compatible with other libraries. Indeed, it is an abstraction layer at the ESP8266WiFi library. It only intervenes when the ESP8266 is started. However, as there is always a BUT, I have encountered some difficulties in connecting to an MQTT server. This is probably more of a bad implementation on my part than a real problem in the library. If you have more experience feel free to share in the comments.

To manage the connection to an MQTT server (broker) you can use this example available on GitHub. WiFiManager is able to integrate the input of additional connection parameters. On the other hand, it is necessary to manage either the registration and the loading of the parameters at the launch of the ESP8266 using the method setSaveConfigCallback.

It is also possible to upgrade wirelessly to Wi-Fi using the ArduinoOTA library. However, you must save and reload the connection parameters. After an update, the WiFiManager library tends to forget the previous connection parameters.