If you are working with the ESP8266 in your project, surely you had the same problem as we faced: what to do when you change the WiFi network to which your ESP8266 is to be connected. This is the main issue faced by many IOT device creators.
Normally we enter the SSID and the password of the WiFi network directly in the programming code, but it can be inconvenient if we transfer the project we are dealing with, or if the end user who is going to use it does not know anything about programming it.
In these cases, its very hard to open the box of the esp board or if it is fixed inside any box in home automation systems and connect it to laptop or PC and need to have an access to the previous code and upload it again into the ESP8266 board which is very stubborn task.
So, in this situation we have 2 solutions which we gonna explain below in detail with code and how to implement it in your previous programs. The first method is by using the code which we provided below and the other is by using an Arduino library named ‘WiFi Manager’. So lets get started.
Table of Contents
Required Components
Here are the required components along with Amazon buying links at best prices.Product Name | Quantity | ![]() | ![]() |
---|---|---|---|
ESP8266 NodeMCU | 1 | https://amzn.to/3tqMwkB | https://amzn.to/3sVdSjG |
Micro USB cable | 1 | https://amzn.to/3s1a8g3 | https://amzn.to/364yInH |
Method 1: Code inside sketch. Storing the WiFi credentials inside EEPROM
In this method, we are going to use EEPROM to store the wife credentials inside it, and retrieving the stored data and connecting to that network if available. If the WiFi network is not available it ill create a WiFi hotspot to which we need to connect any device like laptop or mobile to change the WiFi credentials over web server.
To do so we need to get into setting by typing 192.168.4.1 on your device browser to access the setting page, where we can see the available networks and there signal strengths. So enter the SSID and PASSWORD of the network which you wanted to connect and click save.
That it the network credentials are written into the EEPROM of the board and it will restart and search for the network which was stored in the EEPROM and connect to the network and continue executing our program.
Required Libraries:
- ESP8266WiFi.h
- ESP8266HTTPClient.h
- ESP8266WebServer.h
- EEPROM.h
Method1 Code:
#include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> #include <ESP8266WebServer.h> #include <EEPROM.h> //Variables int i = 0; int statusCode; const char* ssid = "text"; const char* passphrase = "text"; String st; String content; //Function Declaration bool testWifi(void); void launchWeb(void); void setupAP(void); //--------Establishing Local server at port 80 whenever required ESP8266WebServer server(80); void setup() { Serial.begin(115200); //Initializing if(DEBUG)Serial Monitor Serial.println(); Serial.println("Disconnecting previously connected WiFi"); WiFi.disconnect(); EEPROM.begin(512); //Initializing EEPROM delay(10); pinMode(LED_BUILTIN, OUTPUT); Serial.println(); Serial.println(); Serial.println("Startup"); // Read eeprom for ssid and password Serial.println("Reading EEPROM ssid"); String esid; for (int i = 0; i < 32; ++i) { esid += char(EEPROM.read(i)); } Serial.println(); Serial.print("SSID: "); Serial.println(esid); Serial.println("Reading EEPROM pass"); String epass = ""; for (int i = 32; i < 96; ++i) { epass += char(EEPROM.read(i)); } Serial.print("PASS: "); Serial.println(epass); WiFi.begin(esid.c_str(), epass.c_str()); if (testWifi()) { Serial.println("Succesfully Connected!!!"); return; } else { Serial.println("Turning the HotSpot On"); launchWeb(); setupAP();// Setup accesspoint or HotSpot } Serial.println(); Serial.println("Waiting."); while ((WiFi.status() != WL_CONNECTED)) { Serial.print("."); delay(100); server.handleClient(); } } void loop() { if ((WiFi.status() == WL_CONNECTED)) { // Add your program code here which the esp8266 has to perform when it connects to network } else { } } //Functions used for saving WiFi credentials and to connect to it which you do not need to change bool testWifi(void) { int c = 0; Serial.println("Waiting for WiFi to connect"); while ( c < 20 ) { if (WiFi.status() == WL_CONNECTED) { return true; } delay(500); Serial.print("*"); c++; } Serial.println(""); Serial.println("Connection timed out, opening AP or Hotspot"); return false; } void launchWeb() { Serial.println(""); if (WiFi.status() == WL_CONNECTED) Serial.println("WiFi connected"); Serial.print("Local IP: "); Serial.println(WiFi.localIP()); Serial.print("SoftAP IP: "); Serial.println(WiFi.softAPIP()); createWebServer(); // Start the server server.begin(); Serial.println("Server started"); } void setupAP(void) { WiFi.mode(WIFI_STA); WiFi.disconnect(); delay(100); int n = WiFi.scanNetworks(); Serial.println("scan completed"); if (n == 0) Serial.println("No WiFi Networks found"); else { Serial.print(n); Serial.println(" Networks found"); for (int i = 0; i < n; ++i) { // Print SSID and RSSI for each network found Serial.print(i + 1); Serial.print(": "); Serial.print(WiFi.SSID(i)); Serial.print(" ("); Serial.print(WiFi.RSSI(i)); Serial.print(")"); Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*"); delay(10); } } Serial.println(""); st = "<ol>"; for (int i = 0; i < n; ++i) { // Print SSID and RSSI for each network found st += "<li>"; st += WiFi.SSID(i); st += " ("; st += WiFi.RSSI(i); st += ")"; st += (WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*"; st += "</li>"; } st += "</ol>"; delay(100); WiFi.softAP("ESP-NAME", ""); Serial.println("Initializing_Wifi_accesspoint"); launchWeb(); Serial.println("over"); } void createWebServer() { { server.on("/", []() { IPAddress ip = WiFi.softAPIP(); String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]); content = "<!DOCTYPE HTML>\r\n<html>ESP8266 WiFi Connectivity Setup "; content += "<form action=\"/scan\" method=\"POST\"><input type=\"submit\" value=\"scan\"></form>"; content += ipStr; content += "<p>"; content += st; content += "</p><form method='get' action='setting'><label>SSID: </label><input name='ssid' length=32><input name='pass' length=64><input type='submit'></form>"; content += "</html>"; server.send(200, "text/html", content); }); server.on("/scan", []() { //setupAP(); IPAddress ip = WiFi.softAPIP(); String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]); content = "<!DOCTYPE HTML>\r\n<html>go back"; server.send(200, "text/html", content); }); server.on("/setting", []() { String qsid = server.arg("ssid"); String qpass = server.arg("pass"); if (qsid.length() > 0 && qpass.length() > 0) { Serial.println("clearing eeprom"); for (int i = 0; i < 96; ++i) { EEPROM.write(i, 0); } Serial.println(qsid); Serial.println(""); Serial.println(qpass); Serial.println(""); Serial.println("writing eeprom ssid:"); for (int i = 0; i < qsid.length(); ++i) { EEPROM.write(i, qsid[i]); Serial.print("Wrote: "); Serial.println(qsid[i]); } Serial.println("writing eeprom pass:"); for (int i = 0; i < qpass.length(); ++i) { EEPROM.write(32 + i, qpass[i]); Serial.print("Wrote: "); Serial.println(qpass[i]); } EEPROM.commit(); content = "{\"Success\":\"saved to eeprom... reset to boot into new wifi\"}"; statusCode = 200; ESP.reset(); } else { content = "{\"Error\":\"404 not found\"}"; statusCode = 404; Serial.println("Sending 404"); } server.sendHeader("Access-Control-Allow-Origin", "*"); server.send(statusCode, "application/json", content); }); } }
In the above code you need to add the main work code which the esp8266 has to perform after connecting to network If you want to test the above code insert the below code for blinking the LED on board by adding blow code inside the above code by replacing “// Add your program code here which the esp8266 has to perform when it connects to network”.
//---- Program to blink on board LED for (int i = 0; i < 10; i++) { digitalWrite(LED_BUILTIN, HIGH); delay(1000); digitalWrite(LED_BUILTIN, LOW); delay(1000); }
Method2: Using the WiFi manager library from Arduino IDE
What is WiFi Manager?
The Wi-Fi Manager library for Arduino is a popular choice. It allows you to create a captive portal on the ESP8266, enabling users to configure Wi-Fi settings via a web interface. This way, users can easily change the network without modifying the code.
This method is very simple than the previous one, in this you just need to download a library from arduino IDE by following the steps below. To download directly and install manually click here.
Step1: In Arduino IDE got to Sketch/Include Library/Manage Libraries

Step2: search for WiFiManager and install it.

include the library in the sketch or code. by adding
WiFiManager.h
- To Initialize WiFi manger library, in your setup function add
WiFiManager wifiManager;
- Also in the setup function add
wifiManager.autoConnect("AP-NAME", "AP-PASSWORD");
The first parameter specifies the name of the access point or hotspot to be created, while the second parameter is the password. You can leave the password field empty if you prefer an open network.
After uploading the code, you will discover a WiFi network, just like in the previous method. Connect to this network, and it will automatically redirect you to the webserver’s homepage. There, you’ll find options to connect to available WiFi networks. The page is displayed as shown in the image below.
That’s it! You are now successfully connected to the new WiFi network, and your ESP8266 is ready to operate on this network. This method operates similarly to the previous one by storing WiFi credentials in the EEPROM.
If you have any questions or encounter any issues with the code above, please don’t hesitate to leave a comment below. If you found this tutorial helpful, consider sharing it with your friends.