After the EEPROM is successfully loaded with the new SSID/Password, I need to manually power-cycle the device in order for the new settings to work. If I just do ESP.restart(); , the ESP8266 never connects. Am I missing something in my code to be able to change WiFi networks without manually power-cycling the device?
Please find attached the relevant code. The Setup function loads a web page to gather the information from a user, then the confirmPage function loads the values into EEPROM. The ESP8266 then resets itself using the safeRestart function, which then goes into the Setup function again. If I allow the ESP8266 to restart itself, it will never go past the timeout. It I restart the ESP8266 manually by pulling the power cable out the back in, the ESP8266 connects perfectly every time.
void safeRestart() {
//delay(1000);
//WiFi.mode(WIFI_OFF);
//delay(1000);
DEBUG_PRINTLN("Restarting...");
clearPanel();
panel.show();
yield();
ESP.restart();
yield();
//ESP.deepSleep(1000000, RF_DISABLED);
}
void confirmPage() {
DEBUG_PRINTLN("Got Post");
String passString;
String ssidString;
String eepromString;
if (server.arg("ssid").length() > 32) {
errorPage(0);
return;
}
else ssidString = server.arg("ssid");
if (server.arg("pass").length() > 32) {
errorPage(1);
return;
}
else passString = server.arg("pass");
String s = headerHtml;
s += "Thanks! I'm now going to connect to ";
s += ssidString;
if (passString != "") s += " using the password you have given me";
s += ".";
eepromString = "<ssid>";
eepromString += ssidString;
eepromString += "<pass>";
eepromString += passString;
eepromString += "<end>";
DEBUG_PRINTLN("Writing EEPROM");
EEPROM.write(0, 42);
for (int i = 0; i < (eepromString.length()); i++) EEPROM.write(i + 1, eepromString.charAt(i));
EEPROM.write(eepromString.length() + 2, 42);
EEPROM.commit();
DEBUG_PRINT("EEPROM Written: ");
for (int i = 0; i < (eepromString.length() + 1); i++) DEBUG_PRINT(char(EEPROM.read(i)));
DEBUG_PRINTLN("");
server.send(200, "text/html", s);
DEBUG_PRINTLN("Client disconnected");
yield();
safeRestart();
}
void setup() {
Serial.begin(115200);
DEBUG_PRINTLN("Serial Online");
EEPROM.begin(256);
DEBUG_PRINTLN("EEPROM Online");
String ssidString = "";
String passString = "";
String storedRequestString = "";
DEBUG_PRINTLN("Strings Initialized");
DEBUG_PRINT("EEPROM(0) = ");
DEBUG_PRINTLN(EEPROM.read(0));
if (EEPROM.read(0) != 42) {
uint8_t mac[WL_MAC_ADDR_LENGTH];
WiFi.softAPmacAddress(mac);
DEBUG_PRINT("WiFi MAC : ");
DEBUG_PRINTLN(WiFi.softAPmacAddress());
server.begin();
DEBUG_PRINTLN("Server Started");
server.on("/", HTTP_POST, confirmPage);
server.on("/", setupPage);
DEBUG_PRINTLN("Server Pages Set, Waiting for Clients");
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(myIp, myGateway, mySubnet);
DEBUG_PRINTLN("WiFi AP Online");
EEPROM.write(0, 0);
EEPROM.commit();
DEBUG_PRINTLN("Low, Clearing EEPROM");
DEBUG_PRINTLN("EEPROM(0) != 42, EEPROM invalid");
for (int i = 0; i < 256; ++i) EEPROM.write(i, 0);
DEBUG_PRINTLN("EEPROM cleared");
String macId = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) + String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
macId.toUpperCase();
String apString = "Setup " + macId;
WiFi.softAP(apString.c_str());
DEBUG_PRINT("WiFi SSID Set : ");
DEBUG_PRINTLN(apString);
while (1){server.handleClient(); yield();}
}
DEBUG_PRINTLN("EEPROM(0) = 42, using saved settings");
for (int i = 0; i < 256; i++) storedRequestString += char(EEPROM.read(i));
DEBUG_PRINT("EEPROM: ");
DEBUG_PRINTLN(storedRequestString);
int passIndex = storedRequestString.indexOf("<pass>");
int endIndex = storedRequestString.indexOf("<end>");
ssidString = storedRequestString.substring(7, passIndex);
passString = storedRequestString.substring(passIndex + 6, endIndex);
WiFi.mode(WIFI_STA);
WiFi.begin(ssidString.c_str(), passString.c_str());
DEBUG_PRINT("Connecting to ");
DEBUG_PRINTLN(ssidString);
DEBUG_PRINT("Password ");
DEBUG_PRINT(passString);
int timeout = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
yield();
DEBUG_PRINT(".");
timeout++;
if (timeout > 60) {
DEBUG_PRINTLN("Timeout Reached");
EEPROM.write(0, 0);
EEPROM.commit();
DEBUG_PRINTLN("EEPROM Reset");
safeRestart();
}
}
DEBUG_PRINTLN("WiFi connected");
DEBUG_PRINTLN("IP address: ");
DEBUG_PRINTLN(WiFi.localIP());
}