How to Easily Manage Your WiFi Connections with WiFiManager and ESP8266

Are you tired of having to manually update your ESP8266’s WiFi credentials every time you move your project to a new location or switch to a different network? WiFiManager is here to help!

WiFiManager is a powerful library that allows you to easily manage your ESP8266’s WiFi connections, all without having to hard-code your WiFi credentials into your sketch. When installed on your ESP8266, WiFiManager sets the microcontroller to Access-Point mode, so that you can enter your WiFi credentials from a webpage served by WiFiManager itself. As soon as you input the correct credentials, WiFiManager will turn the Access-Point mode off, so that your ESP8266 will automatically connect to the WiFi.

This turns particularly helpful when you need to package your ESP8266 in an actual “standalone” product, since it’s neither useful nor practical to hard-code WiFi credentials on a PCB-soldered ESP8266 (e.g. ESP-12E/F on a circuit board).

The library

To use WiFiManager, you’ll need to install the library in your Arduino IDE. You can do this by going to Sketch > Include Library > Manage Libraries, and then searching for “WiFiManager” in the Library Manager:

Install WiFiManager

       

Alternatively, you can do it by either cloning the WiFiManager repository, or by downloading it as a ZIP file. Make sure to clone / extract it into the Arduino libraries folder.

Here’s where you can find the libraries location based on your OS:

The code

Getting started with WiFiManager is incredibly easy:

#include <WiFiManager.h>

void setup() {
  // Initialize serial communication
  Serial.begin(115200);

  // Connect to WiFi
  WiFiManager wifi_manager;
  wifi_manager.autoConnect("MyWiFiAP");

  // Print IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Your code here
}

As soon as you run the sketch for the first time, your ESP8266 will automatically turn into Access-Point mode. Try searching for WiFi networks using your phone or PC, you should now find one named “MyWiFiAP”. If you connect to it, you’ll be redirected automatically to the captive portal (hosted on 192.168.4.1), which looks like this:

WiFiManager Captive Portal

       

As you configure your WiFi credentials, the ESP8266 will automatically reset and try to connect to the WiFi. If credentials are correct, you’re good to go, otherwise it will turn the Access-Point mode back on. WiFiManager stores data in the ESP8266 flash, so they will persist between resets.

Let’s break it down

We first include the WiFiManager.h library (and the ESP8266WiFi.h library in order to print the IP address later):

#include <ESP8266WiFi.h>
#include <WiFiManager.h>

Start WiFiManager providing the name of the Access-Point:

WiFiManager wifi_manager;
wifi_manager.autoConnect("MyWiFiAP");

Print the IP address assigned to your ESP8266 after the WiFiManager configuration:

Serial.println(WiFi.localIP());

That’s it!

Configure Access-Point

You can protect the AP with a password by simply using:

wifi_manager.autoConnect("MyWiFiAP", "MyPassword");

If you don’t want to bother supplying an AP name either, you can leave it to WiFiManager as well:

wifi_manager.autoConnect();

This will automatically set the chip ID as the SSID for your Access-Point.

Wrappin’ up

WiFiManager is a versatile library that can be used in a wide range of ESP8266 projects. Since WiFi is a critical component of most IoT applications, the ability to easily manage WiFi connections is essential for many projects.

Whether you’re building a smart home device, a weather station, or a remote-controlled robot, chances are you’ll need to connect your ESP8266 to a WiFi network at some point. WiFiManager makes this process much simpler, allowing you to store your WiFi credentials in the ESP8266’s flash memory and easily update them as needed.

Overall, WiFiManager is a must-have library for anyone working with ESP8266 and WiFi. Its ease of use and versatility make it an essential tool for building robust and reliable IoT projects.


Made with ❤️ by Mikepicker