| 
									
										
										
										
											2020-11-10 17:38:33 +01:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Created by lukas on 10.11.20.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Heating.h"
 | 
					
						
							|  |  |  | #include "Pins.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:26:17 +01:00
										 |  |  | void Heating::init(Temperature *tempsensor, unsigned mode) { | 
					
						
							|  |  |  |     switch (mode) { | 
					
						
							|  |  |  |         case TIME: { | 
					
						
							|  |  |  |             const unsigned percentOn = 20; | 
					
						
							| 
									
										
										
										
											2020-11-17 18:37:47 +01:00
										 |  |  |             const unsigned refreshperiod = 900; | 
					
						
							| 
									
										
										
										
											2020-11-17 15:26:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             const auto func = [this]() { | 
					
						
							| 
									
										
										
										
											2020-11-10 17:38:33 +01:00
										 |  |  |                 mLuefterTicker.detach(); | 
					
						
							| 
									
										
										
										
											2020-11-17 15:26:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 // every minute turn on the heating
 | 
					
						
							| 
									
										
										
										
											2020-11-10 17:38:33 +01:00
										 |  |  |                 digitalWrite(LuefterPin, HIGH); | 
					
						
							|  |  |  |                 digitalWrite(HeizungPin, HIGH); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:26:17 +01:00
										 |  |  |                 Serial.println("Turning on heating"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-30 11:14:08 +01:00
										 |  |  |                 mTurnOffTicker.once((int) ((float) refreshperiod * (float) percentOn / 100.0), []() { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:26:17 +01:00
										 |  |  |                     digitalWrite(HeizungPin, LOW); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     Serial.println("Turned off heating!"); | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-30 11:14:08 +01:00
										 |  |  |                 mLuefterTicker.once((int) (((float) refreshperiod * (float) percentOn / 100.0) + 30.0), []() { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:26:17 +01:00
										 |  |  |                     digitalWrite(LuefterPin, LOW); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     Serial.println("Turned off fan!"); | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             func(); | 
					
						
							| 
									
										
										
										
											2020-11-17 18:37:47 +01:00
										 |  |  |             mHeizungTicker.attach(refreshperiod, func); | 
					
						
							| 
									
										
										
										
											2020-11-17 15:26:17 +01:00
										 |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         case HUMIDITY: | 
					
						
							| 
									
										
										
										
											2020-11-30 11:14:08 +01:00
										 |  |  |             mHeatingStatus = false; | 
					
						
							| 
									
										
										
										
											2020-11-17 15:26:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             mHeizungTicker.attach(10, [&tempsensor, this]() { | 
					
						
							|  |  |  |                 // shedule this function because tempread is blocking
 | 
					
						
							|  |  |  |                 schedule_function([this, &tempsensor]() { | 
					
						
							|  |  |  |                     // check temperature
 | 
					
						
							|  |  |  |                     Serial.println("checking humidity"); | 
					
						
							|  |  |  |                     const float hum = tempsensor->getHum(); | 
					
						
							|  |  |  |                     if (hum == -1) return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     Serial.print("humidity read: "); | 
					
						
							|  |  |  |                     Serial.println(hum); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if (hum > 65.0) { | 
					
						
							|  |  |  |                         // turn off active turnoff timers
 | 
					
						
							|  |  |  |                         mLuefterTicker.detach(); | 
					
						
							|  |  |  |                         Serial.println("heating should run now!"); | 
					
						
							|  |  |  |                         // turn on heating and fan
 | 
					
						
							|  |  |  |                         digitalWrite(LuefterPin, HIGH); | 
					
						
							|  |  |  |                         digitalWrite(HeizungPin, HIGH); | 
					
						
							| 
									
										
										
										
											2020-11-30 11:14:08 +01:00
										 |  |  |                         mHeatingStatus = true; | 
					
						
							| 
									
										
										
										
											2020-11-17 15:26:17 +01:00
										 |  |  |                     } else if (hum < 60.0) { | 
					
						
							|  |  |  |                         // if humidity too low turn off heating and fan after 60secs
 | 
					
						
							|  |  |  |                         digitalWrite(HeizungPin, LOW); | 
					
						
							|  |  |  |                         Serial.println("heating should NOT run now!"); | 
					
						
							| 
									
										
										
										
											2020-11-30 11:14:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |                         // if heating status in on set ticker to turn of fan in 60sec
 | 
					
						
							|  |  |  |                         if (mHeatingStatus) { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:26:17 +01:00
										 |  |  |                             mLuefterTicker.once(60, []() { | 
					
						
							|  |  |  |                                 // turn off fan
 | 
					
						
							|  |  |  |                                 digitalWrite(LuefterPin, LOW); | 
					
						
							|  |  |  |                                 Serial.println("turning off fan"); | 
					
						
							|  |  |  |                             }); | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2020-11-30 11:14:08 +01:00
										 |  |  |                         mHeatingStatus = false; | 
					
						
							| 
									
										
										
										
											2020-11-17 15:26:17 +01:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-11-10 17:38:33 +01:00
										 |  |  | } |