From 0becdf380ad067bc9fa752f3f24d887c952f1371 Mon Sep 17 00:00:00 2001 From: Marcus Date: Tue, 16 Apr 2024 01:51:24 +0200 Subject: [PATCH] firmware wip --- Arduino/CanGrow/CanGrow.ino | 206 ++++++++++++++++++++++-------------- 1 file changed, 125 insertions(+), 81 deletions(-) diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index 9faef8b..725ffcf 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -70,7 +70,7 @@ char WebUiPassword[32] = "cangrow"; // GrowName - contains the name of the grow/plant. Up to 32 byte char GrowName[32]; // GrowStart - contains unix timestamp from date where grow starts (00:00) -// unsigned long is 4 byte +// unsigned long is 8 byte unsigned long GrowStart; // DayOfGrow contains on which day the grow is byte DayOfGrow; @@ -153,10 +153,10 @@ const char HTMLheader[] PROGMEM = R"EOF(
@@ -181,6 +181,10 @@ body { } +h1, h2, h3, h4, h5 { + text-align: center; +} + a:link, a:visited { color: #04AA6D; } @@ -222,6 +226,12 @@ a:active { list-style: none; } +.nav li:first-of-type { + background: #026b45; + border-top-left-radius: 3px; + border-bottom-left-radius: 3px; +} + .nav li a { color: #ddd; display: block; @@ -234,6 +244,7 @@ a:active { .nav li a:hover { background: #04AA6D; color: #fff; + border-radius: 3px; } .nav li a:active { @@ -242,6 +253,15 @@ a:active { +)EOF"; + +const char HTMLjsNoWifi[] PROGMEM = R"EOF( + +)EOF"; + +const char HTMLhelp[] PROGMEM = R"EOF( +

CanGrow help

+Here you will get some helpful help. )EOF"; /* @@ -547,27 +567,19 @@ void wipeEEPROM() { bool loadEEPROM() { Serial.println(":: loading EEPROM ::"); - /* - * configured - * - * read var configured from address 511 - I put this to the end to - * prevent confusion with the 1 byte offset in the address when it - * would be at the beginning - more a cosmetic thing - * - * All boolean variables are at the end of the EEPROM - */ - EEPROM.get(511, configured); - Serial.print("configured: "); - Serial.println(configured); + + + // read var WIFIssid from address 0, 32 byte long + // read this first, because we decide on the ssid length (>0?) if + // we run in unconfigured AP mode, nor not + EEPROM.get(0, WIFIssid); - // when configured is > 1 (it should == 1) then read EEPROM furher data - if(configured > 0) { + // when length is > 0 then read furter EEPROM config data + if(strlen(WIFIssid)) { /* * WIFI settings */ - - // read var WIFIssid from address 0, 32 byte long - EEPROM.get(0, WIFIssid); + // read var WIFIpassword from address 32, 64 byte long EEPROM.get(32, WIFIpassword); // read var WIFIip from address 96, 16 byte long @@ -585,7 +597,16 @@ bool loadEEPROM() { /* * System settings */ - + /* + * configured + * + * read var configured from address 511 - I put this to the end to + * prevent confusion with the 1 byte offset in the address when it + * would be at the beginning - more a cosmetic thing + * + * All boolean variables are at the end of the EEPROM + */ + EEPROM.get(511, configured); EEPROM.get(160, WebUiUsername); EEPROM.get(176, WebUiPassword); @@ -598,15 +619,18 @@ bool loadEEPROM() { // print values to Serial output - Serial.println(":: EEPROM loaded ::"); Serial.print("WIFIssid: "); Serial.println(WIFIssid); Serial.print("Use DHCP: "); Serial.println(WIFIuseDHCP); + Serial.print("configured: "); + Serial.println(configured); + } else { + Serial.println("EEPROM value WIFIssid is empty"); } Serial.println(":: EEPROM loaded ::"); - return(configured); + return(strlen(WIFIssid)); } void wifiConnect() { @@ -744,20 +768,16 @@ void setup() { // connect to wifi wifiConnect(); - - // use webhandler for configured state - WebHandler_configured(); // configured is 0, setup Access Point } else { // start an wifi accesspoint wifiAp(); - // use webhandler for unconfigured state - WebHandler_unconfigured(); + } - // general webHandler for wifiConfig, 404, ... - WebHandler_general(); + // set web handler + WebHandler(); // start webserver webserver.begin(); @@ -781,13 +801,11 @@ void loop() { * Web Handler */ - - -void WebHandler_general() { - /* +void WebHandler() { + /* * Webserver handlers * here are the generic webserver handlers like 404 not found - * wifiConfig, ... + * wifiSettings, ... * * if you are looking for the single webpages handler, have a look to * @@ -795,10 +813,12 @@ void WebHandler_general() { */ // generic handler // WiFi Stuff - webserver.on("/wifiConfig", HTTP_GET, WEBwifiConfig); - webserver.on("/wifiConfig/save", HTTP_POST, POSTwifiConfig); + webserver.on("/wifiSettings", HTTP_GET, WEBwifiSettings); + webserver.on("/wifiSettings/save", HTTP_POST, POSTwifiSettings); webserver.on("/style.css", HTTP_GET, WEBstyleCSS); + + // does not work atm TODO webserver.on("/logout", [](){ webserver.send(401, "text/html", "logged out!"); }); // 404 handling @@ -806,25 +826,25 @@ void WebHandler_general() { // 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); + + + if(strlen(WIFIssid) < 1) { + webserver.on("/", HTTP_GET, WEBwifiSettings); + } + + if(configured == false) { + webserver.on("/", HTTP_GET, WEBsystemSettings); + webserver.on("/growSettings", HTTP_GET, WEBsystemSettings); + } + + webserver.on("/systemSettings", HTTP_GET, WEBsystemSettings); + + if(GrowStart < 1) { + webserver.on("/", HTTP_GET, WEBgrowSettings); + } + webserver.on("/growSettings", HTTP_GET, WEBgrowSettings); } -void WebHandler_unconfigured() { - /* - * WebHandler_unconfigured() - * - * Here all URL routings are defined, in unconfigured state - */ - webserver.on("/", HTTP_GET, WEBrootUnconfigured); -} - -void WebHandler_configured() { - /* - * WebHandler_unconfigured() - * - * Here all URL routings are defined, in configured state - */ - webserver.on("/", HTTP_GET, WEBroot); -} void WebAuth() { /* @@ -883,20 +903,48 @@ void WEBlogout() { webserver.send(401, "text/html", body); } +void WEBhelp() { + String body = FPSTR(HTMLheader); + body += FPSTR(HTMLhelp); + body += FPSTR(HTMLfooter); + webserver.send(200, "text/html", body); +} + +/* + * Root pages + */ + +void WEBroot() { + String body = FPSTR(HTMLheader); + body += "

