firmware wip - cleanup, put things into functions

This commit is contained in:
Marcus 2024-05-01 00:46:50 +02:00
parent b82faeb38d
commit bd4ee79ed7

View file

@ -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 += "<br>";
body += "Soil Moisture: ";
body += getSoilmoisture(MoistureSensor_Type);
body += valSoilmoisture;
body += " %<br>";
body += "Humidity: ";
body += getHumidity();
body += valHumidity;
body += " %<br>";
body += "Temperature: ";
body += getTemperature(TemperatureSensor_Type);
body += valTemperature;
body += " °C<br>";
if(UsePump == true) {
body += "Pump water level: ";