From c2d6d508a2e538bc315cdd02895832f318eb0a8a Mon Sep 17 00:00:00 2001 From: Marcus Date: Mon, 21 Oct 2024 01:07:29 +0200 Subject: [PATCH] renamed some files, first steps with wifi settings post --- Arduino/CanGrow/include/CanGrow_Webserver.h | 8 +++-- Arduino/CanGrow/include/Webserver/.h | 0 .../Webserver/{AddHeaderFooter.h => Common.h} | 3 +- .../CanGrow/include/Webserver/Common_HTML.h | 34 +++++++++++++++++++ Arduino/CanGrow/include/Webserver/Page_wifi.h | 21 ++++++++++-- .../include/Webserver/Page_wifi_HTML.h | 4 ++- 6 files changed, 62 insertions(+), 8 deletions(-) delete mode 100644 Arduino/CanGrow/include/Webserver/.h rename Arduino/CanGrow/include/Webserver/{AddHeaderFooter.h => Common.h} (95%) create mode 100644 Arduino/CanGrow/include/Webserver/Common_HTML.h diff --git a/Arduino/CanGrow/include/CanGrow_Webserver.h b/Arduino/CanGrow/include/CanGrow_Webserver.h index 06260bf..54fe7d9 100644 --- a/Arduino/CanGrow/include/CanGrow_Webserver.h +++ b/Arduino/CanGrow/include/CanGrow_Webserver.h @@ -37,7 +37,7 @@ */ #include "Webserver/Header.h" #include "Webserver/Footer.h" -#include "Webserver/AddHeaderFooter.h" +#include "Webserver/Common.h" #include "Webserver/Page_root.h" @@ -52,7 +52,8 @@ LoggingMiddleware requestLogger; /* * 404 error page begins */ - + +// 404 page is a good page template btw const char* Page_404_HTML PROGMEM = R"(%HEADER%

❗ ️ 404 - not found

%FOOTER%)"; @@ -83,6 +84,7 @@ void SetupWebserver() { webserver.on("/", HTTP_GET, WebPage_root); webserver.on("/cangrow.css", HTTP_GET, WebFile_cangrow_CSS); webserver.on("/wifiSettings", HTTP_GET, WebPage_wifi); + webserver.on("/wifiSettings", HTTP_POST, WebPage_wifi); requestLogger.setOutput(Serial); // this activates the middleware @@ -100,7 +102,7 @@ void SetupWebserver() { // 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"); + Serial.println(":: [Webserver] call [wifi:ScanNetworks] to workaround empty scan results bug"); WebPage_wifi_ScanNetworks(); webserver.begin(); diff --git a/Arduino/CanGrow/include/Webserver/.h b/Arduino/CanGrow/include/Webserver/.h deleted file mode 100644 index e69de29..0000000 diff --git a/Arduino/CanGrow/include/Webserver/AddHeaderFooter.h b/Arduino/CanGrow/include/Webserver/Common.h similarity index 95% rename from Arduino/CanGrow/include/Webserver/AddHeaderFooter.h rename to Arduino/CanGrow/include/Webserver/Common.h index 4ab116a..a8ca683 100644 --- a/Arduino/CanGrow/include/Webserver/AddHeaderFooter.h +++ b/Arduino/CanGrow/include/Webserver/Common.h @@ -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() * * @@ -28,6 +28,7 @@ * */ +#include "Common_HTML.h" bool TestHeaderFooter(const String& var) { #ifdef DEBUG diff --git a/Arduino/CanGrow/include/Webserver/Common_HTML.h b/Arduino/CanGrow/include/Webserver/Common_HTML.h new file mode 100644 index 0000000..64f6aaf --- /dev/null +++ b/Arduino/CanGrow/include/Webserver/Common_HTML.h @@ -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( +
✅ Successfully saved!
+)EOF"; diff --git a/Arduino/CanGrow/include/Webserver/Page_wifi.h b/Arduino/CanGrow/include/Webserver/Page_wifi.h index c068dcd..334032b 100644 --- a/Arduino/CanGrow/include/Webserver/Page_wifi.h +++ b/Arduino/CanGrow/include/Webserver/Page_wifi.h @@ -41,7 +41,7 @@ String WebPage_wifi_ScanNetworks() { } else if(n){ for (int i = 0; i < n; ++i){ html += ""; - Serial.print(":: [Webserver:wifi:ScanNetworks] - "); + Serial.print(":: [Webserver:wifi:ScanNetworks] - "); Serial.println(WiFi.SSID(i)); } 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); + } } diff --git a/Arduino/CanGrow/include/Webserver/Page_wifi_HTML.h b/Arduino/CanGrow/include/Webserver/Page_wifi_HTML.h index 1be0ee9..7430627 100644 --- a/Arduino/CanGrow/include/Webserver/Page_wifi_HTML.h +++ b/Arduino/CanGrow/include/Webserver/Page_wifi_HTML.h @@ -29,9 +29,10 @@ const char* Page_wifi_HTML PROGMEM = R"(%HEADER%

📡 WiFi settings

+%SAVE_MSG% %CURRENT_SETTINGS%

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

+
Reload the page, if your network is not listed.

SSID:
Subnet mask:
Gateway:
DNS:
+DHCP:
%FOOTER%)";