firmware wip - wip pump control

This commit is contained in:
Marcus 2024-06-17 01:48:58 +02:00
parent a51cd81651
commit 37e0b33cf3
4 changed files with 57 additions and 22 deletions

View file

@ -43,6 +43,11 @@ unsigned long MaintenanceStarted = 0;
byte PumpOnTimePassed = 0; byte PumpOnTimePassed = 0;
bool PumpOnManual = false; bool PumpOnManual = false;
// helper variable for pump control with soilmoisture
// average of last readings
unsigned short valSoilmoistureAvg = 0;
unsigned short valSoilmoistureAvg_tmp = 0;
byte valSoilmoistureAvg_count = 0;
//unsigned short //unsigned short
/* /*

View file

@ -501,10 +501,25 @@ void controlLED() {
} }
void refreshSensors() { void refreshSensors() {
byte soilmoistureAvgSampleCount = 5;
valSoilmoisture = getSoilmoisture(MoistureSensor_Type); valSoilmoisture = getSoilmoisture(MoistureSensor_Type);
valHumidity = getHumidity(); valHumidity = getHumidity();
valTemperature = getTemperature(TemperatureSensor_Type); valTemperature = getTemperature(TemperatureSensor_Type);
valWaterlevel = getWaterlevel(); valWaterlevel = getWaterlevel();
// get average of 5 readings for valSoilmoisture
valSoilmoistureAvg_tmp = valSoilmoistureAvg_tmp + valSoilmoisture;
if(valSoilmoistureAvg_count < soilmoistureAvgSampleCount - 1) {
valSoilmoistureAvg_count++;
} else {
// build average
valSoilmoistureAvg = valSoilmoistureAvg_tmp / soilmoistureAvgSampleCount;
// reset everything
valSoilmoistureAvg_tmp = 0;
valSoilmoistureAvg_count = 0;
}
} }
void displayScreens() { void displayScreens() {
@ -553,9 +568,10 @@ void displayScreens() {
display.println(""); display.println("");
display.print("Moisture: "); display.print("Moisture: ");
display.print(valSoilmoisture); display.print(valSoilmoisture);
display.println(" %"); display.print(" % ");
display.println(valSoilmoistureAvg);
display.println(""); display.println("");
if(UsePump == true) { if(UsePump > 0) {
display.print("Pump Waterlvl: "); display.print("Pump Waterlvl: ");
switch(valWaterlevel) { switch(valWaterlevel) {
case 0: case 0:
@ -628,7 +644,8 @@ void controlPUMP() {
byte PumpInterval; byte PumpInterval;
// UsePump true and not in harvest state? // UsePump true and not in harvest state?
if ( (UsePump > 0) && (growState() < 3) ) { // dont water within the first 15 sec after startup
if ( (UsePump > 0) && (growState() < 3) && (millis() > 15000) ) {
switch(growState()) { switch(growState()) {
case 1: case 1:
PumpInterval = PumpIntervalVeg; PumpInterval = PumpIntervalVeg;
@ -674,8 +691,8 @@ void controlPUMP() {
break; break;
case 2: case 2:
// when valSoilmoisture is lower then SoilMoisture low do some watering // when valSoilmoistureAvg is lower then SoilMoisture low do some watering
if( (valSoilmoisture < SoilmoistureLow) || ( (valSoilmoisture >= SoilmoistureLow) && ( (PumpOnTimePassed > 0) && (PumpOnTimePassed <= PumpOnTime) ) ) ) { if( (valSoilmoistureAvg < SoilmoistureLow) || ( (valSoilmoistureAvg >= SoilmoistureLow) && ( (PumpOnTimePassed > 0) && (PumpOnTimePassed <= PumpOnTime) ) ) ) {
// check if we alerady exceeded max PumpOnTime // check if we alerady exceeded max PumpOnTime
if(PumpOnTimePassed < PumpOnTime) { if(PumpOnTimePassed < PumpOnTime) {
digitalWrite(PinPUMP, HIGH); digitalWrite(PinPUMP, HIGH);
@ -685,7 +702,7 @@ void controlPUMP() {
PumpLastOn = timeClient.getEpochTime(); PumpLastOn = timeClient.getEpochTime();
PumpOnTimePassed = 0; PumpOnTimePassed = 0;
} }
// when valSoilmoisture is greater then the Low value, // when valSoilmoistureAvg is greater then the Low value,
} else { } else {
digitalWrite(PinPUMP, LOW); digitalWrite(PinPUMP, LOW);
} }
@ -694,8 +711,8 @@ void controlPUMP() {
// //
case 3: case 3:
if( ( (timeClient.getEpochTime() - PumpLastOn) >= (PumpInterval) ) && //TODO calculate PumpInterval into days as well here if( ( (timeClient.getEpochTime() - PumpLastOn) >= (PumpInterval) ) && //TODO calculate PumpInterval into days as well here
( (valSoilmoisture < SoilmoistureLow) || ( (valSoilmoistureAvg < SoilmoistureLow) ||
( (valSoilmoisture >= SoilmoistureLow) && ( (PumpOnTimePassed > 0) && (PumpOnTimePassed <= PumpOnTime) ) ) ( (valSoilmoistureAvg >= SoilmoistureLow) && ( (PumpOnTimePassed > 0) && (PumpOnTimePassed <= PumpOnTime) ) )
) ) { ) ) {
// check if we alerady exceeded max PumpOnTime // check if we alerady exceeded max PumpOnTime
if(PumpOnTimePassed < PumpOnTime) { if(PumpOnTimePassed < PumpOnTime) {
@ -706,7 +723,7 @@ void controlPUMP() {
PumpLastOn = timeClient.getEpochTime(); PumpLastOn = timeClient.getEpochTime();
PumpOnTimePassed = 0; PumpOnTimePassed = 0;
} }
// when valSoilmoisture is greater then the Low value, // when valSoilmoistureAvg is greater then the Low value,
} else { } else {
digitalWrite(PinPUMP, LOW); digitalWrite(PinPUMP, LOW);
} }
@ -714,5 +731,8 @@ void controlPUMP() {
} }
} }
} else {
// ensure pump is off when it should be off
digitalWrite(PinPUMP, LOW);
} }
} }

View file

@ -1,5 +1,5 @@
/* CanGrow_Version.h gets generated from cangrow.sh */ /* CanGrow_Version.h gets generated from cangrow.sh */
const char* CanGrowVer = "0.1-dev"; const char* CanGrowVer = "0.1-dev";
const char* CanGrowBuild = "54fec62-20240616204659"; const char* CanGrowBuild = "a51cd81-20240617013905";

