From c321695d9225792271c879bfa0c2c3570e42fbe5 Mon Sep 17 00:00:00 2001 From: Marcus Date: Wed, 17 Apr 2024 01:08:11 +0200 Subject: [PATCH] firmware wip - i think i now have everything EEPROM load and save related stuff --- Arduino/CanGrow/CanGrow.ino | 224 +++++++++++++++++++++++++++++++----- 1 file changed, 198 insertions(+), 26 deletions(-) diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index ef0b311..ba80e87 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -77,17 +77,17 @@ bool UsePump; // UseFan - is the fan used? bool byte PumpOnTime; bool UseFan; - // In case the user uses no 12V LED on the LED output and an relais instead // we have to disable PWM. So we ask here for what kind of light user is going bool UseLEDrelais; + + + // // Grow Stuff // -unsigned int NtpOffset; - // 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) @@ -108,7 +108,8 @@ byte SunriseHour; // SunriseHour - contains to which minute of SunriseHour the growlight turns on byte SunriseMinute; // PINled_PWM - contains the PWM value for dimming the grow light -byte PINled_PWM; +// default is 255 to ensure it is just on for the case UseLEDrelais is true +byte PINled_PWM = 255; @@ -645,6 +646,8 @@ bool loadEEPROM() { EEPROM.get(166, SoilmoistureLow); // size is 2 byte EEPROM.get(167, ntpOffset); + // size is 1 byte + EEPROM.get(169, UseLEDrelais); // TODO auth does not work atm // EEPROM.get(160, WebUiUsername); @@ -654,11 +657,36 @@ bool loadEEPROM() { * Grow settings */ - //TBD + // size is 32 byte + EEPROM.get(170, GrowName); + // size is 4 byte + EEPROM.get(202, GrowStart); + // size is 1 byte + EEPROM.get(206, DaysVeg); + // size is 1 byte + EEPROM.get(207, DaysBloom); + // size is 1 byte + EEPROM.get(208, LighthoursVeg); + // size is 1 byte + EEPROM.get(209, LighthoursBloom); + // size is 1 byte + EEPROM.get(210, SunriseHour); + // size is 1 byte + EEPROM.get(211, SunriseMinute); + + // to ensure PINled_PWM always set to 255 when UseLEDrelais is true + // we set it here again, does not matter whats stored. + // size is 1 byte + if(UseLEDrelais == true) { + PINled_PWM = 255; + } else { + EEPROM.get(212, PINled_PWM); + } // print values to Serial output + Serial.println("---- WiFi values ----"); Serial.print("WIFIssid: "); Serial.println(WIFIssid); Serial.print("WIFIpassword: "); @@ -666,6 +694,9 @@ bool loadEEPROM() { Serial.print("Use DHCP: "); Serial.println(WIFIuseDHCP); + Serial.println("---- System values ----"); + Serial.print("configured: "); + Serial.println(configured); Serial.print("UseFan: "); Serial.println(UseFan); Serial.print("UsePump: "); @@ -677,11 +708,31 @@ bool loadEEPROM() { Serial.print("SoilmoistureLow: "); Serial.println(SoilmoistureLow); Serial.print("ntpOffset: "); - Serial.println(ntpOffset); + Serial.println(ntpOffset); + Serial.print("UseLEDrelais: "); + Serial.println(UseLEDrelais); + + Serial.println("---- Grow values ----"); + Serial.print("GrowName: "); + Serial.println(GrowName); + Serial.print("GrowStart: "); + Serial.println(GrowStart); + Serial.print("DaysVeg: "); + Serial.println(DaysVeg); + Serial.print("DaysBloom: "); + Serial.println(DaysBloom); + Serial.print("LighthoursVeg: "); + Serial.println(LighthoursVeg); + Serial.print("LighthoursBloom: "); + Serial.println(LighthoursBloom); + Serial.print("SunriseHour: "); + Serial.println(SunriseHour); + Serial.print("SunriseMinute: "); + Serial.println(SunriseMinute); + Serial.print("PINled_PWM: "); + Serial.println(PINled_PWM); - Serial.print("configured: "); - Serial.println(configured); } else { Serial.println("EEPROM value WIFIssid is empty"); } @@ -959,6 +1010,22 @@ void WebAuthApi() { } } +// returns footer with javascript stuff +String returnHTMLfooter() { + String footer; + // show the GrowName in the menu if set + if(strlen(GrowName) > 0){ + // add a seedling to the growname :) + String divGrowName = "🌱 "; + divGrowName += GrowName; + // replace content of html ID GrowName + footer += JSreplaceStr("GrowName", divGrowName); + } + + footer += FPSTR(HTMLfooter); + + return footer; +} /* * returnSelected(bool) @@ -992,7 +1059,7 @@ void WEBstyleCSS() { void WEB404() { String body = FPSTR(HTMLheader); body += "

404 - not found

