firmware WIP - systemSettings restore settings

This commit is contained in:
Marcus 2024-04-16 23:26:58 +02:00
parent 150cbc17d2
commit 3aed858228

View file

@ -67,8 +67,8 @@ bool configured;
// NTP Offset
int ntpOffset;
// MoistureSensor_Type - contains which moisture sensor to use
// 0: analog capacitive sensor
// 1: I2C chirp sensor from catnip electronics
// 1: analog capacitive sensor
// 2: I2C chirp sensor from catnip electronics
byte MoistureSensor_Type;
// SoilmoistureLow - contains the value , when soil moisture is assumed to be low,
byte SoilmoistureLow;
@ -78,6 +78,10 @@ bool UsePump;
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
//
@ -427,10 +431,10 @@ int getWaterlevel() {
* 0 : OK
*/
int waterlevelWARN = 200;
int waterlevelOK = 400;
int waterlevelRAW = 0;
int waterlevel = 0;
byte waterlevelWARN = 200;
byte waterlevelOK = 400;
byte waterlevelRAW = 0;
byte waterlevel = 0;
// enable Vcc for water level sensor
digitalWrite(PINwaterlevel, HIGH);
@ -453,22 +457,28 @@ int getWaterlevel() {
return waterlevel;
}
float getTemperature(bool tempSensor) {
float getTemperature(byte tempSensor) {
/*
* tempSensor
* ==========
* 0/false : DHT11 temp sensor
* 1/true : chirp I2C temp sensor
* 1 : DHT11 temp sensor
* 2 : chirp I2C temp sensor
*/
float temperature = 0;
if(tempSensor == false ) {
// read temperature from DHT11
temperature = dht.readTemperature();
} else {
// read temperature from chrip I2C
temperature = readI2CRegister16bit(0x20, 5) * 0.10 ;
switch(tempSensor) {
case 1:
// read temperature from DHT11
temperature = dht.readTemperature();
break;
case 2:
// read temperature from chrip I2C
temperature = readI2CRegister16bit(0x20, 5) * 0.10 ;
break;
default:
// if sensor type is not recognized, return 99
temperature = 99.99;
}
return temperature;
@ -483,8 +493,8 @@ int getSoilmoisture(byte moistureSensor) {
/*
* moistureSensor
* ==============
* 0 : analog capacitive moisture sensor
* 1 : chirp I2C moisture sensor
* 1 : analog capacitive moisture sensor
* 2 : chirp I2C moisture sensor
*/
// value to return
@ -494,25 +504,32 @@ int getSoilmoisture(byte moistureSensor) {
// value for dry
int dry;
if(moistureSensor == 0 ) {
// read analog value from analog moisture sensor
wet = 180;
dry= 590;
digitalWrite(PINsoilmoisture, HIGH);
// wait a bit to let the circuit stabilize
delay(100);
// get analog input value
soilmoisture = analogRead(PINanalog);
// disable Vcc for the sensor to release analog pin
digitalWrite(PINsoilmoisture, LOW);
} else {
// read soil moisture from chrip I2C
wet = 560;
dry= 250;
// get raw value from I2C chirp sensor
soilmoisture = readI2CRegister16bit(0x20, 0);
switch(moistureSensor) {
case 1:
// read analog value from analog moisture sensor
wet = 180;
dry= 590;
digitalWrite(PINsoilmoisture, HIGH);
// wait a bit to let the circuit stabilize
delay(100);
// get analog input value
soilmoisture = analogRead(PINanalog);
// disable Vcc for the sensor to release analog pin
digitalWrite(PINsoilmoisture, LOW);
break;
case 2:
// read soil moisture from chrip I2C
wet = 560;
dry= 250;
// get raw value from I2C chirp sensor
soilmoisture = readI2CRegister16bit(0x20, 0);
break;
default:
wet = 0;
dry = 1;
soilmoisture = -1;
}
return map(soilmoisture, wet, dry, 100, 0);
@ -942,7 +959,25 @@ void WebAuthApi() {
}
}
/*
* returnSelected(bool)
* returns char[] "selected" if bool is true
* useful for html forms, to preset a saved value as selected
*/
String returnStrSelected(byte savedValue, byte selectId) {
String returnStr;
if(configured == true) {
if(savedValue == selectId) {
returnStr = "selected";
} else {
returnStr = "";
}
}
return returnStr;
}
/*
*
* Web pages
@ -1020,11 +1055,8 @@ void WEBwifiSettings() {
for(int i = 0 ; i < ssidsAvail; i++) {
String wifiName = WiFi.SSID(i);
Serial.println(wifiName);
body += "<option value='";
body += wifiName;
body += "'>";
body += wifiName;
body += "</option>\n";
body += "<option value='" + wifiName + "'>";
body += wifiName + "</option>\n";
}
body += "</select><br>\n";
body += "Password: <input type='password' name='WIFIpassword'><br>\n";
@ -1055,48 +1087,48 @@ void WEBsystemSettings() {
body += "<p>here you can set which features and sensors you use<br>";
body += "</p>";
/*
* // size is 1 byte
EEPROM.put(162, UseFan);
// size is 1 byte
EEPROM.put(163, UsePump);
// size is 1 byte
EEPROM.put(164, PumpOnTime);
// size is 1 byte
EEPROM.put(165, MoistureSensor_Type);
// size is 1 byte
EEPROM.put(166, SoilmoistureLow);
// size is 2 byte
EEPROM.put(167, ntpOffset);
* */
body += "<form method='post' action='/systemSettings/save'>\n";
body += "Use fan: <select id='UseFan' name='UseFan' required>\n";
body += "<option disabled value='' selected hidden>---</option>\n";
body += "<option value='1'>Yes</option>\n";
body += "<option value='0'>No</option>\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";
body += "Use pump: <select id='UsePump' name='UsePump' required>\n";
body += "<option disabled value='' selected hidden>---</option>\n";
body += "<option value='1'>Yes</option>\n";
body += "<option value='0'>No</option>\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 += "Pump on time: <input type='text' name='PumpOnTime' required><br>\n";
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='";
body += PumpOnTime;
body += "'required><br>\n";
body += "Moisture sensor type: <select id='MoistureSensor_Type' name='MoistureSensor_Type' required>\n";
body += "<option disabled value='' selected hidden>---</option>\n";
body += "<option value='0'>Analog capacitive</option>\n";
body += "<option value='1'>I2C chirp</option>\n";
if(configured == false) {
body += "<option disabled value='' selected hidden>---</option>\n";
}
body += "<option value='1'" + returnStrSelected(MoistureSensor_Type, 1) + ">Analog capacitive</option>\n";
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' required><br>\n";
body += "Soil moisture low: <input type='text' name='SoilmoistureLow' value='";
body += SoilmoistureLow;
body += "' required><br>\n";
body += "NTP offset: <input type='text' name='ntpOffset' required><br>\n";
body += "NTP offset: <input type='text' name='ntpOffset' value='";
body += ntpOffset;
body+= "' required><br>\n";
body += "<input type='submit' value='Save'>\n";
body += "</form>\n";
@ -1128,8 +1160,7 @@ 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 += "'/>";
body += PINled_PWM + "'/>";
body += "<input type='submit' value='Save'>\n";
body += "</form>\n";
@ -1184,6 +1215,8 @@ void POSTsystemSettings() {
EEPROM.put(166, SoilmoistureLow);
// size is 2 byte
EEPROM.put(167, ntpOffset);
// size is 1 byte
EEPROM.put(169, UseLEDrelais);
EEPROM.commit();