Skip to content

Commit 30daca7

Browse files
committed
fix reboot on webui wifi connect taking too long
1 parent bb5260f commit 30daca7

4 files changed

Lines changed: 42 additions & 22 deletions

File tree

src/board_driver.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ void BoardDriver::loadLedSettings() {
10011001
return;
10021002
}
10031003
Preferences prefs;
1004-
prefs.begin("ledSettings", true);
1004+
prefs.begin("ledSettings", false);
10051005
brightness = prefs.getUChar("brightness", BRIGHTNESS);
10061006
dimMultiplier = prefs.getUChar("dimMult", 70);
10071007
prefs.end();
@@ -1040,10 +1040,10 @@ void BoardDriver::loadHardwareConfig() {
10401040
if (!ChessUtils::ensureNvsInitialized()) return;
10411041

10421042
Preferences prefs;
1043-
prefs.begin("hwConfig", true);
1043+
prefs.begin("hwConfig", false);
10441044
if (!prefs.isKey("ver")) {
10451045
prefs.end();
1046-
Serial.println("No saved hardware config using compile-time defaults");
1046+
Serial.println("No saved hardware config, using compile-time defaults");
10471047
return;
10481048
}
10491049
hwConfig.ledPin = prefs.getUChar("ledPin", LED_PIN);

src/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ void setup() {
105105
}
106106

107107
void loop() {
108+
// Process deferred WiFi reconnection (from web UI)
109+
wifiManager.checkPendingWiFi();
110+
108111
// Check for pending board edits from WiFi (FEN-based)
109112
String editFen;
110113
if (wifiManager.getPendingBoardEdit(editFen)) {

src/wifi_manager_esp32.cpp

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
static const char* INITIAL_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
1212

13-
WiFiManagerESP32::WiFiManagerESP32(BoardDriver* bd, MoveHistory* mh) : boardDriver(bd), moveHistory(mh), server(AP_PORT), wifiSSID(SECRET_SSID), wifiPassword(SECRET_PASS), gameMode("0"), lichessToken(""), botConfig(), scanAllChannels(WIFI_SCAN_ALL_CHANNELS), currentFen(INITIAL_FEN), hasPendingEdit(false), boardEvaluation(0.0f), otaUpdater(bd), autoOtaEnabled(false) {}
13+
WiFiManagerESP32::WiFiManagerESP32(BoardDriver* bd, MoveHistory* mh) : boardDriver(bd), moveHistory(mh), server(AP_PORT), wifiSSID(SECRET_SSID), wifiPassword(SECRET_PASS), gameMode("0"), lichessToken(""), botConfig(), scanAllChannels(WIFI_SCAN_ALL_CHANNELS), currentFen(INITIAL_FEN), hasPendingEdit(false), hasPendingWiFi(false), boardEvaluation(0.0f), otaUpdater(bd), autoOtaEnabled(false) {}
1414

1515
void WiFiManagerESP32::begin() {
1616
Serial.println("=== Starting OpenChess WiFi Manager (ESP32) ===");
@@ -159,32 +159,23 @@ void WiFiManagerESP32::handleConnectWiFi(AsyncWebServerRequest* request) {
159159
prefs.end();
160160
scanAllChannels = newScanAll;
161161
Serial.printf("WiFi scan all channels: %s\n", scanAllChannels ? "enabled" : "disabled");
162-
request->send(200, "text/plain", "OK");
163162
changed = true;
164163
}
165164
}
166165
}
167166

168167
if (newWifiSSID.length() >= 1 && newWifiPassword.length() >= 5 && (newWifiSSID != wifiSSID || newWifiPassword != wifiPassword)) {
169-
if (!changed) {
170-
request->send(200, "text/plain", "OK");
171-
changed = true;
172-
}
173-
if (connectToWiFi(newWifiSSID, newWifiPassword, true)) {
174-
if (!ChessUtils::ensureNvsInitialized())
175-
Serial.println("NVS init failed - WiFi credentials not saved");
176-
prefs.begin("wifiCreds", false);
177-
prefs.putString("ssid", newWifiSSID);
178-
prefs.putString("pass", newWifiPassword);
179-
prefs.end();
180-
wifiSSID = newWifiSSID;
181-
wifiPassword = newWifiPassword;
182-
Serial.println("WiFi credentials updated and saved to NVS");
183-
}
184-
return;
168+
// Defer WiFi reconnection to the main loop to avoid blocking the async_tcp
169+
// task, which would trigger the ESP32 task watchdog (WDT).
170+
pendingWiFiSSID = newWifiSSID;
171+
pendingWiFiPassword = newWifiPassword;
172+
hasPendingWiFi = true;
173+
changed = true;
185174
}
186175

187-
if (!changed)
176+
if (changed)
177+
request->send(200, "text/plain", "OK");
178+
else
188179
request->send(400, "text/plain", "ERROR");
189180
}
190181

@@ -400,6 +391,25 @@ void WiFiManagerESP32::clearPendingEdit() {
400391
hasPendingEdit = false;
401392
}
402393

394+
void WiFiManagerESP32::checkPendingWiFi() {
395+
if (!hasPendingWiFi)
396+
return;
397+
hasPendingWiFi = false;
398+
String newSSID = pendingWiFiSSID;
399+
String newPass = pendingWiFiPassword;
400+
if (connectToWiFi(newSSID, newPass, true)) {
401+
if (!ChessUtils::ensureNvsInitialized())
402+
Serial.println("NVS init failed - WiFi credentials not saved");
403+
prefs.begin("wifiCreds", false);
404+
prefs.putString("ssid", newSSID);
405+
prefs.putString("pass", newPass);
406+
prefs.end();
407+
wifiSSID = newSSID;
408+
wifiPassword = newPass;
409+
Serial.println("WiFi credentials updated and saved to NVS");
410+
}
411+
}
412+
403413
bool WiFiManagerESP32::connectToWiFi(const String& ssid, const String& password, bool fromWeb) {
404414
if (!fromWeb && WiFi.status() == WL_CONNECTED) {
405415
Serial.println("Already connected to WiFi");

src/wifi_manager_esp32.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ class WiFiManagerESP32 {
5454
String pendingFenEdit;
5555
bool hasPendingEdit;
5656

57+
// Deferred WiFi reconnection (set by web handler, processed in main loop)
58+
String pendingWiFiSSID;
59+
String pendingWiFiPassword;
60+
volatile bool hasPendingWiFi;
61+
5762
// Web interface methods
5863
String getWiFiInfoJSON();
5964
String getBoardUpdateJSON();
@@ -117,6 +122,8 @@ class WiFiManagerESP32 {
117122
void clearPendingEdit();
118123
// WiFi connection management
119124
bool connectToWiFi(const String& ssid, const String& password, bool fromWeb = false);
125+
// Call from main loop to process deferred WiFi reconnection
126+
void checkPendingWiFi();
120127
};
121128

122129
#endif // WIFI_MANAGER_ESP32_H

0 commit comments

Comments
 (0)