diff --git a/lib/WiFiManager/include/WiFiManager.h b/lib/WiFiManager/include/WiFiManager.h index b797c60..381fe80 100644 --- a/lib/WiFiManager/include/WiFiManager.h +++ b/lib/WiFiManager/include/WiFiManager.h @@ -776,8 +776,6 @@ protected: String WiFi_psk(bool persistent = true) const; bool WiFi_scanNetworks(); bool WiFi_scanNetworks(bool force); // Always async - returns false if scan started but not complete - bool WiFi_scanNetworks(bool force,bool async); // async parameter ignored - always async - bool WiFi_scanNetworks(unsigned int cachetime,bool async); // async parameter ignored - always async bool WiFi_scanNetworks(unsigned int cachetime); void WiFi_scanComplete(int networksFound); bool WiFiSetCountry(); diff --git a/lib/WiFiManager/include/templates/WiFiPollingJS.h b/lib/WiFiManager/include/templates/WiFiPollingJS.h new file mode 100644 index 0000000..f761772 --- /dev/null +++ b/lib/WiFiManager/include/templates/WiFiPollingJS.h @@ -0,0 +1,117 @@ +/** + * WiFiPollingJS.h + * JavaScript code for WiFi network scanning with AJAX polling + * This is embedded in the WiFi configuration page for async WiFi scanning + */ + +#ifndef _WIFI_POLLING_JS_H_ +#define _WIFI_POLLING_JS_H_ + +#include + +const char PROGMEM WIFI_POLLING_JS[] = R"rawliteral( + +)rawliteral"; + +#endif // _WIFI_POLLING_JS_H_ + diff --git a/lib/WiFiManager/include/wm_consts_en.h b/lib/WiFiManager/include/wm_consts_en.h index cf703fb..d728709 100644 --- a/lib/WiFiManager/include/wm_consts_en.h +++ b/lib/WiFiManager/include/wm_consts_en.h @@ -59,7 +59,7 @@ const char R_exit[] PROGMEM = "/exit"; const char R_close[] PROGMEM = "/close"; const char R_erase[] PROGMEM = "/erase"; const char R_status[] PROGMEM = "/status"; -const char R_scanstatus[] PROGMEM = "/wifi/scanstatus"; +const char R_scanstatus[] PROGMEM = "/wifistatus"; const char R_update[] PROGMEM = "/update"; const char R_updatedone[] PROGMEM = "/u"; diff --git a/lib/WiFiManager/include/wm_strings_en.h b/lib/WiFiManager/include/wm_strings_en.h index da6bfac..edc5ee6 100644 --- a/lib/WiFiManager/include/wm_strings_en.h +++ b/lib/WiFiManager/include/wm_strings_en.h @@ -31,10 +31,13 @@ const char HTTP_HEAD_START[] PROGMEM = "" const char HTTP_SCRIPT[] PROGMEM = ""; // @todo add button states, disable on click , show ack , spinner etc const char HTTP_HEAD_END[] PROGMEM = "
"; // {c} = _bodyclass diff --git a/lib/WiFiManager/src/WiFiManager.cpp b/lib/WiFiManager/src/WiFiManager.cpp index 000f4b8..8232760 100644 --- a/lib/WiFiManager/src/WiFiManager.cpp +++ b/lib/WiFiManager/src/WiFiManager.cpp @@ -11,6 +11,7 @@ */ #include "WiFiManager.h" +#include "templates/WiFiPollingJS.h" #if defined(ESP8266) || defined(ESP32) @@ -1381,7 +1382,14 @@ void WiFiManager::handleRoot(AsyncWebServerRequest *request) { void WiFiManager::handleWifi(AsyncWebServerRequest *request, boolean scan) { #ifdef WM_DEBUG_LEVEL DEBUG_WM(WM_DEBUG_VERBOSE, F("<- HTTP Wifi")); + DEBUG_WM(WM_DEBUG_DEV, F("handleWifi called, scan="), scan ? "true" : "false"); #endif + if (captivePortal(request)) { + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(WM_DEBUG_DEV, F("Captive portal redirect")); + #endif + return; // If captive portal redirect instead of displaying the page + } handleRequest(request); String page = getHTTPHead(FPSTR(S_titlewifi), FPSTR(C_wifi)); // @token titlewifi if (scan) { @@ -1440,112 +1448,29 @@ void WiFiManager::handleWifi(AsyncWebServerRequest *request, boolean scan) { if(_showBack) page += FPSTR(HTTP_BACKBTN); reportStatus(page); - // Add JavaScript for AJAX polling - page += F(""); + // TODO: Known issue - Adding WIFI_POLLING_JS causes silent failure (empty response) + // This is likely due to: + // 1. String size limit: Page is already ~9-10KB, adding ~2.5KB JS may exceed ESPAsyncWebServer limits + // 2. Memory fragmentation: Large String append operations on ESP8266 can cause heap fragmentation + // 3. PROGMEM access: FPSTR() on large PROGMEM strings may have timing issues with async operations + // 4. Response size: ESPAsyncWebServer may silently fail or truncate responses exceeding ~12KB + // Workaround: Temporarily disabled until we can optimize the page size or use chunked responses + // page += FPSTR(WIFI_POLLING_JS); page += getHTTPEnd(); + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(WM_DEBUG_DEV, F("Page length: "), String(page.length())); + DEBUG_WM(WM_DEBUG_DEV, F("_numNetworks: "), String(_numNetworks)); + DEBUG_WM(WM_DEBUG_DEV, F("_scanInProgress: "), _scanInProgress ? "true" : "false"); + DEBUG_WM(WM_DEBUG_DEV, F("_lastscan: "), String(_lastscan)); + DEBUG_WM(WM_DEBUG_DEV, F("About to send response")); + #endif + request->send(200, FPSTR(HTTP_HEAD_CT), page); #ifdef WM_DEBUG_LEVEL - DEBUG_WM(WM_DEBUG_DEV, F("Sent config page")); + DEBUG_WM(WM_DEBUG_DEV, F("Response sent")); #endif } @@ -1615,17 +1540,9 @@ bool WiFiManager::WiFi_scanNetworks(){ return WiFi_scanNetworks(false); } -bool WiFiManager::WiFi_scanNetworks(unsigned int cachetime,bool async){ - // async parameter ignored - always async now - return WiFi_scanNetworks(millis()-_lastscan > cachetime); -} bool WiFiManager::WiFi_scanNetworks(unsigned int cachetime){ return WiFi_scanNetworks(millis()-_lastscan > cachetime); } -bool WiFiManager::WiFi_scanNetworks(bool force,bool async){ - // async parameter ignored - always async now - return WiFi_scanNetworks(force); -} bool WiFiManager::WiFi_scanNetworks(bool force){ #ifdef WM_DEBUG_LEVEL // DEBUG_WM(WM_DEBUG_DEV,_numNetworks,(millis()-_lastscan )); @@ -2742,8 +2659,21 @@ boolean WiFiManager::captivePortal(AsyncWebServerRequest *request) { #ifdef WM_DEBUG_LEVEL DEBUG_WM(WM_DEBUG_VERBOSE, F("<- Request redirected to captive portal")); DEBUG_WM(WM_DEBUG_DEV, "serverLoc " + serverLoc); + DEBUG_WM(WM_DEBUG_DEV, "Original URL " + request->url()); #endif - request->redirect((String)F("http://") + serverLoc); + // Preserve the original path in the redirect + String redirectUrl = (String)F("http://") + serverLoc + request->url(); + if (request->params() > 0) { + redirectUrl += F("?"); + for (size_t i = 0; i < request->params(); i++) { + if (i > 0) redirectUrl += F("&"); + redirectUrl += request->getParam(i)->name() + F("=") + request->getParam(i)->value(); + } + } + #ifdef WM_DEBUG_LEVEL + DEBUG_WM(WM_DEBUG_DEV, "Redirect URL " + redirectUrl); + #endif + request->redirect(redirectUrl); return true; } return false;