How to Using the WiFiManager library with ESP8266

Why need Using the WiFiManager library with ESP8266. If you want to design an IoT module as a commercial product and you went sell it. you will require to let the user configure the Wi-Fi credentials themselves as long as the MQTT server, port, username, and password that will be used by the user.

All the extra data needs to be stored on the SPIFFS filesystem so if the module will begin will be used to connect to Wi-Fi and to connect to the MQTT server.

In the first part of this tutorial, we will explain how to make a web server to get Wi-Fi network credentials with the help of the WiFiManager library, save them to the SPIFFS file, and in the second part.


≡ Required Component :


Here is a list of the hardware we suggest to Using the WiFiManager library with ESP8266.

  1. NodeMcu8266
  2. Breadboard
  3. Connecting wire
  4. Breadboard Power Supply Module

≡ Using the WiFiManager library Code


#include "FS.h"

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

boolean init_esp = false;

void setup() 
{
  Serial.begin(115200); delay(10);
  
  if(init_esp)
  {
    SPIFFS.format();
    WiFi.disconnect();
 }
  
  WiFiManager wifiManager;
  
  wifiManager.setConfigPortalTimeout(240);
  
  if (!wifiManager.autoConnect("ESP_AP", "password")) 
  {
    Serial.println(F("Failed to connect. Reset and try again..."));
    delay(3000);
    //reset and try again
    ESP.reset();
    delay(5000);
  }

  //if you get here you have connected to the WiFi
  Serial.println(F("Connected to Wifi."));
  Serial.print(F("My IP: "));
  Serial.println(WiFi.localIP());
}

void loop() {
  //add your code for loop()
}

≡ Code Overview WiFiManager library: 


Use the following Library to see for WiFiManager:

#include "FS.h"
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>

In fact, you require to clear the previously cached values in the SPIFFS you can set init_esp to true to clean the before-stored Wi-Fi credentials and to format the SPIFFS. This can be the problem when you want to move the ESP8266 module to another WiFi network. Next, you flash the code the setup() function will format the SPIFFS. Change then the value after the false so you will not format the flash every time when the ESP8266 is starting:

boolean init_esp = false;
void setup()
{
 Serial.begin(115200); delay(10);
 if(init_esp)
 {
 SPIFFS.format();
 WiFi.disconnect();
 }

Instantiate the Wi-Fi manager object This will begin the ESP in the Access Point mode and the captive portal that will redirect you to a configuration web page:

WiFiManager wifiManager;

Set the closing timeout to 240 seconds:

wifiManager.setConfigPortalTimeout(240);
if (!wifiManager.autoConnect("ESP_AP", "1234"))
{
Serial.println(F("Failed to connect. Reset and try again..."));
delay(3000);
//reset and try again
ESP.reset();
delay(5000);
}

At this time, we  attached now to the Wi-Fi network, after we selected the Wi-Fi network and password from a web browser:

 //if you get here you have connected to the WiFi
 Serial.println(F("Connected to Wifi."));
 Serial.print(F("My IP: "));
 Serial.println(WiFi.localIP());
}
void loop() {
 //add your code for loop()
}

After you select and flash the ESP8266 with this code, look for surrounding Wi-Fi networks. You will see a network named ESP_AP. Connect to it, enter the password, and click on Sign in to the network. If you don’t see this message, go
To your web browser and try to access any link or enter 192.168.1.1 in the address bar. You will  redirected to this page.

Using WiFiManager library ESP8266

On this page, you will see the access point name that was set, ESP_AP, and four buttons, which explained in the following list. Configure Wi-Fi: Scan surrounding Wi-Fi networks nearby and it will show you a list of them together with their signal power. If you have many Wi-Fi networks you can pick the one with the most power, as shown in the next screenshot

Using WiFiManager library ESP8266

Info: As shown in the next screenshot, this page will read the Chip ID, MAC address for the access point or Station Mode of the ESP8266, and the flash details such as flash size and flash ID:

Using WiFiManager library ESP8266

Visit to get more tutorials on IoT project

INTERNET OF THINGS

 337 total views,  3 views today