firmware wip - now fading really works, renamed some stuff

This commit is contained in:
Marcus 2024-04-30 02:31:55 +02:00
parent eb0f91e313
commit f0106cbb9b

View file

@ -960,7 +960,7 @@ void wifiAp() {
//Serial.println("The login credentials for the WebUI are 'cangrow' for username and password"); //Serial.println("The login credentials for the WebUI are 'cangrow' for username and password");
} }
void setOutput(byte pin, byte pinState) { void setOutput(byte Output, byte OutputState) {
/* /*
* Pin assignments * Pin assignments
* *
@ -969,40 +969,46 @@ void setOutput(byte pin, byte pinState) {
* 3 - PUMP * 3 - PUMP
* *
*/ */
bool UseRelais = true;
byte OutputPin;
switch(Output) {
switch(pin) {
case 1: case 1:
if(pinState > 0) { OutputPin = PINled;
if(UseLEDrelais == true) { if(UseLEDrelais == true) {
digitalWrite(PINled, HIGH); UseRelais = true;
} else {
analogWrite(PINled, PINledPWM);
}
} else { } else {
digitalWrite(PINled, LOW); UseRelais = false;
} }
break; break;
case 2: case 2:
if(pinState > 0) { OutputPin = PINfan;
if(UseFANrelais == true) { if(UseFANrelais == true) {
digitalWrite(PINfan, HIGH); UseRelais = true;
} else {
analogWrite(PINfan, PINledPWM);
}
} else { } else {
digitalWrite(PINfan, LOW); UseRelais = false;
} }
break; break;
// PUMP Pin (D0) does not support PWM, so we do not need to care about // PUMP Pin (D0) does not support PWM, so we do not need to care about
case 3: case 3:
if(pinState > 0) { OutputPin = PINpump;
digitalWrite(PINpump, HIGH);
} else {
digitalWrite(PINpump, LOW);
}
break; break;
} }
//~ Serial.print("Output: ");
//~ Serial.println(Output);
//~ Serial.print("OutputPin: ");
//~ Serial.println(OutputPin);
//~ Serial.print("OutputState: ");
//~ Serial.println(OutputState);
//~ Serial.print("UseRelais: ");
//~ Serial.println(UseRelais);
if( (UseRelais == true) || (OutputPin == PINpump) ) {
digitalWrite(OutputPin, OutputState);
} else {
analogWrite(OutputPin, OutputState);
}
} }
/* /*
@ -1148,9 +1154,6 @@ void loop() {
// do every second when everything is configured and grow is started // do every second when everything is configured and grow is started
if( (configured == true) && (strlen(GrowName) > 0) && (currentRuntime - outputPrevTime >= 1000) ){ if( (configured == true) && (strlen(GrowName) > 0) && (currentRuntime - outputPrevTime >= 1000) ){
/*
* LED controlling
*/
// calculate acutal DayOfGrow // calculate acutal DayOfGrow
DayOfGrow = int(ceil(float((timeClient.getEpochTime() - GrowStart) / 60 / 60 / 24))); DayOfGrow = int(ceil(float((timeClient.getEpochTime() - GrowStart) / 60 / 60 / 24)));
@ -1162,31 +1165,63 @@ void loop() {
lightHours = LighthoursVeg; lightHours = LighthoursVeg;
} }
/*
* LED controlling
*/
// check if secondsToday is larger then secondsSunrise time AND if // check if secondsToday is larger then secondsSunrise time AND if
// secondsToday is smaller then the sum of secondsSunrise + seconds of lightHours // secondsToday is smaller then the sum of secondsSunrise + seconds of lightHours
if( (SunFade == true) && ((secondsToday >= secondsSunrise) && (secondsToday <= ( secondsSunrise + (lightHours * 60 * 60))) ) ){ if( ((secondsToday >= secondsSunrise) && (secondsToday <= ( secondsSunrise + (lightHours * 60 * 60))) ) ){
Serial.println("light on time");
// in the first n minutes of lighting (SunFadeDuration), we want
//to raise the light slowly to prevent stress from the plant // when SunFade is true, fade LED light. Otherwise just turn on or off
if(secondsSunrise + SunFadeDuration * 60 >= secondsToday) { 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 // convert progress sunrise to PWM value
PINledPWM_tmp = (SunFadeDuration * 60 - ((secondsSunrise + SunFadeDuration * 60) - secondsToday)) * 255 / (SunFadeDuration * 60); 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) && (secondsToday >= ((secondsSunrise + lightHours * 60 * 60) - SunFadeDuration * 60) ) ) { } else if( (SunFade == true) && (UseLEDrelais == false) && (secondsToday >= ((secondsSunrise + lightHours * 60 * 60) - SunFadeDuration * 60) ) ) {
// calculate progress sunset to PWM value // calculate progress sunset to PWM value
PINledPWM_tmp = (secondsSunrise + (lightHours * 60 * 60) - secondsToday) * 255 / (SunFadeDuration * 60); PINledPWM_tmp = (secondsSunrise + (lightHours * 60 * 60) - secondsToday) * PINledPWM / (SunFadeDuration * 60);
setOutput(1, PINledPWM_tmp);
Serial.print("sunset PWM: ");
Serial.println(PINledPWM_tmp);
} else { } else {
Serial.println("just turn on the light");
// no sunrise or sunset, just keep the LED turned on // no sunrise or sunset, just keep the LED turned on
setOutput(1, PINledPWM); setOutput(1, PINledPWM);
} }
} else { } else {
Serial.println("good night time");
// turn off // turn off
setOutput(1, 0); setOutput(1, 0);
} }
// current time gets previous time for new interval // 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; outputPrevTime = currentRuntime;
} }
@ -1506,8 +1541,8 @@ void WEBroot() {
body += "MOSFET<select id='output' name='output' required>\n"; body += "MOSFET<select id='output' name='output' required>\n";
body += "<option disabled value='' selected hidden>---</option>\n"; body += "<option disabled value='' selected hidden>---</option>\n";
body += "<option value='1'>LED</option>\n"; body += "<option value='1'>LED</option>\n";
body += "<option value='2'>PUMP</option>\n"; body += "<option value='2'>FAN</option>\n";
body += "<option value='3'>FAN</option>\n"; body += "<option value='3'>PUMP</option>\n";
body += "</select><br>"; body += "</select><br>";
body += "On/Off: <select id='state' name='state' required>\n"; body += "On/Off: <select id='state' name='state' required>\n";
@ -1517,9 +1552,7 @@ void WEBroot() {
body += "</select><br>\n"; body += "</select><br>\n";
if(UseFANrelais == false) { if(UseFANrelais == false) {
body += "Speed FAN: <input type='range' id='PINfanPWM' name='PINfanPWM' min='1' max='255' value='"; body += "Intensity: <input type='range' id='OutputPWM' name='OutputPWM' min='1' max='255' value='255'/><br>\n";
body += PINfanPWM;
body += "'/><br>\n";
} }
body += "<input type='submit' value='Save'>\n"; body += "<input type='submit' value='Save'>\n";
@ -1776,7 +1809,7 @@ void WEBgrowSettings() {
body += "<option value='0'" + returnStrSelected(SunFade, 0) + ">No</option>\n"; body += "<option value='0'" + returnStrSelected(SunFade, 0) + ">No</option>\n";
body += "</select><br>\n"; body += "</select><br>\n";
body += "Fading duration (minutes): <input type='number' name='SunFadeDuration' min='0' max='255' value='"; body += "Fading duration (minutes): <input type='number' name='SunFadeDuration' min='1' max='255' value='";
body += SunFadeDuration; body += SunFadeDuration;
body+= "' required><br>\n"; body+= "' required><br>\n";
@ -2055,20 +2088,25 @@ void POSTwifiSettings() {
void POSTsetOutput() { void POSTsetOutput() {
byte OutputState = webserver.arg("state").toInt(); byte OutputState = webserver.arg("state").toInt();
byte OutputNr = webserver.arg("output").toInt(); byte OutputNr = webserver.arg("output").toInt();
PINledPWM = webserver.arg("PINledPWM").toInt(); //PINledPWM = webserver.arg("PINledPWM").toInt();
PINfanPWM = webserver.arg("PINfanPWM").toInt(); byte OutputPWM = webserver.arg("OutputPWM").toInt();
Serial.println(":: GETswitchMOSFET ::"); Serial.println(":: POSTsetOutput ::");
Serial.print("OutputState: "); Serial.print("OutputState: ");
Serial.println(OutputState); Serial.println(OutputState);
Serial.print("OutputNr: "); Serial.print("OutputNr: ");
Serial.println(OutputNr); Serial.println(OutputNr);
if((OutputNr > 3) || (OutputNr < 1) || (OutputState > 255) || (OutputState < 255)) { if((OutputNr > 3) || (OutputNr < 1) || (OutputState > 255) || (OutputState < 0)) {
webserver.send(400, "text/plain", "not valid\n"); webserver.send(400, "text/plain", "not valid\n");
} else { } else {
setOutput(OutputNr, OutputState); if(OutputState > 0){
setOutput(OutputNr, OutputPWM);
} else {
setOutput(OutputNr, 0);
}
webserver.sendHeader("Location", String("/?success"), true); webserver.sendHeader("Location", String("/?success"), true);
webserver.send(302, "text/plain", "switch: success!\n"); webserver.send(302, "text/plain", "switch: success!\n");
} }