"; - body += FPSTR(HTMLfooter); + body += returnHTMLfooter(); webserver.send(404, "text/html", body); } @@ -1008,8 +1075,7 @@ void WEBlogout() { void WEBhelp() { String body = FPSTR(HTMLheader); body += FPSTR(HTMLhelp); - body += JSreplaceStr("GrowName", "abcdefgh"); - body += FPSTR(HTMLfooter); + body += returnHTMLfooter(); webserver.send(200, "text/html", body); } @@ -1023,7 +1089,7 @@ void WEBroot() { body += "

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

"; - body += FPSTR(HTMLfooter); + body += returnHTMLfooter(); webserver.send(200, "text/html", body); } @@ -1059,6 +1125,11 @@ void WEBwifiSettings() { body += wifiName + "\n"; } body += "
\n"; + if(strlen(WIFIssid) > 0) { + body += "Currently connected to: "; + body += WIFIssid; + body += "
\n"; + } body += "Password:
\n"; body += "IP:
\n"; body += "Subnet mask:
\n"; @@ -1066,7 +1137,7 @@ void WEBwifiSettings() { body += "DNS:
\n"; body += "\n"; body += "\n"; - body += FPSTR(HTMLfooter); + body += returnHTMLfooter(); webserver.send(200, "text/html", body); } @@ -1087,33 +1158,37 @@ void WEBsystemSettings() { 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"; - - body += "Use relais for LED: \n"; if(configured == false){body += "\n";} body += "\n"; body += "\n"; body += "
\n"; // TODO ugly. can this done be better? - body += "Pump on time:
\n"; + // MoistureSensor_Type byte body += "Moisture sensor type:
\n"; - body += "Soil moisture low:
\n"; - body += "NTP offset:
\n"; body += "\n"; body += "
\n"; - body += FPSTR(HTMLfooter); + body += returnHTMLfooter(); webserver.send(200, "text/html", body); } @@ -1159,12 +1236,48 @@ void WEBgrowSettings() { body += "
\n"; - body += "Brightness "; + + + 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 += FPSTR(HTMLfooter); + body += returnHTMLfooter(); webserver.send(200, "text/html", body); } @@ -1177,12 +1290,71 @@ void WEBgrowSettings() { */ void POSTgrowSettings() { - PINled_PWM = webserver.arg("PINled_PWM").toInt(); + + if(UseLEDrelais == true) { + // if a relais is used to turn on grow light, we force PWM to max val + PINled_PWM = 255; + } else { + // otherwise just do PWM + PINled_PWM = webserver.arg("PINled_PWM").toInt(); + } + + String GrowName_tmp = webserver.arg("GrowName"); + GrowName_tmp.toCharArray(GrowName, 32); + + + GrowStart = webserver.arg("GrowStart").toInt(); + DaysVeg = webserver.arg("DaysVeg").toInt(); + DaysBloom = webserver.arg("DaysBloom").toInt(); + LighthoursVeg = webserver.arg("LighthoursVeg").toInt(); + LighthoursBloom = webserver.arg("LighthoursBloom").toInt(); + SunriseHour = webserver.arg("SunriseHour").toInt(); + SunriseMinute = webserver.arg("SunriseMinute").toInt(); + + // size is 32 byte + EEPROM.put(170, GrowName); + // size is 4 byte + EEPROM.put(202, GrowStart); + // size is 1 byte + EEPROM.put(206, DaysVeg); + // size is 1 byte + EEPROM.put(207, DaysBloom); + // size is 1 byte + EEPROM.put(208, LighthoursVeg); + // size is 1 byte + EEPROM.put(209, LighthoursBloom); + // size is 1 byte + EEPROM.put(210, SunriseHour); + // size is 1 byte + EEPROM.put(211, SunriseMinute); + // size is 1 byte + EEPROM.put(212, PINled_PWM); + + EEPROM.commit(); + + + analogWrite(PINled, PINled_PWM); Serial.println(":: POSTgrowSettings ::"); + Serial.print("GrowName: "); + Serial.println(GrowName); + Serial.print("GrowStart: "); + Serial.println(GrowStart); + Serial.print("DaysVeg: "); + Serial.println(DaysVeg); + Serial.print("DaysBloom: "); + Serial.println(DaysBloom); + Serial.print("LighthoursVeg: "); + Serial.println(LighthoursVeg); + Serial.print("LighthoursBloom: "); + Serial.println(LighthoursBloom); + Serial.print("SunriseHour: "); + Serial.println(SunriseHour); + Serial.print("SunriseMinute: "); + Serial.println(SunriseMinute); Serial.print("PINled_PWM: "); Serial.println(PINled_PWM); @@ -1198,7 +1370,7 @@ void POSTsystemSettings() { UsePump = webserver.arg("UsePump").toInt(); PumpOnTime = webserver.arg("PumpOnTime").toInt(); UseFan = webserver.arg("UseFan").toInt(); - + UseLEDrelais = webserver.arg("UseLEDrelais").toInt(); configured = true; // size is 1 byte