added system settings page, added web restart dialoge

This commit is contained in:
Marcus 2024-10-21 22:16:20 +02:00
parent 827c8cd184
commit de26abaf05
14 changed files with 232 additions and 92 deletions

View file

@ -135,7 +135,7 @@ void setup() {
LFS_Init();
LoadConfig();
Wifi_Init();
SetupWebserver();
Webserver_Init();
}

View file

@ -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;

View file

@ -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];

View file

@ -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);

View file

@ -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();
}
}

View file

@ -42,7 +42,8 @@ bool TestHeaderFooter(const String& var) {
(var == "CGVER") ||
(var == "CGBUILD") ||
(var == "GROWNAME") ||
(var == "CANGROW_CSS")) {
(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();
}

View file

@ -36,3 +36,11 @@ const char Common_HTML_SAVE_MSG[] PROGMEM = R"EOF(
const char Common_HTML_SAVE_MSG_ERR[] PROGMEM = R"EOF(
<div class='infomsg'>!! ERROR saving!</div>
)EOF";
const char Common_HTML_NEED_RESTART[] PROGMEM = R"EOF(
<div class='warnmsg'>&#10071; Restart is required to apply new settings!
<form action='/system/restart'>
<input type='submit' value='Restart now' />
</form>
</div>
)EOF";

View file

@ -40,11 +40,12 @@ const char* Header_HTML PROGMEM = R"(<!DOCTYPE html>
</head>
<body>
<ul class='nav'><li><a href='/'>&#x1F331; %GROWNAME%</a></li>
<li><a href='/growSettings' >&#128262; Grow settings</a></li>
<li><a href='/systemSettings' >&#9881; System settings</a></li>
<li><a href='/wifiSettings' >&#128225; WiFi settings</a></li>
<li><a href='/grow/' >&#128262; Grow settings</a></li>
<li><a href='/system/' >&#9881; System settings</a></li>
<li><a href='/wifi/' >&#128225; WiFi settings</a></li>
<li><a href='/help' >&#x2753; Help</a></li>
<li><span class='MenuTime'>05:01:40</span></li>
<li><a href='https://git.la10cy.net/DeltaLima/CanGrow' target='_blank'>CanGrow v%CGVER% - %CGBUILD%</a></li>
</ul>
<div class='center'>)";
<div class='center'>
%NEED_RESTART%)";

View file

@ -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.
*
*/

View file

@ -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.
*
*/

View file

@ -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);
}
}

View file

@ -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%
<h2>&#9881; System settings</h2>
<ul class='subnav'>
<li><a href='/system/update'>&#x1F504; Firmware update</a></li>
<li><a href='/system/restart' >&#x1F501; CanGrow restart</a></li>
<li><a href='/system/wipe' >&#x1F4A3; Factory reset</a></li>
</ul>
<p>here you can set which features and sensors you use<br></p><form method='post' action='/systemSettings/save'>
Fan mode: <select id='UseFan' name='UseFan' required>
<option value='1'>Off</option>
<option value='1' selected >Use</option>
<option value='0'>Do not use</option>
</select><br>
Pump mode: <select id='UsePump' name='UsePump' required>
<option value='0' selected >Off</option>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
</select><br><p class='helpbox'><b>1:</b> Water every few days.<br> <b>2:</b> Water if the soil moisture falls below <i>Soilmoisture low</i> value<br> <b>3:</b> Water every few days if the soil moisture falls below <i>Soilmoisture low</i> value.</p>
Use relais for LED: <select id='UseLEDrelais' name='UseLEDrelais' required>
<option value='1'>Yes</option>
<option value='0' selected >No</option>
</select><br>
Use relais for FAN: <select id='UseFANrelais' name='UseFANrelais' required>
<option value='1'>Yes</option>
<option value='0' selected >No</option>
</select><br>
Pump ON time: <input class='inputShort' type='number' name='PumpOnTime' min='0' max='255' value='1' required> Seconds<br>
Soilmoisture sensor: <select id='MoistureSensor_Type' name='MoistureSensor_Type' required>
<option value='1'>Analog capacitive</option>
<option value='2' selected >I2C Chirp</option>
</select><br>
Soilmoisture low: <input class='inputShort' type='number' name='SoilmoistureLow' min='0' value='22' required> %<br>
Temperature sensor: <select id='TemperatureSensor_Type' name='TemperatureSensor_Type' required>
<option value='1' selected >I2C BME280</option>
<option value='2'>I2C Chirp</option>
</select><br>
NTP offset: <input class='inputShort' type='number' name='NtpOffset' min='-12' max='14' value='2' required> Hours<br>
Maintenance Duration: <input class='inputShort' type='number' name='MaintenanceDuration' min='0' max='900' value='900' required> Seconds<br>
ESP32-Cam IP (optional): <input type='text' name='Esp32CamIP' maxlength='16' value='' ><br>
<input type='submit' value='&#x1F4BE; Save settings'>
</form>
%FOOTER%)";
const char* Page_system_restart_HTML PROGMEM = R"(%HEADER%
<h1>&#10071; Restart CanGrow</h1>
<div class='warnmsg'>
%RESTART_MSG%
</div>
%FOOTER%)";
const char* Page_system_restart_HTML_RESTART_MSG PROGMEM = R"(Do you want to restart CanGrow?<br>Please confirm.
<form action='/system/restart' method='post'><input type='hidden' name='confirmed' value='true' />
<input type='submit' value='Confirm restart' />
</form>)";
const char* Page_system_restart_HTML_RESTART_MSG_POST PROGMEM = R"(Restarting...)";

View file

@ -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 {

View file

@ -35,7 +35,7 @@ const char* Page_wifi_HTML PROGMEM = R"(%HEADER%
<p>Select your wifi network from the SSID list.
<br>Reload the page, if your network is not listed.</p>
<form method='post' action='/wifiSettings/save'>
<form method='post' action='/wifi/'>
<u>SSID</u>:<br>
<select id='configWifi.ssid' name='configWifi.ssid' required>
@ -90,3 +90,5 @@ IP address: <b>%CONFIGWIFI_IP%</b><br>
Subnet mask: <b>%CONFIGWIFI_NETMASK%</b><br>
Gateway: <b>%CONFIGWIFI_GATEWAY%</b><br>
DNS: <b>%CONFIGWIFI_DNS%</b><br><br>)";