https://github.com/khoih-prog/ESP_WiFiManager
How To Install Using Arduino Library Manager
This is an ESP32 / ESP8266 WiFi Connection Manager with fallback web configuration portal. Use this library for configuring ESP32, ESP8266 modules’ WiFi, etc. Credentials at runtime. You can also specify static DNS servers, personalized HostName. With examples supporting ArduinoJson 6.0.0+ as well as 5.13.5- .
This library is based on, modified, bug-fixed and improved from:
to add support to ESP32 besides ESP8266.
So, how it works?
In Configuration Portal Mode, it starts an access point called ESP_XXXXXX. Connect to it using the configurable password you can define in the code. For example, your_password (see examples):
// SSID and PW for Config Portal
String ssid = "ESP_" + String(ESP_getChipId(), HEX);
const char* password = "your_password";
After you connected, please, go to http://192.168.4.1, you’ll see this Main page:
Select Information to enter the Info page where the board info will be shown (long page)
or short page (default)
Select Configuration to enter this page where you can select an AP and specify its WiFi Credentials
Enter your credentials, then click Save. The WiFi Credentials will be saved and the board reboots to connect to the selected WiFi AP.
If you’re already connected to a listed WiFi AP and don’t want to change anything, just select Exit Portal from the Main page to reboot the board and connect to the previously-stored AP. The WiFi Credentials are still intact.
Sample Code
//Ported to ESP32
#ifdef ESP32
#include <esp_wifi.h>
#include <WiFi.h>
#include <WiFiClient.h>
#define ESP_getChipId() ((uint32_t)ESP.getEfuseMac())
#define LED_ON HIGH
#define LED_OFF LOW
#else
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#define ESP_getChipId() (ESP.getChipId())
#define LED_ON LOW
#define LED_OFF HIGH
#endif
#include <ESP_WiFiManager.h> //https://github.com/khoih-prog/ESP_WiFiManager
// SSID and PW for your Router
String Router_SSID;
String Router_Pass;
void heartBeatPrint(void)
{
static int num = 1;
if (WiFi.status() == WL_CONNECTED)
Serial.print("H"); // H means connected to WiFi
else
Serial.print("F"); // F means not connected to WiFi
if (num == 80)
{
Serial.println();
num = 1;
}
else if (num++ % 10 == 0)
{
Serial.print(" ");
}
}
void check_status()
{
static ulong checkstatus_timeout = 0;
#define HEARTBEAT_INTERVAL 10000L
// Print hearbeat every HEARTBEAT_INTERVAL (10) seconds.
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
{
heartBeatPrint();
checkstatus_timeout = millis() + HEARTBEAT_INTERVAL;
}
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("\nStarting AutoConnectAP");
// Use this to default DHCP hostname to ESP8266-XXXXXX or ESP32-XXXXXX
//ESP_WiFiManager ESP_wifiManager;
// Use this to personalize DHCP hostname (RFC952 conformed)
ESP_WiFiManager ESP_wifiManager("AutoConnectAP");
ESP_wifiManager.setDebugOutput(true);
//set custom ip for portal
ESP_wifiManager.setAPStaticIPConfig(IPAddress(192, 168, 100, 1), IPAddress(192, 168, 100, 1), IPAddress(255, 255, 255, 0));
ESP_wifiManager.setMinimumSignalQuality(-1);
// Set static IP, Gateway, Subnetmask, DNS1 and DNS2. New in v1.0.5+
ESP_wifiManager.setSTAStaticIPConfig(IPAddress(192, 168, 2, 114), IPAddress(192, 168, 2, 1), IPAddress(255, 255, 255, 0),
IPAddress(192, 168, 2, 1), IPAddress(8, 8, 8, 8));
// We can't use WiFi.SSID() in ESP32 as it's only valid after connected.
// SSID and Password stored in ESP32 wifi_ap_record_t and wifi_config_t are also cleared in reboot
// Have to create a new function to store in EEPROM/SPIFFS for this purpose
Router_SSID = ESP_wifiManager.WiFi_SSID();
Router_Pass = ESP_wifiManager.WiFi_Pass();
//Remove this line if you do not want to see WiFi password printed
Serial.println("Stored: SSID = " + Router_SSID + ", Pass = " + Router_Pass);
if (Router_SSID != "")
{
ESP_wifiManager.setConfigPortalTimeout(60); //If no access point name has been previously entered disable timeout.
Serial.println("Got stored Credentials. Timeout 60s");
}
else
{
Serial.println("No stored Credentials. No timeout");
}
String chipID = String(ESP_getChipId(), HEX);
chipID.toUpperCase();
// SSID and PW for Config Portal
String AP_SSID = "ESP_" + chipID + "_AutoConnectAP";
String AP_PASS = "MyESP_" + chipID;
// Get Router SSID and PASS from EEPROM, then open Config portal AP named "ESP_XXXXXX_AutoConnectAP" and PW "MyESP_XXXXXX"
// 1) If got stored Credentials, Config portal timeout is 60s
// 2) If no stored Credentials, stay in Config portal until get WiFi Credentials
ESP_wifiManager.autoConnect(AP_SSID.c_str(), AP_PASS.c_str());
//or use this for Config portal AP named "ESP_XXXXXX" and NULL password
//ESP_wifiManager.autoConnect();
//if you get here you have connected to the WiFi
Serial.println("WiFi connected");
}
void loop()
{
// put your main code here, to run repeatedly
check_status();
}