firmware wip - i think i now have everything EEPROM load and save related stuff

This commit is contained in:
Marcus 2024-04-17 01:08:11 +02:00
parent 3aed858228
commit c321695d92
1 changed files with 198 additions and 26 deletions

View File

@ -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 += "<h1>404 - not found</h1>";
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 += "<p>";
body += timeClient.getFormattedTime();
body += "</p>";
body += FPSTR(HTMLfooter);
body += returnHTMLfooter();
webserver.send(200, "text/html", body);
}
@ -1059,6 +1125,11 @@ void WEBwifiSettings() {
body += wifiName + "</option>\n";
}
body += "</select><br>\n";
if(strlen(WIFIssid) > 0) {
body += "Currently connected to: ";
body += WIFIssid;
body += "<br>\n";
}
body += "Password: <input type='password' name='WIFIpassword'><br>\n";
body += "IP: <input type='text' name='WIFIip'><br>\n";
body += "Subnet mask: <input type='text' name='WIFInetmask'><br>\n";
@ -1066,7 +1137,7 @@ void WEBwifiSettings() {
body += "DNS: <input type='text' name='WIFIdns'><br>\n";
body += "<input type='submit' value='Save'>\n";
body += "</form>\n";
body += FPSTR(HTMLfooter);
body += returnHTMLfooter();
webserver.send(200, "text/html", body);
}
@ -1087,33 +1158,37 @@ void WEBsystemSettings() {
body += "<p>here you can set which features and sensors you use<br>";
body += "</p>";
// form starts
body += "<form method='post' action='/systemSettings/save'>\n";
// UseFan bool
body += "Use fan: <select id='UseFan' name='UseFan' required>\n";
if(configured == false){body += "<option disabled value='' selected hidden>---</option>\n";}
body += "<option value='1'" + returnStrSelected(UseFan, 1) + ">Yes</option>\n";
body += "<option value='0'" + returnStrSelected(UseFan, 0) + ">No</option>\n";
body += "</select><br>\n";
// UsePump bool
body += "Use pump: <select id='UsePump' name='UsePump' required>\n";
if(configured == false){body += "<option disabled value='' selected hidden>---</option>\n";}
body += "<option value='1'" + returnStrSelected(UsePump, 1) + ">Yes</option>\n";
body += "<option value='0'" + returnStrSelected(UsePump, 0) + ">No</option>\n";
body += "</select><br>\n";
body += "Use relais for LED: <select id='UseLEDrelais' name='UseLEDrelais' required>\n";
// UseLEDrelais bool
body += "Use relais for LED: <select id='UseLEDrelais' name='UseLEDrelais' required>\n";
if(configured == false){body += "<option disabled value='' selected hidden>---</option>\n";}
body += "<option value='1'" + returnStrSelected(UseLEDrelais, 1) + ">Yes</option>\n";
body += "<option value='0'" + returnStrSelected(UseLEDrelais, 0) + ">No</option>\n";
body += "</select><br>\n";
// TODO ugly. can this done be better?
body += "Pump on time: <input type='text' name='PumpOnTime' value='";
// PumpOnTime int
body += "Pump on time: <input type='number' name='PumpOnTime' value='";
body += PumpOnTime;
body += "'required><br>\n";
// MoistureSensor_Type byte
body += "Moisture sensor type: <select id='MoistureSensor_Type' name='MoistureSensor_Type' required>\n";
if(configured == false) {
body += "<option disabled value='' selected hidden>---</option>\n";
@ -1122,18 +1197,20 @@ void WEBsystemSettings() {
body += "<option value='2'" + returnStrSelected(MoistureSensor_Type, 2) + ">I2C chirp</option>\n";
body += "</select><br>\n";
body += "Soil moisture low: <input type='text' name='SoilmoistureLow' value='";
// SoilmoistureLow byte
body += "Soil moisture low: <input type='number' name='SoilmoistureLow' value='";
body += SoilmoistureLow;
body += "' required><br>\n";
body += "NTP offset: <input type='text' name='ntpOffset' value='";
// ntpOffset int
body += "NTP offset: <input type='number' name='ntpOffset' value='";
body += ntpOffset;
body+= "' required><br>\n";
body += "<input type='submit' value='Save'>\n";
body += "</form>\n";
body += FPSTR(HTMLfooter);
body += returnHTMLfooter();
webserver.send(200, "text/html", body);
}
@ -1159,12 +1236,48 @@ void WEBgrowSettings() {
body += "<form method='post' action='/growSettings/save'>\n";
body += "Brightness <input type='range' id='PINled_PWM' name='PINled_PWM' min='1' max='255' value='";
body += PINled_PWM + "'/>";
body += "Grow name: <input type='text' name='GrowName' value='";
body += GrowName;
body+= "' required><br>\n";
body += "Grow start date: <input type='text' name='GrowStart' value='";
body += GrowStart;
body+= "' required><br>\n";
body += "Days of vegetation: <input type='number' name='DaysVeg' value='";
body += DaysVeg;
body+= "' required><br>\n";
body += "Days of bloom: <input type='number' name='DaysBloom' value='";
body += DaysBloom;
body+= "' required><br>\n";
body += "Hours light on vegetation: <input type='number' name='LighthoursVeg' value='";
body += LighthoursVeg;
body+= "' required><br>\n";
body += "Hours light on bloom: <input type='number' name='LighthoursBloom' value='";
body += LighthoursBloom;
body+= "' required><br>\n";
body += "Sunrise: <input type='number' name='SunriseHour' value='";
body += SunriseHour;
body+= "' required>\n";
body += " : <input type='number' name='SunriseMinute' value='";
body += SunriseMinute;
body+= "' required><br>\n";
if(UseLEDrelais == false) {
body += "Brightness LED: <input type='range' id='PINled_PWM' name='PINled_PWM' min='1' max='255' value='";
body += PINled_PWM;
body += "'/><br>\n";
}
body += "<input type='submit' value='Save'>\n";
body += "</form>\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