|
10 | 10 |
|
11 | 11 | static const char* INITIAL_FEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; |
12 | 12 |
|
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) {} |
14 | 14 |
|
15 | 15 | void WiFiManagerESP32::begin() { |
16 | 16 | Serial.println("=== Starting OpenChess WiFi Manager (ESP32) ==="); |
@@ -159,32 +159,23 @@ void WiFiManagerESP32::handleConnectWiFi(AsyncWebServerRequest* request) { |
159 | 159 | prefs.end(); |
160 | 160 | scanAllChannels = newScanAll; |
161 | 161 | Serial.printf("WiFi scan all channels: %s\n", scanAllChannels ? "enabled" : "disabled"); |
162 | | - request->send(200, "text/plain", "OK"); |
163 | 162 | changed = true; |
164 | 163 | } |
165 | 164 | } |
166 | 165 | } |
167 | 166 |
|
168 | 167 | 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; |
185 | 174 | } |
186 | 175 |
|
187 | | - if (!changed) |
| 176 | + if (changed) |
| 177 | + request->send(200, "text/plain", "OK"); |
| 178 | + else |
188 | 179 | request->send(400, "text/plain", "ERROR"); |
189 | 180 | } |
190 | 181 |
|
@@ -400,6 +391,25 @@ void WiFiManagerESP32::clearPendingEdit() { |
400 | 391 | hasPendingEdit = false; |
401 | 392 | } |
402 | 393 |
|
| 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 | + |
403 | 413 | bool WiFiManagerESP32::connectToWiFi(const String& ssid, const String& password, bool fromWeb) { |
404 | 414 | if (!fromWeb && WiFi.status() == WL_CONNECTED) { |
405 | 415 | Serial.println("Already connected to WiFi"); |
|
0 commit comments