From bd4ee79ed75a3ac26c60bb5ea11fe0600e0f99be Mon Sep 17 00:00:00 2001 From: Marcus Date: Wed, 1 May 2024 00:46:50 +0200 Subject: [PATCH] firmware wip - cleanup, put things into functions --- Arduino/CanGrow/CanGrow.ino | 135 ++++++++++++++++++------------------ 1 file changed, 69 insertions(+), 66 deletions(-) diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index cb88d59..b7d019a 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -62,6 +62,7 @@ float valTemperature; // valTemperature - contains the value of getHumidity() float valHumidity; // valWaterlevel - contains the value of getWaterlevel() +byte valWaterlevel; // do we need a restart? (e.g. after wifi settings change) bool NeedRestart; bool FirstRun; @@ -974,6 +975,64 @@ void setOutput(byte Output, byte OutputState) { } } + +void setGrowLED() { + byte lightHours; + byte PINledPWM_tmp; + unsigned int secondsSunrise = (SunriseHour * 60 * 60) + (SunriseMinute * 60); + unsigned int secondsToday = (timeClient.getHours() * 60 * 60) + (timeClient.getMinutes() * 60) + timeClient.getSeconds(); + + if(DayOfGrow > DaysVeg ) { + lightHours = LighthoursBloom; + } else { + lightHours = LighthoursVeg; + } + + // check if secondsToday is larger then secondsSunrise time AND if + // secondsToday is smaller then the sum of secondsSunrise + seconds of lightHours + if( ((secondsToday >= secondsSunrise) && (secondsToday <= ( secondsSunrise + (lightHours * 60 * 60))) ) ){ + //Serial.println("light on time"); + + // when SunFade is true, fade LED light. Otherwise just turn on or off + if( (SunFade == true) && (UseLEDrelais == false) && (secondsSunrise + SunFadeDuration * 60 >= secondsToday) ) { + // in the first n minutes of lighting (SunFadeDuration), we want + // to raise the light slowly to prevent stress from the plant + // convert progress sunrise to PWM value + PINledPWM_tmp = (SunFadeDuration * 60 - ((secondsSunrise + SunFadeDuration * 60) - secondsToday)) * PINledPWM / (SunFadeDuration * 60); + setOutput(1, PINledPWM_tmp); + //Serial.print("sunrise PWM; "); + //Serial.println(PINledPWM_tmp); + + } else if( (SunFade == true) && (UseLEDrelais == false) && (secondsToday >= ((secondsSunrise + lightHours * 60 * 60) - SunFadeDuration * 60) ) ) { + // calculate progress sunset to PWM value + PINledPWM_tmp = (secondsSunrise + (lightHours * 60 * 60) - secondsToday) * PINledPWM / (SunFadeDuration * 60); + setOutput(1, PINledPWM_tmp); + //Serial.print("sunset PWM: "); + //Serial.println(PINledPWM_tmp); + + } else { + //Serial.println("just turn on the light"); + // no sunrise or sunset, just keep the LED turned on + setOutput(1, PINledPWM); + } + + } else { + //Serial.println("good night time"); + // turn off + setOutput(1, 0); + } +} + +void refreshSensors() { + valSoilmoisture = getSoilmoisture(MoistureSensor_Type); + valHumidity = getHumidity(); + valTemperature = getTemperature(TemperatureSensor_Type); + valWaterlevel = getWaterlevel(); +} + + + + /* * Setup * @@ -1106,85 +1165,29 @@ void setup() { */ void loop() { // var definition - unsigned int secondsSunrise = (SunriseHour * 60 * 60) + (SunriseMinute * 60); - unsigned int secondsToday = (timeClient.getHours() * 60 * 60) + (timeClient.getMinutes() * 60) + timeClient.getSeconds(); unsigned long currentRuntime = millis(); - byte lightHours; - byte PINledPWM_tmp; + // first we call webserver handle client webserver.handleClient(); + // do every second when everything is configured and grow is started if( (configured == true) && (strlen(GrowName) > 0) && (currentRuntime - outputPrevTime >= 1000) ){ + // refresh all sensor values + refreshSensors(); + // calculate acutal DayOfGrow DayOfGrow = int(ceil(float((timeClient.getEpochTime() - GrowStart) / 60 / 60 / 24))); // decide if we are in Veg or Bloom phase of grow // when DayOfGrow is larger then DaysVeg we must be in Bloom - if(DayOfGrow > DaysVeg ) { - lightHours = LighthoursBloom; - } else { - lightHours = LighthoursVeg; - } + // set the actual state of the Grow LED + setGrowLED(); - /* - * LED controlling - */ - - // check if secondsToday is larger then secondsSunrise time AND if - // secondsToday is smaller then the sum of secondsSunrise + seconds of lightHours - if( ((secondsToday >= secondsSunrise) && (secondsToday <= ( secondsSunrise + (lightHours * 60 * 60))) ) ){ - Serial.println("light on time"); - - // when SunFade is true, fade LED light. Otherwise just turn on or off - if( (SunFade == true) && (UseLEDrelais == false) && (secondsSunrise + SunFadeDuration * 60 >= secondsToday) ) { - // in the first n minutes of lighting (SunFadeDuration), we want - // to raise the light slowly to prevent stress from the plant - // convert progress sunrise to PWM value - PINledPWM_tmp = (SunFadeDuration * 60 - ((secondsSunrise + SunFadeDuration * 60) - secondsToday)) * PINledPWM / (SunFadeDuration * 60); - setOutput(1, PINledPWM_tmp); - Serial.print("sunrise PWM; "); - Serial.println(PINledPWM_tmp); - - } else if( (SunFade == true) && (UseLEDrelais == false) && (secondsToday >= ((secondsSunrise + lightHours * 60 * 60) - SunFadeDuration * 60) ) ) { - // calculate progress sunset to PWM value - PINledPWM_tmp = (secondsSunrise + (lightHours * 60 * 60) - secondsToday) * PINledPWM / (SunFadeDuration * 60); - setOutput(1, PINledPWM_tmp); - Serial.print("sunset PWM: "); - Serial.println(PINledPWM_tmp); - - } else { - Serial.println("just turn on the light"); - // no sunrise or sunset, just keep the LED turned on - setOutput(1, PINledPWM); - } - - } else { - Serial.println("good night time"); - // turn off - setOutput(1, 0); - } // current time gets previous time for new interval - - - - /* ------------------------------- - // check if secondsToday is larger then secondsSunrise time AND if secondsToday is smaller then the sum of secondsSunrise + seconds of lightHours - if( (secondsToday >= secondsSunrise) && (secondsToday <= ( secondsSunrise + (lightHours * 60 * 60) ) )){ - // turn on light - - - // TODO write a LED control function which takes also takes care of UseLEDrelais - //analogWrite(PINled, PINledPWM); - setOutput(1, PINledPWM); - } else { - // turn off - digitalWrite(PINled, LOW); - } - ---------------------------------- */ outputPrevTime = currentRuntime; } @@ -1499,13 +1502,13 @@ void WEBroot() { body += DayOfGrow; body += "
"; body += "Soil Moisture: "; - body += getSoilmoisture(MoistureSensor_Type); + body += valSoilmoisture; body += " %
"; body += "Humidity: "; - body += getHumidity(); + body += valHumidity; body += " %
"; body += "Temperature: "; - body += getTemperature(TemperatureSensor_Type); + body += valTemperature; body += " °C
"; if(UsePump == true) { body += "Pump water level: ";