added wifi network scan

This commit is contained in:
Marcus 2024-10-20 23:09:20 +02:00
parent 979b214d43
commit e843df1a28
3 changed files with 52 additions and 8 deletions

View file

@ -44,14 +44,15 @@
#include "Webserver/Page_wifi.h" #include "Webserver/Page_wifi.h"
AsyncWebServer webserver(80); AsyncWebServer webserver(80);
// log incoming requests // log incoming requests
LoggingMiddleware requestLogger; 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% const char* Page_404_HTML PROGMEM = R"(%HEADER%
<div class='warnmsg'><h1>&#10071; &#65039; 404 - not found</h1></div> <div class='warnmsg'><h1>&#10071; &#65039; 404 - not found</h1></div>
%FOOTER%)"; %FOOTER%)";
@ -66,6 +67,12 @@ void WebserverNotFound(AsyncWebServerRequest* request) {
request->send(404, "text/html", Page_404_HTML, Proc_WebPage_404); request->send(404, "text/html", Page_404_HTML, Proc_WebPage_404);
} }
/*
* 404 error page ends
*/
/* /*
* setup all the webhandlers * setup all the webhandlers
*/ */
@ -88,7 +95,18 @@ void SetupWebserver() {
webserver.onNotFound(WebserverNotFound); 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(); webserver.begin();
Serial.println(":: [Webserver] ready to serve"); Serial.println(":: [Webserver] ready to serve");
} }

View file

@ -30,13 +30,39 @@
#include "Page_wifi_HTML.h" #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 += "<option value='" + WiFi.SSID(i) + "'>" + WiFi.SSID(i) + "</option>";
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/ // https://techtutorialsx.com/2018/07/23/esp32-arduino-http-server-template-processing-with-multiple-placeholders/
String Proc_WebPage_wifi(const String& var) { String Proc_WebPage_wifi(const String& var) {
if(TestHeaderFooter(var)) { if(TestHeaderFooter(var)) {
return AddHeaderFooter(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") { } else if(var == "CONFIGWIFI_SSID") {
return String(configWifi.ssid); return String(configWifi.ssid);
} else if(var == "CONFIGWIFI_DHCP") { } else if(var == "CONFIGWIFI_DHCP") {
@ -50,7 +76,7 @@ String Proc_WebPage_wifi(const String& var) {
} else if(var == "CONFIGWIFI_DNS") { } else if(var == "CONFIGWIFI_DNS") {
return String(WiFi.dnsIP().toString()); return String(WiFi.dnsIP().toString());
} else if(var == "WIFI_LIST") { } else if(var == "WIFI_LIST") {
return String(""); return String(WebPage_wifi_ScanNetworks());
} else { } else {
return String(); return String();
} }

View file

@ -29,7 +29,7 @@
const char* Page_wifi_HTML PROGMEM = R"(%HEADER% const char* Page_wifi_HTML PROGMEM = R"(%HEADER%
<h2>&#128225; WiFi settings</h2> <h2>&#128225; WiFi settings</h2>
%SNIP_CURRSETTINGS% %CURRENT_SETTINGS%
<p>Select your wifi network from the SSID list. <p>Select your wifi network from the SSID list.
<br>To use DHCP leave IP, Subnet, Gateway and DNS fields blank!</p> <br>To use DHCP leave IP, Subnet, Gateway and DNS fields blank!</p>
<form method='post' action='/wifiSettings/save'> <form method='post' action='/wifiSettings/save'>
@ -46,7 +46,7 @@ DNS: <input type='text' name='configWifi.dns'><br>
</form> </form>
%FOOTER%)"; %FOOTER%)";
const char* Page_wifi_HTML_curSettings PROGMEM = R"(<u>Current Settings:</u><br>WiFi SSID: <b>%CONFIGWIFI_SSID%</b><br> const char* Page_wifi_HTML_CURRENT_SETTINGS PROGMEM = R"(<u>Current Settings:</u><br>WiFi SSID: <b>%CONFIGWIFI_SSID%</b><br>
Use DHCP: <b>%CONFIGWIFI_DHCP%</b><br> Use DHCP: <b>%CONFIGWIFI_DHCP%</b><br>
IP address: <b>%CONFIGWIFI_IP%</b><br> IP address: <b>%CONFIGWIFI_IP%</b><br>
Subnet mask: <b>%CONFIGWIFI_NETMASK%</b><br> Subnet mask: <b>%CONFIGWIFI_NETMASK%</b><br>