From 37e0b33cf321593f0eb8565c004359e016b5abe8 Mon Sep 17 00:00:00 2001 From: Marcus Date: Mon, 17 Jun 2024 01:48:58 +0200 Subject: [PATCH] firmware wip - wip pump control --- Arduino/CanGrow/CanGrow_Init.h | 5 ++++ Arduino/CanGrow/CanGrow_SysFunctions.h | 38 ++++++++++++++++++++------ Arduino/CanGrow/CanGrow_Version.h | 2 +- Arduino/CanGrow/CanGrow_WebFunctions.h | 34 +++++++++++++++-------- 4 files changed, 57 insertions(+), 22 deletions(-) diff --git a/Arduino/CanGrow/CanGrow_Init.h b/Arduino/CanGrow/CanGrow_Init.h index d48c1ad..d5929f4 100644 --- a/Arduino/CanGrow/CanGrow_Init.h +++ b/Arduino/CanGrow/CanGrow_Init.h @@ -43,6 +43,11 @@ unsigned long MaintenanceStarted = 0; byte PumpOnTimePassed = 0; 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 /* diff --git a/Arduino/CanGrow/CanGrow_SysFunctions.h b/Arduino/CanGrow/CanGrow_SysFunctions.h index 933e12a..dddcaf6 100644 --- a/Arduino/CanGrow/CanGrow_SysFunctions.h +++ b/Arduino/CanGrow/CanGrow_SysFunctions.h @@ -501,10 +501,25 @@ void controlLED() { } void refreshSensors() { + byte soilmoistureAvgSampleCount = 5; + valSoilmoisture = getSoilmoisture(MoistureSensor_Type); valHumidity = getHumidity(); valTemperature = getTemperature(TemperatureSensor_Type); 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() { @@ -553,9 +568,10 @@ void displayScreens() { display.println(""); display.print("Moisture: "); display.print(valSoilmoisture); - display.println(" %"); + display.print(" % "); + display.println(valSoilmoistureAvg); display.println(""); - if(UsePump == true) { + if(UsePump > 0) { display.print("Pump Waterlvl: "); switch(valWaterlevel) { case 0: @@ -628,7 +644,8 @@ void controlPUMP() { byte PumpInterval; // 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()) { case 1: PumpInterval = PumpIntervalVeg; @@ -674,8 +691,8 @@ void controlPUMP() { break; case 2: - // when valSoilmoisture is lower then SoilMoisture low do some watering - if( (valSoilmoisture < SoilmoistureLow) || ( (valSoilmoisture >= SoilmoistureLow) && ( (PumpOnTimePassed > 0) && (PumpOnTimePassed <= PumpOnTime) ) ) ) { + // when valSoilmoistureAvg is lower then SoilMoisture low do some watering + if( (valSoilmoistureAvg < SoilmoistureLow) || ( (valSoilmoistureAvg >= SoilmoistureLow) && ( (PumpOnTimePassed > 0) && (PumpOnTimePassed <= PumpOnTime) ) ) ) { // check if we alerady exceeded max PumpOnTime if(PumpOnTimePassed < PumpOnTime) { digitalWrite(PinPUMP, HIGH); @@ -685,7 +702,7 @@ void controlPUMP() { PumpLastOn = timeClient.getEpochTime(); PumpOnTimePassed = 0; } - // when valSoilmoisture is greater then the Low value, + // when valSoilmoistureAvg is greater then the Low value, } else { digitalWrite(PinPUMP, LOW); } @@ -694,8 +711,8 @@ void controlPUMP() { // case 3: if( ( (timeClient.getEpochTime() - PumpLastOn) >= (PumpInterval) ) && //TODO calculate PumpInterval into days as well here - ( (valSoilmoisture < SoilmoistureLow) || - ( (valSoilmoisture >= SoilmoistureLow) && ( (PumpOnTimePassed > 0) && (PumpOnTimePassed <= PumpOnTime) ) ) + ( (valSoilmoistureAvg < SoilmoistureLow) || + ( (valSoilmoistureAvg >= SoilmoistureLow) && ( (PumpOnTimePassed > 0) && (PumpOnTimePassed <= PumpOnTime) ) ) ) ) { // check if we alerady exceeded max PumpOnTime if(PumpOnTimePassed < PumpOnTime) { @@ -706,7 +723,7 @@ void controlPUMP() { PumpLastOn = timeClient.getEpochTime(); PumpOnTimePassed = 0; } - // when valSoilmoisture is greater then the Low value, + // when valSoilmoistureAvg is greater then the Low value, } else { digitalWrite(PinPUMP, LOW); } @@ -714,5 +731,8 @@ void controlPUMP() { } } + } else { + // ensure pump is off when it should be off + digitalWrite(PinPUMP, LOW); } } diff --git a/Arduino/CanGrow/CanGrow_Version.h b/Arduino/CanGrow/CanGrow_Version.h index 33c763b..cd3b8ad 100644 --- a/Arduino/CanGrow/CanGrow_Version.h +++ b/Arduino/CanGrow/CanGrow_Version.h @@ -1,5 +1,5 @@ /* CanGrow_Version.h gets generated from cangrow.sh */ const char* CanGrowVer = "0.1-dev"; -const char* CanGrowBuild = "54fec62-20240616204659"; +const char* CanGrowBuild = "a51cd81-20240617013905"; diff --git a/Arduino/CanGrow/CanGrow_WebFunctions.h b/Arduino/CanGrow/CanGrow_WebFunctions.h index e445453..f35951a 100644 --- a/Arduino/CanGrow/CanGrow_WebFunctions.h +++ b/Arduino/CanGrow/CanGrow_WebFunctions.h @@ -373,7 +373,7 @@ void WEBroot() { } if(UsePump > 0) { - body += "
Pump water level: "; + body += "Pump water level: "; switch(getWaterlevel()) { case 0: body += "OK"; @@ -515,15 +515,19 @@ void WEBgrowSettings() { body += "'/> %
\n"; } - if(UsePump > 0) { - body += "Pump interval vegetation: days
\n"; - - body += "Pump interval bloom: days
\n"; - } + + body += "Pump interval vegetation: every (); + objRuntime["PumpOnTimePassed"] = PumpOnTimePassed; + objRuntime["PumpOnManual"] = PumpOnManual; + objRuntime["valSoilmoistureAvg"] = valSoilmoistureAvg; + objRuntime["valSoilmoistureAvg_tmp"] = valSoilmoistureAvg_tmp; + objRuntime["valSoilmoistureAvg_count"] = valSoilmoistureAvg_count; // WiFi JsonObject objWiFi = jsonDebug["wifi"].add(); @@ -1077,8 +1089,6 @@ void APIgetDebug() { objSystem["NtpOffset"] = NtpOffset; objSystem["MaintenanceDuration"] = MaintenanceDuration; objSystem["PumpOnTime"] = PumpOnTime; - objSystem["PumpOnTimePassed"] = PumpOnTimePassed; - objSystem["PumpOnManual"] = PumpOnManual; objSystem["PumpLastOn"] = PumpLastOn; // Grow