From e843df1a285a8e16bf7e3611bdc11b9fc6fc6dd1 Mon Sep 17 00:00:00 2001 From: Marcus Date: Sun, 20 Oct 2024 23:09:20 +0200 Subject: [PATCH] added wifi network scan --- Arduino/CanGrow/include/CanGrow_Webserver.h | 24 ++++++++++++-- Arduino/CanGrow/include/Webserver/Page_wifi.h | 32 +++++++++++++++++-- .../include/Webserver/Page_wifi_HTML.h | 4 +-- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/Arduino/CanGrow/include/CanGrow_Webserver.h b/Arduino/CanGrow/include/CanGrow_Webserver.h index f5f57cb..06260bf 100644 --- a/Arduino/CanGrow/include/CanGrow_Webserver.h +++ b/Arduino/CanGrow/include/CanGrow_Webserver.h @@ -44,14 +44,15 @@ #include "Webserver/Page_wifi.h" - - AsyncWebServer webserver(80); // log incoming requests LoggingMiddleware requestLogger; -/* 404 error site - this is also a good example for a simple WebPage */ +/* + * 404 error page begins + */ + const char* Page_404_HTML PROGMEM = R"(%HEADER%

❗ ️ 404 - not found

%FOOTER%)"; @@ -66,6 +67,12 @@ void WebserverNotFound(AsyncWebServerRequest* request) { request->send(404, "text/html", Page_404_HTML, Proc_WebPage_404); } +/* + * 404 error page ends + */ + + + /* * setup all the webhandlers */ @@ -88,7 +95,18 @@ void SetupWebserver() { webserver.onNotFound(WebserverNotFound); + // Workaround, see comment at + // https://github.com/mathieucarbou/ESPAsyncWebServer/blob/main/docs/index.md#scanning-for-available-wifi-networks + // call the network scan once, so there are some values at the first call + // of the wifi settings page. otherwise the first call of the wifi scan would return + // an empty list of networks + Serial.println(":: [Webserver] call [wifi:ScanNetworks] to workaround empty results bug"); + WebPage_wifi_ScanNetworks(); + webserver.begin(); Serial.println(":: [Webserver] ready to serve"); + + + } diff --git a/Arduino/CanGrow/include/Webserver/Page_wifi.h b/Arduino/CanGrow/include/Webserver/Page_wifi.h index 682b708..c068dcd 100644 --- a/Arduino/CanGrow/include/Webserver/Page_wifi.h +++ b/Arduino/CanGrow/include/Webserver/Page_wifi.h @@ -30,13 +30,39 @@ #include "Page_wifi_HTML.h" - +String WebPage_wifi_ScanNetworks() { + String html; + + Serial.println(":: [Webserver:wifi:ScanNetworks] scanning for available networks:"); + // https://github.com/mathieucarbou/ESPAsyncWebServer/blob/main/docs/index.md#scanning-for-available-wifi-networks + int n = WiFi.scanComplete(); + if(n == -2){ + WiFi.scanNetworks(true); + } else if(n){ + for (int i = 0; i < n; ++i){ + html += ""; + Serial.print(":: [Webserver:wifi:ScanNetworks] - "); + Serial.println(WiFi.SSID(i)); + } + WiFi.scanDelete(); + if(WiFi.scanComplete() == -2){ + WiFi.scanNetworks(true); + } + } + return html; +} // https://techtutorialsx.com/2018/07/23/esp32-arduino-http-server-template-processing-with-multiple-placeholders/ String Proc_WebPage_wifi(const String& var) { if(TestHeaderFooter(var)) { return AddHeaderFooter(var); - //SNIP_CURRSETTINGS + //CURRENT_SETTINGS + } else if(var == "CURRENT_SETTINGS") { + if(strlen(configWifi.ssid) > 0) { + return String(Page_wifi_HTML_CURRENT_SETTINGS); + } else { + return String(); + } } else if(var == "CONFIGWIFI_SSID") { return String(configWifi.ssid); } else if(var == "CONFIGWIFI_DHCP") { @@ -50,7 +76,7 @@ String Proc_WebPage_wifi(const String& var) { } else if(var == "CONFIGWIFI_DNS") { return String(WiFi.dnsIP().toString()); } else if(var == "WIFI_LIST") { - return String(""); + return String(WebPage_wifi_ScanNetworks()); } else { return String(); } diff --git a/Arduino/CanGrow/include/Webserver/Page_wifi_HTML.h b/Arduino/CanGrow/include/Webserver/Page_wifi_HTML.h index 2923b12..1be0ee9 100644 --- a/Arduino/CanGrow/include/Webserver/Page_wifi_HTML.h +++ b/Arduino/CanGrow/include/Webserver/Page_wifi_HTML.h @@ -29,7 +29,7 @@ const char* Page_wifi_HTML PROGMEM = R"(%HEADER%

📡 WiFi settings

-%SNIP_CURRSETTINGS% +%CURRENT_SETTINGS%

Select your wifi network from the SSID list.
To use DHCP leave IP, Subnet, Gateway and DNS fields blank!

@@ -46,7 +46,7 @@ DNS:
%FOOTER%)"; -const char* Page_wifi_HTML_curSettings PROGMEM = R"(Current Settings:
WiFi SSID: %CONFIGWIFI_SSID%
+const char* Page_wifi_HTML_CURRENT_SETTINGS PROGMEM = R"(Current Settings:
WiFi SSID: %CONFIGWIFI_SSID%
Use DHCP: %CONFIGWIFI_DHCP%
IP address: %CONFIGWIFI_IP%
Subnet mask: %CONFIGWIFI_NETMASK%