Quick answer: install WiFiManager by tzapu (v2.x) from the Library Manager, then replace WiFi.begin(ssid, password) with WiFiManager wm; wm.autoConnect("ESP32-Setup", "setup1234");. On first boot (or when the saved network fails) the ESP32 creates a Wi-Fi hotspot called ESP32-Setup. Join it from your phone and a setup page opens (or browse to 192.168.4.1). Pick your network, type the password and save. The ESP32 stores the credentials in flash and reconnects automatically from then on. Add setConfigPortalTimeout(180) so a power cut never leaves it stuck in setup mode, and WiFiManagerParameter fields to collect extra settings such as an MQTT server.
| Function | What it does |
|---|---|
autoConnect(name, pass) | Connect with saved credentials, or open the portal if that fails |
setConnectTimeout(s) | How long to try the saved network |
setConfigPortalTimeout(s) | How long the portal stays open before giving up |
startConfigPortal(name, pass) | Open the portal on demand (e.g. a button) |
resetSettings() | Erase saved Wi-Fi credentials |
addParameter(¶m) | Add a custom field (MQTT server, device name…) |
setConfigPortalBlocking(false) + process() | Keep your code running while the portal is open |
Every ESP32 tutorial starts with const char* ssid = "MyWiFi";. That is fine on your desk and a disaster as soon as the device leaves it. Give the board to a friend, move house, change your router password or build ten units for different customers, and every one needs the firmware edited and re-flashed over USB.
WiFiManager fixes this. On first boot, or whenever it cannot join a saved network, the ESP32 starts its own access point. You connect to it from a phone, a setup page opens automatically (a captive portal), you pick your home network from a list, type the password, and the ESP32 saves it and joins. Next boot it reconnects on its own. The same portal can also collect other settings, such as an MQTT server address or a device name, so one firmware image works for everybody.
This guide sets it up end to end: basic use, a timeout and fallback, custom fields saved to flash, a button to reopen the portal, a non-blocking mode, a static IP and the problems people actually hit.
How WiFiManager Works
- On boot, the library checks the credentials stored in the ESP32's flash memory.
- If it can connect, your program continues normally.
- If there are no credentials or the connection fails, it starts an access point (for example
ESP32-Setup) and a tiny web server with DNS redirection. - Your phone joins that access point and is sent to the portal page, usually automatically. If not, you open
192.168.4.1. - You choose a network, enter the password and press save. The ESP32 stores the credentials, restarts the WiFi stack and joins your network.
The credentials are saved by the ESP32 WiFi system in non-volatile storage, so they survive power loss. This is the same access point mode described in the WiFi tutorial, wrapped in a polished setup flow.
Install the Library
In the Arduino IDE open Sketch → Include Library → Manage Libraries, search for WiFiManager, and install the one by tzapu (version 2.x supports the ESP32). If the installed version is a very old 0.x release, update it, because the ESP32 support lives in the newer one.
The Simplest Possible Use
#include <WiFi.h>
#include <WiFiManager.h>
void setup() {
Serial.begin(115200);
WiFiManager wm;
bool connected = wm.autoConnect("ESP32-Setup", "setup1234");
if (!connected) {
Serial.println("Failed to connect, restarting");
ESP.restart();
}
Serial.println("WiFi connected");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}
void loop() {
}autoConnect(name, password) does everything. The first argument is the access point name you will see on your phone, and the second is its password (at least 8 characters). Leave the password out to create an open access point, which is easier but means anyone nearby can open your setup page, so a password is better.
First run: open WiFi settings on your phone, join ESP32-Setup, and wait for the portal to appear. If it does not pop up on its own, browse to http://192.168.4.1. Tap Configure WiFi, select your network, enter the password and save. The Serial Monitor shows the connection and the new IP address.
Second run: reset the board. It reconnects with the saved details and never opens the portal.
Note that autoConnect() blocks until it connects or gives up, which brings us to timeouts.
Timeouts and Fallback
By default the portal stays open forever. That is bad for a device that should keep working after a power cut while the router is still booting: it would wait in setup mode instead of retrying. Set timeouts:
WiFiManager wm;
wm.setConnectTimeout(20); // seconds to try a saved network
wm.setConfigPortalTimeout(180); // seconds the portal stays open
if (!wm.autoConnect("ESP32-Setup", "setup1234")) {
Serial.println("No WiFi. Restarting to try again...");
delay(3000);
ESP.restart();
}With these settings the ESP32 tries the saved network for 20 seconds, then opens the portal for three minutes, and if nobody configures it, restarts and tries the saved network again. This loop is exactly what you want after a power outage: the ESP32 boots faster than the router, fails once, restarts and connects when the router is ready.
If your project can do something useful offline (log to an SD card, run a local display), continue running instead of restarting, and retry WiFi later in the background.
Know when the portal opens
A callback lets you show a message on a display or light an LED:
void onConfigMode(WiFiManager* wm) {
Serial.println("Entered config mode");
Serial.print("Connect to: ");
Serial.println(wm->getConfigPortalSSID());
Serial.print("Portal IP: ");
Serial.println(WiFi.softAPIP());
}
wm.setAPCallback(onConfigMode);Custom Fields: Collect MQTT Settings and More
The portal can ask for extra values. This is how you build one firmware that works with any broker address.
#include <WiFi.h>
#include <WiFiManager.h>
#include <Preferences.h>
Preferences prefs;
bool shouldSave = false;
char mqttServer[40] = "192.168.1.10";
char mqttPort[6] = "1883";
char deviceName[24] = "esp32-node";
void saveConfigCallback() {
shouldSave = true;
}
void setup() {
Serial.begin(115200);
// load saved values
prefs.begin("config", false);
strlcpy(mqttServer, prefs.getString("server", mqttServer).c_str(), sizeof(mqttServer));
strlcpy(mqttPort, prefs.getString("port", mqttPort).c_str(), sizeof(mqttPort));
strlcpy(deviceName, prefs.getString("name", deviceName).c_str(), sizeof(deviceName));
WiFiManagerParameter pServer("server", "MQTT server", mqttServer, 40);
WiFiManagerParameter pPort("port", "MQTT port", mqttPort, 6);
WiFiManagerParameter pName("name", "Device name", deviceName, 24);
WiFiManager wm;
wm.setSaveConfigCallback(saveConfigCallback);
wm.addParameter(&pServer);
wm.addParameter(&pPort);
wm.addParameter(&pName);
wm.setConfigPortalTimeout(180);
if (!wm.autoConnect("ESP32-Setup", "setup1234")) {
ESP.restart();
}
strlcpy(mqttServer, pServer.getValue(), sizeof(mqttServer));
strlcpy(mqttPort, pPort.getValue(), sizeof(mqttPort));
strlcpy(deviceName, pName.getValue(), sizeof(deviceName));
if (shouldSave) {
prefs.putString("server", mqttServer);
prefs.putString("port", mqttPort);
prefs.putString("name", deviceName);
Serial.println("Config saved");
}
Serial.printf("Server: %s:%s Name: %s\n", mqttServer, mqttPort, deviceName);
}
void loop() {
}How it works: the three WiFiManagerParameter objects become extra text boxes on the portal page, pre-filled with the current values. When you save, the callback sets a flag, and after autoConnect() returns, the sketch copies the new values out of the parameters and writes them to flash with Preferences. On the next boot, they are read back first and used as the defaults.
Use Preferences (or LittleFS) rather than EEPROM for this: it is simpler and designed for key-value settings. The stored values connect directly to projects like the MQTT temperature sensor and the smart switch, where the broker address no longer needs to be compiled in.
A Button to Reopen the Portal
If someone changes their router or moves the device, they need a way to re-enter setup without re-flashing. The BOOT button (GPIO 0) is handy: hold it for three seconds to open the portal on demand.
#include <WiFi.h>
#include <WiFiManager.h>
const int RESET_BUTTON = 0; // BOOT button, LOW when pressed
unsigned long pressStart = 0;
void startPortal() {
WiFiManager wm;
wm.setConfigPortalTimeout(180);
if (!wm.startConfigPortal("ESP32-Setup", "setup1234")) {
Serial.println("Portal timed out");
}
}
void checkResetButton() {
if (digitalRead(RESET_BUTTON) == LOW) {
if (pressStart == 0) pressStart = millis();
if (millis() - pressStart > 3000) {
Serial.println("Button held: opening config portal");
startPortal();
pressStart = 0;
}
} else {
pressStart = 0;
}
}
void setup() {
Serial.begin(115200);
pinMode(RESET_BUTTON, INPUT_PULLUP);
WiFiManager wm;
wm.autoConnect("ESP32-Setup", "setup1234");
}
void loop() {
checkResetButton();
// your normal code
}To wipe saved credentials completely (for example before giving the board away), call wm.resetSettings(); once. You can wire that to a longer button hold, such as ten seconds.
On your own boards, use a separate button on a safe pin such as GPIO 27 instead of GPIO 0, so a stuck bootloader button can never trigger it by accident (see the ESP32 pinout guide).
Non-Blocking Mode
autoConnect() freezes your program while the portal is open. If the device should keep doing its job (reading sensors, driving outputs) while waiting to be configured, use non-blocking mode:
WiFiManager wm;
void setup() {
Serial.begin(115200);
wm.setConfigPortalBlocking(false);
wm.setConfigPortalTimeout(0); // keep portal available, never time out
if (wm.autoConnect("ESP32-Setup", "setup1234")) {
Serial.println("Connected");
} else {
Serial.println("Portal running in background");
}
}
void loop() {
wm.process(); // must be called often to serve the portal
// your normal code keeps running here
}process() serves the portal when it is open and does nothing otherwise, so it is cheap to leave in the loop.
Static IP Through the Portal
Combine WiFiManager with a fixed address using setSTAStaticIPConfig, before autoConnect():
wm.setSTAStaticIPConfig(IPAddress(192,168,1,184),
IPAddress(192,168,1,1),
IPAddress(255,255,255,0));This hardcodes the network layout again, which defeats part of the purpose, so on distributed devices a router-side DHCP reservation is usually better. The static IP guide explains the trade-off in detail.
Security Notes
- Put a password on the setup access point (8+ characters). An open portal lets anyone in range replace your Wi-Fi credentials.
- Set a portal timeout. A portal that closes after a few minutes is far less exposed than one that stays up forever.
- The saved Wi-Fi password is stored unencrypted in flash by default. Anyone with physical access and a USB cable can read it, which is equally true of hard-coded credentials. Flash encryption is available for products that need it.
- Don't leave
resetSettings()insetup(). It's handy for testing, but if you forget it there the board wipes its credentials on every boot. - Give shared devices a guest network, not your main Wi-Fi password.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Portal page doesn't pop up on the phone | Captive-portal detection is slow or hidden | Browse to http://192.168.4.1; on Android tap "stay connected" when it warns there's no internet |
| ESP32-Setup network doesn't appear | Board isn't in config mode yet, or phone scanning 5 GHz only | Check the Serial Monitor; the ESP32 hotspot is 2.4 GHz only |
| Home network missing from the list | Router broadcasts 5 GHz only, or the network is hidden | Enable 2.4 GHz on the router; type a hidden SSID manually |
| Keeps going back into setup mode | Wrong saved password, weak signal, or router slower to boot than the ESP32 | Raise setConnectTimeout(), re-enter credentials, use the timeout-and-restart loop |
| Stuck in setup after every power cut | No portal timeout | setConfigPortalTimeout(180) then ESP.restart() |
WiFiManager.h not found, or WebServer conflicts | Wrong or duplicate library (ESP8266-only fork) | Install tzapu's WiFiManager 2.x; remove duplicates from the libraries folder |
| Custom parameter values are empty | Read before autoConnect() returned, or saved when the callback didn't fire | Use getValue() after autoConnect(); save only when shouldSave is true |
| Credentials gone after re-flashing | "Erase All Flash Before Sketch Upload" was enabled | Set it to Disabled in the Arduino IDE Tools menu |
| Credentials gone on every boot | resetSettings() left in the code | Remove it, or call it only on a long button press |
| Portal opens but saving fails to connect | Password typo or WPA3-only router | Re-type carefully; enable WPA2/WPA3 mixed mode on the router |
For deeper connection issues, use the Wi-Fi troubleshooting guide.
Alternatives and Frequently Asked Questions
Are there other ways to provision WiFi? Yes. Espressif's BLE provisioning and SoftAP provisioning with a phone app, and Improv WiFi (used by ESPHome) let you send credentials over Bluetooth or USB. ESPHome includes a fallback hotspot and captive portal out of the box, see Home Assistant without MQTT.
Does WiFiManager work on ESP8266 too? Yes, it started there. The same code mostly works on both, which is useful if you also build with NodeMCU boards.
Can I theme the portal? Yes. You can change the title, add a custom menu and inject your own CSS. Check the library examples.
Does it slow down boot? Only when it has to open the portal. A normal boot with saved credentials connects as fast as plain WiFi.begin().
Is it suitable for products I sell? It is a good starting point for hobby and small-batch devices. For commercial products, consider a provisioning scheme with a secure per-device setup password and a proper factory-reset procedure.
Where are the credentials stored? In the ESP32's NVS (non-volatile storage) partition, managed by the Wi-Fi driver. They survive power loss and normal sketch uploads.
Can I use WiFiManager with ESPAsyncWebServer? Yes, but use the async-compatible fork (for example ESPAsyncWiFiManager), or close WiFiManager's portal before starting your own server. Two web servers on port 80 at once will conflict.
What should I build with it? Anything that leaves your desk: the MQTT sensor, the smart switch, or an ESP32-CAM streamer.




