renamed some files, first steps with wifi settings post

This commit is contained in:
Marcus 2024-10-21 01:07:29 +02:00
parent e843df1a28
commit c2d6d508a2
6 changed files with 62 additions and 8 deletions

View file

@ -37,7 +37,7 @@
*/ */
#include "Webserver/Header.h" #include "Webserver/Header.h"
#include "Webserver/Footer.h" #include "Webserver/Footer.h"
#include "Webserver/AddHeaderFooter.h" #include "Webserver/Common.h"
#include "Webserver/Page_root.h" #include "Webserver/Page_root.h"
@ -52,7 +52,8 @@ LoggingMiddleware requestLogger;
/* /*
* 404 error page begins * 404 error page begins
*/ */
// 404 page is a good page template btw
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%)";
@ -83,6 +84,7 @@ void SetupWebserver() {
webserver.on("/", HTTP_GET, WebPage_root); webserver.on("/", HTTP_GET, WebPage_root);
webserver.on("/cangrow.css", HTTP_GET, WebFile_cangrow_CSS); webserver.on("/cangrow.css", HTTP_GET, WebFile_cangrow_CSS);
webserver.on("/wifiSettings", HTTP_GET, WebPage_wifi); webserver.on("/wifiSettings", HTTP_GET, WebPage_wifi);
webserver.on("/wifiSettings", HTTP_POST, WebPage_wifi);
requestLogger.setOutput(Serial); requestLogger.setOutput(Serial);
// this activates the middleware // this activates the middleware
@ -100,7 +102,7 @@ void SetupWebserver() {
// call the network scan once, so there are some values at the first call // 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 // of the wifi settings page. otherwise the first call of the wifi scan would return
// an empty list of networks // an empty list of networks
Serial.println(":: [Webserver] call [wifi:ScanNetworks] to workaround empty results bug"); Serial.println(":: [Webserver] call [wifi:ScanNetworks] to workaround empty scan results bug");
WebPage_wifi_ScanNetworks(); WebPage_wifi_ScanNetworks();
webserver.begin(); webserver.begin();

View file

@ -1,6 +1,6 @@
/* /*
* *
* include/Webserver/AddHeaderFooter.h - header file with functions to add * include/Webserver/Common.h - header file with common webserver functions
* HTML header or footer to a String() * HTML header or footer to a String()
* *
* *
@ -28,6 +28,7 @@
* *
*/ */
#include "Common_HTML.h"
bool TestHeaderFooter(const String& var) { bool TestHeaderFooter(const String& var) {
#ifdef DEBUG #ifdef DEBUG

View file

@ -0,0 +1,34 @@
/*
*
* include/Webserver/Common_HTML.h - header file with common HTML snippets
* HTML header or footer to a String()
*
*
* MIT License
*
* Copyright (c) 2024 DeltaLima
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
const char Common_HTML_SAVE_MSG[] PROGMEM = R"EOF(
<div class='infomsg'>&#x2705; Successfully saved!</div>
)EOF";

View file

@ -41,7 +41,7 @@ String WebPage_wifi_ScanNetworks() {
} else if(n){ } else if(n){
for (int i = 0; i < n; ++i){ for (int i = 0; i < n; ++i){
html += "<option value='" + WiFi.SSID(i) + "'>" + WiFi.SSID(i) + "</option>"; html += "<option value='" + WiFi.SSID(i) + "'>" + WiFi.SSID(i) + "</option>";
Serial.print(":: [Webserver:wifi:ScanNetworks] - "); Serial.print(":: [Webserver:wifi:ScanNetworks] - ");
Serial.println(WiFi.SSID(i)); Serial.println(WiFi.SSID(i));
} }
WiFi.scanDelete(); WiFi.scanDelete();
@ -82,6 +82,21 @@ String Proc_WebPage_wifi(const String& var) {
} }
} }
void WebPage_wifi(AsyncWebServerRequest *request) {
request->send_P(200, "text/html", Page_wifi_HTML, Proc_WebPage_wifi); String Proc_WebPage_wifi_POST(const String& var) {
if(var == "SAVE_MSG") {
return String(Common_HTML_SAVE_MSG);
} else {
return Proc_WebPage_wifi(var);
}
}
void WebPage_wifi(AsyncWebServerRequest *request) {
if(request->hasParam("configWifi.ssid", true)) {
const AsyncWebParameter* p = request->getParam("configWifi.ssid", true);
Serial.printf(":: [Webserver:wifi] POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
request->send_P(200, "text/html", Page_wifi_HTML, Proc_WebPage_wifi_POST);
} else {
request->send_P(200, "text/html", Page_wifi_HTML, Proc_WebPage_wifi);
}
} }

View file

@ -29,9 +29,10 @@
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>
%SAVE_MSG%
%CURRENT_SETTINGS% %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>Reload the page, if your network is not listed.</p>
<form method='post' action='/wifiSettings/save'> <form method='post' action='/wifiSettings/save'>
SSID: <select id='configWifi.ssid' name='configWifi.ssid' required> SSID: <select id='configWifi.ssid' name='configWifi.ssid' required>
<option disabled value='' selected hidden>-Select your network-</option> <option disabled value='' selected hidden>-Select your network-</option>
@ -42,6 +43,7 @@ IP: <input type='text' name='configWifi.ip'><br>
Subnet mask: <input type='text' name='configWifi.netmask'><br> Subnet mask: <input type='text' name='configWifi.netmask'><br>
Gateway: <input type='text' name='configWifi.gateway'><br> Gateway: <input type='text' name='configWifi.gateway'><br>
DNS: <input type='text' name='configWifi.dns'><br> DNS: <input type='text' name='configWifi.dns'><br>
DHCP: <input type='checkbox' name='configWifi.dhcp' value='1'><br>
<input type='submit' value='&#x1F4BE; Save settings'> <input type='submit' value='&#x1F4BE; Save settings'>
</form> </form>
%FOOTER%)"; %FOOTER%)";