Skip to content

Commit 8f5171c

Browse files
committed
Added ability to call StartConfigPortal() so that access point name can be unique and automatically generated from the ESP8266 Chip Id.
Tries to reconnect in station mode with whatever credentials have been saved when StartConfigPortal times out otherwise device will be without WiFi after exiting StartConfigPortal.
1 parent 566bcbe commit 8f5171c

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

WiFiManager.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) {
149149
return startConfigPortal(apName, apPassword);
150150
}
151151

152+
boolean WiFiManager::startConfigPortal() {
153+
String ssid = "ESP" + String(ESP.getChipId());
154+
return startConfigPortal(ssid.c_str());
155+
}
156+
152157
boolean WiFiManager::startConfigPortal(char const *apName, char const *apPassword) {
153158
//setup AP
154159
WiFi.mode(WIFI_AP_STA);
@@ -203,7 +208,13 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo
203208
}
204209
yield();
205210
}
206-
211+
//Timed out so go back to station mode using system-stored ssid and pass
212+
if (connectWifi(WiFi.SSID().c_str(),WiFi.psk().c_str()) != WL_CONNECTED) {
213+
DEBUG_WM(F("Failed to connect after configuration mode timeout."));
214+
} else {
215+
//connected
216+
WiFi.mode(WIFI_STA);
217+
}
207218
server.reset();
208219
dnsServer.reset();
209220

@@ -235,7 +246,7 @@ int WiFiManager::connectWifi(String ssid, String pass) {
235246
ETS_UART_INTR_DISABLE();
236247
wifi_station_disconnect();
237248
ETS_UART_INTR_ENABLE();
238-
249+
239250
WiFi.begin();
240251
} else {
241252
DEBUG_WM("No saved credentials");

WiFiManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class WiFiManager
7474
boolean autoConnect(char const *apName, char const *apPassword = NULL);
7575

7676
//if you want to always start the config portal, without trying to connect first
77+
boolean startConfigPortal();
7778
boolean startConfigPortal(char const *apName, char const *apPassword = NULL);
7879

7980
// get the AP name of the config portal, so it can be used in the callback

0 commit comments

Comments
 (0)