View file

@ -373,7 +373,7 @@ void WEBroot() {
} }
if(UsePump > 0) { if(UsePump > 0) {
body += "<br><b>Pump water level:</b> "; body += "<b>Pump water level:</b> ";
switch(getWaterlevel()) { switch(getWaterlevel()) {
case 0: case 0:
body += "<span style='color: green;'>OK</span>"; body += "<span style='color: green;'>OK</span>";
@ -515,15 +515,19 @@ void WEBgrowSettings() {
body += "'/> %<br>\n"; body += "'/> %<br>\n";
} }
if(UsePump > 0) {
body += "Pump interval vegetation: <input class='inputShort' type='number' name='PumpIntervalVeg' min='0' max='255' value='"; body += "Pump interval vegetation: every <input class='inputShort' type='number' name='PumpIntervalVeg' min='0' max='255' value='";
body += PumpIntervalVeg; body += PumpIntervalVeg;
body+= "' required>days<br>\n"; body += "' required ";
if( (UsePump != 1) && UsePump != 3) { body += "readonly"; }
body += "Pump interval bloom: <input class='inputShort' type='number' name='PumpIntervalBloom' min='0' max='255' value='"; body += "> days<br>\n";
body += PumpIntervalBloom;
body+= "' required>days<br>\n"; body += "Pump interval bloom: every <input class='inputShort' type='number' name='PumpIntervalBloom' min='0' max='255' value='";
} body += PumpIntervalBloom;
body += "' required ";
if((UsePump != 1) && (UsePump != 3)) { body += "readonly"; }
body += "> days<br>\n";
body += "<input type='submit' value='&#x1F4BE; Save settings'>\n"; body += "<input type='submit' value='&#x1F4BE; Save settings'>\n";
body += "</form>\n"; body += "</form>\n";
@ -1040,6 +1044,7 @@ void APIgetSensors() {
JsonDocument jsonSensors; JsonDocument jsonSensors;
jsonSensors["soilmoisture"] = valSoilmoisture; jsonSensors["soilmoisture"] = valSoilmoisture;
jsonSensors["soilmoistureAvg"] = valSoilmoistureAvg;
jsonSensors["temperature"] = valTemperature; jsonSensors["temperature"] = valTemperature;
jsonSensors["humidity"] = valHumidity; jsonSensors["humidity"] = valHumidity;
jsonSensors["waterlevel"] = valWaterlevel; jsonSensors["waterlevel"] = valWaterlevel;
@ -1054,6 +1059,13 @@ void APIgetDebug() {
JsonDocument jsonDebug; JsonDocument jsonDebug;
// Runtime vars
JsonObject objRuntime = jsonDebug["runtime"].add<JsonObject>();
objRuntime["PumpOnTimePassed"] = PumpOnTimePassed;
objRuntime["PumpOnManual"] = PumpOnManual;
objRuntime["valSoilmoistureAvg"] = valSoilmoistureAvg;
objRuntime["valSoilmoistureAvg_tmp"] = valSoilmoistureAvg_tmp;
objRuntime["valSoilmoistureAvg_count"] = valSoilmoistureAvg_count;
// WiFi // WiFi
JsonObject objWiFi = jsonDebug["wifi"].add<JsonObject>(); JsonObject objWiFi = jsonDebug["wifi"].add<JsonObject>();
@ -1077,8 +1089,6 @@ void APIgetDebug() {
objSystem["NtpOffset"] = NtpOffset; objSystem["NtpOffset"] = NtpOffset;
objSystem["MaintenanceDuration"] = MaintenanceDuration; objSystem["MaintenanceDuration"] = MaintenanceDuration;
objSystem["PumpOnTime"] = PumpOnTime; objSystem["PumpOnTime"] = PumpOnTime;
objSystem["PumpOnTimePassed"] = PumpOnTimePassed;
objSystem["PumpOnManual"] = PumpOnManual;
objSystem["PumpLastOn"] = PumpLastOn; objSystem["PumpLastOn"] = PumpLastOn;
// Grow // Grow