configured!

"; + body += "

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

"; + body += FPSTR(HTMLfooter); + + webserver.send(200, "text/html", body); +} /* * Config pages */ -void WEBwifiConfig() { +void WEBwifiSettings() { byte ssidsAvail = WiFi.scanNetworks(); String body = FPSTR(HTMLheader); + if(strlen(WIFIssid) == 0) { + body += "

CanGrow

"; + body += "

CanGrow is actually unconfigured. You need to Setup your WiFi below first.
"; + body += "
After you configured the WiFi connection successfully, you can start your grow 🥦"; + body += "
"; + body += "

"; + } body += "

WiFi config

\n"; if(webserver.hasArg("success")) { - body += "
Successfully saved!
"; + body += "
Successfully saved!
Please restart the device.
"; } body += "

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

"; - body += "
\n"; + body += "\n"; body += "SSID:
\n"; body += "\n"; body += "
\n"; + body += FPSTR(HTMLjsNoWifi); body += FPSTR(HTMLfooter); webserver.send(200, "text/html", body); } -void WEBroot() { +void WEBsystemSettings() { String body = FPSTR(HTMLheader); - body += "

configured!

"; - body += "

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

System settings

"; + body += "

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

"; body += FPSTR(HTMLfooter); webserver.send(200, "text/html", body); } -void WEBrootUnconfigured() { +/* + * Grow pages + */ +void WEBgrowSettings() { String body = FPSTR(HTMLheader); - body += "

CanGrow

"; - body += "

CanGrow is actually unconfigured. You need to Configure WiFi.
"; - body += "
After you configured the WiFi connection successfully, you can start your grow 🥦"; - body += "
"; + body += "

Grow Settings

"; + body += "

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

"; body += FPSTR(HTMLfooter); webserver.send(200, "text/html", body); -} +} + /* * @@ -952,7 +1002,7 @@ void WEBrootUnconfigured() { * */ -void POSTwifiConfig() { +void POSTwifiSettings() { String WIFIssid_new = webserver.arg("WIFIssid"); String WIFIpassword_new = webserver.arg("WIFIpassword"); String WIFIip_new = webserver.arg("WIFIip"); @@ -976,7 +1026,7 @@ void POSTwifiConfig() { WIFIuseDHCP = true; } - configured = true; + EEPROM.put(0, WIFIssid); EEPROM.put(32, WIFIpassword); @@ -985,11 +1035,10 @@ void POSTwifiConfig() { EEPROM.put(128, WIFIgateway); EEPROM.put(144, WIFIdns); EEPROM.put(510, WIFIuseDHCP); - EEPROM.put(511, configured); EEPROM.commit(); - Serial.println(":: POSTwifiConfig ::"); + Serial.println(":: POSTwifiSettings ::"); Serial.print("WIFIssid: "); Serial.println(WIFIssid_new); @@ -1007,15 +1056,10 @@ void POSTwifiConfig() { Serial.println(WIFIdns_new); Serial.print("WIFIuseDHCP: "); Serial.println(WIFIuseDHCP); - Serial.print("configured: "); - Serial.println(configured); - String body = FPSTR(HTMLheader); - body += "

WiFi Config

"; - body += "

WiFi settings successfully saved. Please restart the device.

"; - body += FPSTR(HTMLfooter); - - webserver.send(200, "text/html", body); + + webserver.sendHeader("Location", String("/wifiSettings?success"), true); + webserver.send(302, "text/plain", "wifiSettings/save: success!"); }