From de26abaf058bc07b5feca70a2f39df9b6cdee996 Mon Sep 17 00:00:00 2001 From: Marcus Date: Mon, 21 Oct 2024 22:16:20 +0200 Subject: [PATCH] added system settings page, added web restart dialoge --- Arduino/CanGrow/CanGrow.ino | 2 +- Arduino/CanGrow/include/CanGrow.h | 7 +- Arduino/CanGrow/include/CanGrow_LittleFS.h | 6 +- Arduino/CanGrow/include/CanGrow_Webserver.h | 16 ++-- Arduino/CanGrow/include/CanGrow_Wifi.h | 25 +++--- Arduino/CanGrow/include/Webserver/Common.h | 19 ++-- .../CanGrow/include/Webserver/Common_HTML.h | 8 ++ Arduino/CanGrow/include/Webserver/Header.h | 9 +- .../CanGrow/include/Webserver/Page_settings.h | 30 ------- .../include/Webserver/Page_settings_HTML.h | 28 ------ .../CanGrow/include/Webserver/Page_system.h | 79 ++++++++++++++++ .../include/Webserver/Page_system_HTML.h | 89 +++++++++++++++++++ Arduino/CanGrow/include/Webserver/Page_wifi.h | 2 + .../include/Webserver/Page_wifi_HTML.h | 4 +- 14 files changed, 232 insertions(+), 92 deletions(-) delete mode 100644 Arduino/CanGrow/include/Webserver/Page_settings.h delete mode 100644 Arduino/CanGrow/include/Webserver/Page_settings_HTML.h create mode 100644 Arduino/CanGrow/include/Webserver/Page_system.h create mode 100644 Arduino/CanGrow/include/Webserver/Page_system_HTML.h diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index fe1a454..b6cbaa3 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -135,7 +135,7 @@ void setup() { LFS_Init(); LoadConfig(); Wifi_Init(); - SetupWebserver(); + Webserver_Init(); } diff --git a/Arduino/CanGrow/include/CanGrow.h b/Arduino/CanGrow/include/CanGrow.h index 9bed8b4..b6a2574 100644 --- a/Arduino/CanGrow/include/CanGrow.h +++ b/Arduino/CanGrow/include/CanGrow.h @@ -43,14 +43,17 @@ #define CANGROW_SSID "CanGrow-unconfigured" +// do we need a restart? (e.g. after wifi settings change) +bool needRestart; + struct Config_WiFi { char ssid[32]; char password[64]; + bool dhcp; byte ip[4] = {192,168,4,20}; byte netmask[4] = {255,255,255,0}; - byte gateway[4] = {192,168,4,255}; + byte gateway[4] = {0,0,0,0}; byte dns[4] = {0,0,0,0}; - bool dhcp; }; Config_WiFi configWifi; diff --git a/Arduino/CanGrow/include/CanGrow_LittleFS.h b/Arduino/CanGrow/include/CanGrow_LittleFS.h index 82642be..45e0b02 100644 --- a/Arduino/CanGrow/include/CanGrow_LittleFS.h +++ b/Arduino/CanGrow/include/CanGrow_LittleFS.h @@ -183,7 +183,8 @@ bool LoadConfig() { JsonObject objWifi = doc["wifi"][0]; strlcpy(configWifi.ssid, objWifi["ssid"], sizeof(configWifi.ssid)); strlcpy(configWifi.password, objWifi["password"], sizeof(configWifi.password)); - + // Copy bool / int directly into struct + configWifi.dhcp = objWifi["dhcp"]; // load the ip addresses as array int i; for(i=0; i <4 ; i++) { @@ -192,8 +193,7 @@ bool LoadConfig() { configWifi.gateway[i] = objWifi["gateway"][i]; configWifi.dns[i] = objWifi["dns"][i]; } - // Copy bool / int directly into struct - configWifi.dhcp = objWifi["dhcp"]; + // * System * JsonObject objSystem = doc["system"][0]; diff --git a/Arduino/CanGrow/include/CanGrow_Webserver.h b/Arduino/CanGrow/include/CanGrow_Webserver.h index 54fe7d9..b93aed3 100644 --- a/Arduino/CanGrow/include/CanGrow_Webserver.h +++ b/Arduino/CanGrow/include/CanGrow_Webserver.h @@ -43,6 +43,8 @@ #include "Webserver/Page_wifi.h" +#include "Webserver/Page_system.h" + AsyncWebServer webserver(80); // log incoming requests @@ -77,22 +79,26 @@ void WebserverNotFound(AsyncWebServerRequest* request) { /* * setup all the webhandlers */ -void SetupWebserver() { +void Webserver_Init() { Serial.println(":: [Webserver] initializing"); /* url handler definition */ 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); + webserver.on("/wifi/", HTTP_GET, WebPage_wifi); + webserver.on("/wifi/", HTTP_POST, WebPage_wifi); + webserver.on("/system/", HTTP_GET, WebPage_system); + webserver.on("/system/", HTTP_POST, WebPage_system); + webserver.on("/system/restart", HTTP_GET, WebPage_system_restart); + webserver.on("/system/restart", HTTP_POST, WebPage_system_restart); requestLogger.setOutput(Serial); // this activates the middleware if(configSystem.httpLogSerial == true) { - Serial.println(":: [Webserver] request logging to serial is enabled"); + Serial.println(":: [Webserver] serial logging: enabled"); webserver.addMiddleware(&requestLogger); } else { - Serial.println(":: [Webserver] request logging to serial is disabled"); + Serial.println(":: [Webserver] serial logging: disabled"); } webserver.onNotFound(WebserverNotFound); diff --git a/Arduino/CanGrow/include/CanGrow_Wifi.h b/Arduino/CanGrow/include/CanGrow_Wifi.h index 50fde1c..4533a77 100644 --- a/Arduino/CanGrow/include/CanGrow_Wifi.h +++ b/Arduino/CanGrow/include/CanGrow_Wifi.h @@ -33,10 +33,10 @@ void Wifi_Connect() { WiFi.begin(configWifi.ssid, configWifi.password); if(configWifi.dhcp == false) { Serial.println(":: [WiFi] using static ip configuration:"); - Serial.printf(":: [WiFi] IP : %s\n", IP2Char(configWifi.ip)); - Serial.printf(":: [WiFi] Netmask: %s\n", IP2Char(configWifi.netmask)); - Serial.printf(":: [WiFi] Gateway: %s\n", IP2Char(configWifi.gateway)); - Serial.printf(":: [WiFi] DNS : %s\n", IP2Char(configWifi.dns)); + Serial.printf(":: [WiFi] IP : %s\n", IP2Char(configWifi.ip)); + Serial.printf(":: [WiFi] Netmask: %s\n", IP2Char(configWifi.netmask)); + Serial.printf(":: [WiFi] Gateway: %s\n", IP2Char(configWifi.gateway)); + Serial.printf(":: [WiFi] DNS : %s\n", IP2Char(configWifi.dns)); WiFi.config(configWifi.ip, configWifi.dns, configWifi.gateway, configWifi.netmask); } else { @@ -53,10 +53,10 @@ void Wifi_Connect() { if(configWifi.dhcp == true) { Serial.println(":: [WiFi] DHCP offered ip configuration:"); - Serial.printf(":: [WiFi] IP : %s\n", IP2Char(WiFi.localIP())); - Serial.printf(":: [WiFi] Netmask: %s\n", IP2Char(WiFi.subnetMask())); - Serial.printf(":: [WiFi] Gateway: %s\n", IP2Char(WiFi.gatewayIP())); - Serial.printf(":: [WiFi] DNS : %s\n", IP2Char(WiFi.dnsIP())); + Serial.printf(":: [WiFi] IP : %s\n", IP2Char(WiFi.localIP())); + Serial.printf(":: [WiFi] Netmask: %s\n", IP2Char(WiFi.subnetMask())); + Serial.printf(":: [WiFi] Gateway: %s\n", IP2Char(WiFi.gatewayIP())); + Serial.printf(":: [WiFi] DNS : %s\n", IP2Char(WiFi.dnsIP())); } } @@ -65,17 +65,18 @@ void Wifi_AP() { Serial.printf(":: [WiFi] create access point: %s\n", CANGROW_SSID); WiFi.softAPConfig(configWifi.ip, configWifi.gateway, configWifi.netmask); WiFi.softAP(CANGROW_SSID); - Serial.print(":: [WiFi] access point started with IP: "); - Serial.println(WiFi.softAPIP()); + Serial.println(":: [WiFi] access point started:"); + Serial.printf(":: [WiFi] IP : %s\n", IP2Char(configWifi.ip)); + Serial.printf(":: [WiFi] Netmask: %s\n", IP2Char(configWifi.netmask)); } void Wifi_Init() { Serial.println(":: [WiFi] initializing"); if(strlen(configWifi.ssid) == 0) { - Serial.println(":: [WiFi] no value found in configWifi.ssid, creating access point"); + Serial.println(":: [WiFi] configWifi.ssid is unset"); Wifi_AP(); } else { - Serial.printf(":: [SETUP] value found in configWifi.ssid, connecting to SSID: %s\n", configWifi.ssid); + Serial.printf(":: [WiFi] connecting to SSID: %s\n", configWifi.ssid); Wifi_Connect(); } } diff --git a/Arduino/CanGrow/include/Webserver/Common.h b/Arduino/CanGrow/include/Webserver/Common.h index 43fd9b4..db28d78 100644 --- a/Arduino/CanGrow/include/Webserver/Common.h +++ b/Arduino/CanGrow/include/Webserver/Common.h @@ -37,12 +37,13 @@ bool TestHeaderFooter(const String& var) { #endif if( - (var == "HEADER") || - (var == "FOOTER") || - (var == "CGVER") || - (var == "CGBUILD") || - (var == "GROWNAME") || - (var == "CANGROW_CSS")) { + (var == "HEADER") || + (var == "FOOTER") || + (var == "CGVER") || + (var == "CGBUILD") || + (var == "GROWNAME") || + (var == "CANGROW_CSS") || + (var == "NEED_RESTART")) { return true; } else { return false; @@ -62,6 +63,12 @@ String AddHeaderFooter(const String& var) { return String(configGrow.growName); } else if(var == "CANGROW_CSS") { return String(File_cangrow_CSS); + } else if(var == "NEED_RESTART") { + if(needRestart == true) { + return String(Common_HTML_NEED_RESTART); + } else { + return String(); + } } else { return String(); } diff --git a/Arduino/CanGrow/include/Webserver/Common_HTML.h b/Arduino/CanGrow/include/Webserver/Common_HTML.h index 3d87dc8..72c98e1 100644 --- a/Arduino/CanGrow/include/Webserver/Common_HTML.h +++ b/Arduino/CanGrow/include/Webserver/Common_HTML.h @@ -36,3 +36,11 @@ const char Common_HTML_SAVE_MSG[] PROGMEM = R"EOF( const char Common_HTML_SAVE_MSG_ERR[] PROGMEM = R"EOF(
!! ERROR saving!
)EOF"; + +const char Common_HTML_NEED_RESTART[] PROGMEM = R"EOF( +
❗ Restart is required to apply new settings! +
+ +
+
+)EOF"; diff --git a/Arduino/CanGrow/include/Webserver/Header.h b/Arduino/CanGrow/include/Webserver/Header.h index 2a5260e..dd084da 100644 --- a/Arduino/CanGrow/include/Webserver/Header.h +++ b/Arduino/CanGrow/include/Webserver/Header.h @@ -40,11 +40,12 @@ const char* Header_HTML PROGMEM = R"( -
)"; +
+ %NEED_RESTART%)"; diff --git a/Arduino/CanGrow/include/Webserver/Page_settings.h b/Arduino/CanGrow/include/Webserver/Page_settings.h deleted file mode 100644 index f0e5a30..0000000 --- a/Arduino/CanGrow/include/Webserver/Page_settings.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * - * include/Webserver/Page_settings.h - settings page header file - * - * - * 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. - * - */ - - diff --git a/Arduino/CanGrow/include/Webserver/Page_settings_HTML.h b/Arduino/CanGrow/include/Webserver/Page_settings_HTML.h deleted file mode 100644 index e88c72c..0000000 --- a/Arduino/CanGrow/include/Webserver/Page_settings_HTML.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * - * include/Webserver/Page_settings_HTML.h - settings page HTML header file - * - * - * 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. - * - */ diff --git a/Arduino/CanGrow/include/Webserver/Page_system.h b/Arduino/CanGrow/include/Webserver/Page_system.h new file mode 100644 index 0000000..932e715 --- /dev/null +++ b/Arduino/CanGrow/include/Webserver/Page_system.h @@ -0,0 +1,79 @@ +/* + * + * include/Webserver/Page_system.h - system settings page header file + * + * + * 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. + * + */ + + +#include "Page_system_HTML.h" + + + +// https://techtutorialsx.com/2018/07/23/esp32-arduino-http-server-template-processing-with-multiple-placeholders/ +String Proc_WebPage_system(const String& var) { + if(TestHeaderFooter(var)) { + return AddHeaderFooter(var); + } else if(var == "LOL") { + return String("Nice"); + } else { + return String(); + } +} + +void WebPage_system(AsyncWebServerRequest *request) { + request->send_P(200, "text/html", Page_system_HTML, Proc_WebPage_system); +} + + +/* + * Page subpage restart + */ +String Proc_WebPage_system_restart(const String& var) { + if(TestHeaderFooter(var)) { + return AddHeaderFooter(var); + } else if(var == "RESTART_MSG") { + return String(Page_system_restart_HTML_RESTART_MSG); + } else { + return String(); + } +} + +String Proc_WebPage_system_restart_POST(const String& var) { + if(var == "RESTART_MSG") { + return String(Page_system_restart_HTML_RESTART_MSG_POST); + } else { + return Proc_WebPage_system_restart(var); + } +} + +void WebPage_system_restart(AsyncWebServerRequest *request) { + if(request->method() == HTTP_POST) { + request->send_P(200, "text/html", Page_system_restart_HTML, Proc_WebPage_system_restart_POST); + } else { + request->send_P(200, "text/html", Page_system_restart_HTML, Proc_WebPage_system_restart); + } + +} diff --git a/Arduino/CanGrow/include/Webserver/Page_system_HTML.h b/Arduino/CanGrow/include/Webserver/Page_system_HTML.h new file mode 100644 index 0000000..a6d5a2b --- /dev/null +++ b/Arduino/CanGrow/include/Webserver/Page_system_HTML.h @@ -0,0 +1,89 @@ +/* + * + * include/Webserver/Page_system_HTML.h - system settings page HTML header file + * + * + * 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* Page_system_HTML PROGMEM = R"(%HEADER% +

⚙ System settings

+ +

here you can set which features and sensors you use

+Fan mode:
+Pump mode:

1: Water every few days.
2: Water if the soil moisture falls below Soilmoisture low value
3: Water every few days if the soil moisture falls below Soilmoisture low value.

+Use relais for LED:
+Use relais for FAN:
+Pump ON time: Seconds
+Soilmoisture sensor:
+Soilmoisture low: %
+Temperature sensor:
+NTP offset: Hours
+Maintenance Duration: Seconds
+ESP32-Cam IP (optional):
+ +
+%FOOTER%)"; + + +const char* Page_system_restart_HTML PROGMEM = R"(%HEADER% +

❗ Restart CanGrow

+
+%RESTART_MSG% +
+%FOOTER%)"; + +const char* Page_system_restart_HTML_RESTART_MSG PROGMEM = R"(Do you want to restart CanGrow?
Please confirm. +
+ +
)"; + +const char* Page_system_restart_HTML_RESTART_MSG_POST PROGMEM = R"(Restarting...)"; diff --git a/Arduino/CanGrow/include/Webserver/Page_wifi.h b/Arduino/CanGrow/include/Webserver/Page_wifi.h index 6d58394..3f49715 100644 --- a/Arduino/CanGrow/include/Webserver/Page_wifi.h +++ b/Arduino/CanGrow/include/Webserver/Page_wifi.h @@ -192,6 +192,8 @@ void WebPage_wifi(AsyncWebServerRequest *request) { } if(SaveConfig()) { + // we need a restart to apply the new settings + needRestart = true; Serial.println(":: [Webserver:wifi] config saved"); request->send_P(200, "text/html", Page_wifi_HTML, Proc_WebPage_wifi_POST); } else { diff --git a/Arduino/CanGrow/include/Webserver/Page_wifi_HTML.h b/Arduino/CanGrow/include/Webserver/Page_wifi_HTML.h index 8921f41..88e21f2 100644 --- a/Arduino/CanGrow/include/Webserver/Page_wifi_HTML.h +++ b/Arduino/CanGrow/include/Webserver/Page_wifi_HTML.h @@ -35,7 +35,7 @@ const char* Page_wifi_HTML PROGMEM = R"(%HEADER%

Select your wifi network from the SSID list.
Reload the page, if your network is not listed.

-
+ SSID: