From b3c0959521cc3393a0e0fd1b22067f2bdbd584a4 Mon Sep 17 00:00:00 2001 From: Marcus Date: Wed, 17 Apr 2024 02:03:45 +0200 Subject: [PATCH] firmware wip - changed webhandlers - working now with redirects instead setting handlers --- Arduino/CanGrow/CanGrow.ino | 342 ++++++++++++++++++------------------ 1 file changed, 173 insertions(+), 169 deletions(-) diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index 4d8212b..42cd920 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -945,56 +945,34 @@ void WebHandler() { * * WebHandler_unconfigured() and WebHandler_configured() */ - // generic handler + // style.css + webserver.on("/style.css", HTTP_GET, WEBstyleCSS); + + // Web root + webserver.on("/", HTTP_GET, WEBroot); + // WiFi Stuff webserver.on("/wifiSettings", HTTP_GET, WEBwifiSettings); webserver.on("/wifiSettings/save", HTTP_POST, POSTwifiSettings); - webserver.on("/style.css", HTTP_GET, WEBstyleCSS); + // System stuff + webserver.on("/systemSettings", HTTP_GET, WEBsystemSettings); + webserver.on("/systemSettings/save", HTTP_POST, POSTsystemSettings); + + // Grow stuff + webserver.on("/growSettings", HTTP_GET, WEBgrowSettings); + webserver.on("/growSettings/save", HTTP_POST, POSTgrowSettings); // help webserver.on("/help", HTTP_GET, WEBhelp); // does not work atm TODO - webserver.on("/logout", [](){ webserver.send(401, "text/html", "logged out!"); }); + //webserver.on("/logout", [](){ webserver.send(401, "text/html", "logged out!"); }); // 404 handling // favicon.ico is a special one, because its requested everytime and i dont wont to deliver the // failed whole page every call. we can save up this 0,5kb traffic :o) webserver.on("/favicon.ico", [](){ webserver.send(404, "text/html", "404 - not found"); }); webserver.onNotFound(WEB404); - - - // when wifi is unconfigured (WIFIssid <1) then root are Wifi settings - if(strlen(WIFIssid) < 1) { - webserver.on("/", HTTP_GET, WEBwifiSettings); - webserver.on("/systemSettings", HTTP_GET, WEBwifiSettings); - webserver.on("/growSettings", HTTP_GET, WEBwifiSettings); - } else { - - // now we need to receive systemSettings - webserver.on("/systemSettings/save", HTTP_POST, POSTsystemSettings); - - // when system settings are unconfigured , then system settings are root - if(configured == false) { - webserver.on("/", HTTP_GET, WEBsystemSettings); - webserver.on("/systemSettings", HTTP_GET, WEBsystemSettings); - webserver.on("/growSettings", HTTP_GET, WEBsystemSettings); - - - } else { - webserver.on("/systemSettings", HTTP_GET, WEBsystemSettings); - } - - - webserver.on("/growSettings", HTTP_GET, WEBgrowSettings); - // now we need to receive growSettings - webserver.on("/growSettings/save", HTTP_POST, POSTgrowSettings); - // when grow is unconfigured (GrowStart <1) then Grow settings are root - if(GrowStart < 1) { - webserver.on("/", HTTP_GET, WEBgrowSettings); - } else { - webserver.on("/", HTTP_GET, WEBroot); - } - } + } @@ -1101,14 +1079,26 @@ void WEBhelp() { */ void WEBroot() { - String body = FPSTR(HTMLheader); - body += "

configured!

"; - body += "

"; - body += timeClient.getFormattedTime(); - body += "

"; - body += returnHTMLfooter(); - - webserver.send(200, "text/html", body); + if(strlen(WIFIssid) < 1) { + webserver.sendHeader("Location", String("/wifiSettings"), true); + webserver.send(302, "text/plain", "please configure wifiSettings first"); + } else if(configured == false){ + webserver.sendHeader("Location", String("/systemSettings"), true); + webserver.send(302, "text/plain", "please configure systemSettings first"); + } else if(strlen(GrowName) < 1){ + webserver.sendHeader("Location", String("/growSettings"), true); + webserver.send(302, "text/plain", "please configure growSettings first"); + } else { + String body = FPSTR(HTMLheader); + + body += "

configured!

"; + body += "

"; + body += timeClient.getFormattedTime(); + body += "

"; + body += returnHTMLfooter(); + + webserver.send(200, "text/html", body); + } } /* @@ -1163,143 +1153,157 @@ void WEBwifiSettings() { } void WEBsystemSettings() { - String body = FPSTR(HTMLheader); - - if(configured == false) { - body += "

Step 2: System settings

"; - body += "

Please configure all settings
"; - body += "

"; - } - - body += "

System settings

"; - if(webserver.hasArg("success")) { - body += FPSTR(HTMLsuccess); - } - body += "

here you can set which features and sensors you use
"; - body += "

"; - - // form starts - body += "
\n"; - - // UseFan bool - body += "Use fan:
\n"; - - // UsePump bool - body += "Use pump:
\n"; - - // UseLEDrelais bool - body += "Use relais for LED:
\n"; - - // TODO ugly. can this done be better? - // PumpOnTime int - body += "Pump on time:
\n"; - - // MoistureSensor_Type byte - body += "Moisture sensor type:
\n"; - - // SoilmoistureLow byte - body += "Soil moisture low:
\n"; + + body += "

System settings

"; + if(webserver.hasArg("success")) { + body += FPSTR(HTMLsuccess); + } + body += "

here you can set which features and sensors you use
"; + body += "

"; + + // form starts + body += "\n"; + + // UseFan bool + body += "Use fan:
\n"; - // ntpOffset int - body += "NTP offset:
\n"; - - body += "\n"; - body += "
\n"; + // UsePump bool + body += "Use pump:
\n"; + + // UseLEDrelais bool + body += "Use relais for LED:
\n"; + + // TODO ugly. can this done be better? + // PumpOnTime int + body += "Pump on time:
\n"; - body += returnHTMLfooter(); + // MoistureSensor_Type byte + body += "Moisture sensor type:
\n"; - webserver.send(200, "text/html", body); + // SoilmoistureLow byte + body += "Soil moisture low:
\n"; + + // ntpOffset int + body += "NTP offset:
\n"; + + body += "\n"; + body += "\n"; + + body += returnHTMLfooter(); + + webserver.send(200, "text/html", body); + } } /* * Grow pages */ void WEBgrowSettings() { - String body = FPSTR(HTMLheader); + // if system settings are unconfigured, we cannot proceed with growSettings + if(configured == false) { + webserver.sendHeader("Location", String("/systemSettings"), true); + webserver.send(302, "text/plain", "please configure systemSettings first"); + } else { - if(strlen(GrowName) < 1) { - body += "

Step 3: Grow settings

"; - body += "

Please configure all settings
"; + String body = FPSTR(HTMLheader); + + if(strlen(GrowName) < 1) { + body += "

Step 3: Grow settings

"; + body += "

Please configure all settings
"; + body += "

"; + } + + body += "

Grow Settings

"; + if(webserver.hasArg("success")) { + body += FPSTR(HTMLsuccess); + } + body += "

Here you can set everything grow related, like light hours, how much water, LED brightness
"; body += "

"; + + body += "
\n"; + + + + body += "Grow name:
\n"; + + body += "Grow start date:
\n"; + + body += "Days of vegetation:
\n"; + + body += "Days of bloom:
\n"; + + body += "Hours light on vegetation:
\n"; + + body += "Hours light on bloom:
\n"; + + body += "Sunrise: \n"; + body += " :
\n"; + + if(UseLEDrelais == false) { + body += "Brightness LED:
\n"; + } + + body += "\n"; + body += "
\n"; + body += returnHTMLfooter(); + + webserver.send(200, "text/html", body); } - - body += "

Grow Settings

"; - if(webserver.hasArg("success")) { - body += FPSTR(HTMLsuccess); - } - body += "

Here you can set everything grow related, like light hours, how much water, LED brightness
"; - body += "

"; - - body += "
\n"; - - - - body += "Grow name:
\n"; - - body += "Grow start date:
\n"; - - body += "Days of vegetation:
\n"; - - body += "Days of bloom:
\n"; - - body += "Hours light on vegetation:
\n"; - - body += "Hours light on bloom:
\n"; - - body += "Sunrise: \n"; - body += " :
\n"; - - if(UseLEDrelais == false) { - body += "Brightness LED:
\n"; - } - - body += "\n"; - body += "
\n"; - body += returnHTMLfooter(); - - webserver.send(200, "text/html", body); }