fix(wifi): skip SDK-cached credentials when SSID is already configured (closes #3)
WiFi.begin() without params connects to any SDK-cached network, including open public hotspots, and then overwrites config.ssid with the wrong SSID. Now Try 1 (SDK credentials) is only attempted on first boot when no SSID is configured. Once the user has saved credentials, we go straight to Try 2 (EEPROM config), so the correct network is always used. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
c606bcc589
commit
09b8680408
@@ -67,37 +67,42 @@ void ICACHE_FLASH_ATTR setupWiFi() {
|
|||||||
WiFi.hostname(config.hostname);
|
WiFi.hostname(config.hostname);
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
|
|
||||||
// Try 1: Use WiFi.begin() without params - uses SDK stored credentials
|
// Try 1: Use WiFi.begin() without params - only when no SSID is configured.
|
||||||
Serial.println("Trying SDK-stored credentials...");
|
// Skipped if user has a saved SSID: SDK-cached credentials may include open
|
||||||
WiFi.begin();
|
// networks (e.g. public hotspots) that would be preferred over the user's
|
||||||
|
// network, and a successful connect would overwrite config.ssid (issue #3).
|
||||||
|
if (strlen(config.ssid) == 0) {
|
||||||
|
Serial.println("No SSID configured, trying SDK-stored credentials...");
|
||||||
|
WiFi.begin();
|
||||||
|
|
||||||
// SYNCHRONOUS wait for connection (max 10 seconds)
|
// SYNCHRONOUS wait for connection (max 10 seconds)
|
||||||
Serial.print("Connecting to WiFi");
|
Serial.print("Connecting to WiFi");
|
||||||
int attempts = 0;
|
int attempts = 0;
|
||||||
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
|
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
|
||||||
showWiFiConnecting(attempts);
|
showWiFiConnecting(attempts);
|
||||||
delay(500);
|
delay(500);
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
attempts++;
|
attempts++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (WiFi.status() == WL_CONNECTED) {
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
Serial.println("\nWiFi connected!");
|
Serial.println("\nWiFi connected!");
|
||||||
Serial.print("SSID: ");
|
Serial.print("SSID: ");
|
||||||
Serial.println(WiFi.SSID());
|
Serial.println(WiFi.SSID());
|
||||||
Serial.print("IP: ");
|
Serial.print("IP: ");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
Serial.print("Gateway: ");
|
Serial.print("Gateway: ");
|
||||||
Serial.println(WiFi.gatewayIP());
|
Serial.println(WiFi.gatewayIP());
|
||||||
Serial.print("DNS: ");
|
Serial.print("DNS: ");
|
||||||
Serial.println(WiFi.dnsIP());
|
Serial.println(WiFi.dnsIP());
|
||||||
|
|
||||||
safeStringCopy(WiFi.SSID(), config.ssid, sizeof(config.ssid));
|
safeStringCopy(WiFi.SSID(), config.ssid, sizeof(config.ssid));
|
||||||
saveConfig();
|
saveConfig();
|
||||||
|
|
||||||
showIP();
|
showIP();
|
||||||
wifiConnState = WIFI_CONN_CONNECTED;
|
wifiConnState = WIFI_CONN_CONNECTED;
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try 2: If we have EEPROM credentials, try those
|
// Try 2: If we have EEPROM credentials, try those
|
||||||
@@ -105,7 +110,7 @@ void ICACHE_FLASH_ATTR setupWiFi() {
|
|||||||
Serial.println("\nTrying EEPROM credentials...");
|
Serial.println("\nTrying EEPROM credentials...");
|
||||||
WiFi.begin(config.ssid, config.password);
|
WiFi.begin(config.ssid, config.password);
|
||||||
|
|
||||||
attempts = 0;
|
int attempts = 0;
|
||||||
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
|
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
|
||||||
showWiFiConnecting(attempts);
|
showWiFiConnecting(attempts);
|
||||||
delay(500);
|
delay(500);
|
||||||
|
|||||||
+33
-32
@@ -761,41 +761,42 @@ void ICACHE_FLASH_ATTR setupWiFi() {
|
|||||||
// STRATEGY: First try SDK-stored credentials (from WiFiManager), then EEPROM config
|
// STRATEGY: First try SDK-stored credentials (from WiFiManager), then EEPROM config
|
||||||
// WiFiManager stores credentials in ESP flash separately from our EEPROM
|
// WiFiManager stores credentials in ESP flash separately from our EEPROM
|
||||||
|
|
||||||
// Try 1: Use WiFi.begin() without params - uses SDK stored credentials
|
// Try 1: Use WiFi.begin() without params - only when no SSID is configured.
|
||||||
Serial.println("Trying SDK-stored credentials...");
|
// Skipped if user has a saved SSID: SDK-cached credentials may include open
|
||||||
WiFi.begin(); // Uses credentials stored by WiFiManager/SDK
|
// networks (e.g. public hotspots) that would be preferred over the user's
|
||||||
|
// network, and a successful connect would overwrite config.ssid (issue #3).
|
||||||
|
if (strlen(config.ssid) == 0) {
|
||||||
|
Serial.println("No SSID configured, trying SDK-stored credentials...");
|
||||||
|
WiFi.begin();
|
||||||
|
|
||||||
// SYNCHRONOUS wait for connection (max 10 seconds)
|
// SYNCHRONOUS wait for connection (max 10 seconds)
|
||||||
Serial.print("Connecting to WiFi");
|
Serial.print("Connecting to WiFi");
|
||||||
int attempts = 0;
|
int attempts = 0;
|
||||||
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
|
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
|
||||||
delay(500);
|
delay(500);
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
showNumber(attempts, false);
|
showNumber(attempts, false);
|
||||||
attempts++;
|
attempts++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (WiFi.status() == WL_CONNECTED) {
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
Serial.println("\n✅ WiFi connected!");
|
Serial.println("\n✅ WiFi connected!");
|
||||||
Serial.print("SSID: ");
|
Serial.print("SSID: ");
|
||||||
Serial.println(WiFi.SSID());
|
Serial.println(WiFi.SSID());
|
||||||
Serial.print("IP: ");
|
Serial.print("IP: ");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
Serial.print("Gateway: ");
|
Serial.print("Gateway: ");
|
||||||
Serial.println(WiFi.gatewayIP());
|
Serial.println(WiFi.gatewayIP());
|
||||||
Serial.print("DNS: ");
|
Serial.print("DNS: ");
|
||||||
Serial.println(WiFi.dnsIP());
|
Serial.println(WiFi.dnsIP());
|
||||||
|
|
||||||
// Sync connected SSID to our config
|
safeStringCopy(WiFi.SSID(), config.ssid, sizeof(config.ssid));
|
||||||
safeStringCopy(WiFi.SSID(), config.ssid, sizeof(config.ssid));
|
saveConfig();
|
||||||
// Note: password stays in SDK storage, we don't have access to it
|
|
||||||
saveConfig();
|
|
||||||
|
|
||||||
// Show IP on display
|
showIP();
|
||||||
showIP();
|
wifiConnState = WIFI_CONN_CONNECTED;
|
||||||
|
return;
|
||||||
wifiConnState = WIFI_CONN_CONNECTED;
|
}
|
||||||
return; // Success!
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try 2: If we have EEPROM credentials, try those
|
// Try 2: If we have EEPROM credentials, try those
|
||||||
@@ -803,7 +804,7 @@ void ICACHE_FLASH_ATTR setupWiFi() {
|
|||||||
Serial.println("\nTrying EEPROM credentials...");
|
Serial.println("\nTrying EEPROM credentials...");
|
||||||
WiFi.begin(config.ssid, config.password);
|
WiFi.begin(config.ssid, config.password);
|
||||||
|
|
||||||
attempts = 0;
|
int attempts = 0;
|
||||||
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
|
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
|
||||||
delay(500);
|
delay(500);
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
|
|||||||
Reference in New Issue
Block a user