PumpenSteuerung/src/Heating.cpp

116 lines
3.7 KiB
C++
Raw Normal View History

2020-11-10 17:38:33 +01:00
//
// Created by lukas on 10.11.20.
//
#include "Heating.h"
#include "Pins.h"
void Heating::init(unsigned mode, const SettingState* settings) {
switch (mode) {
case TIME: {
const unsigned percentOn = 20;
2020-11-17 18:37:47 +01:00
const unsigned refreshperiod = 900;
const auto func = [this]() {
2020-11-10 17:38:33 +01:00
mLuefterTicker.detach();
// every minute turn on the heating
2020-11-10 17:38:33 +01:00
digitalWrite(LuefterPin, HIGH);
digitalWrite(HeizungPin, HIGH);
Serial.println("Turning on heating");
mTurnOffTicker.once((int) ((float) refreshperiod * (float) percentOn / 100.0), []() {
digitalWrite(HeizungPin, LOW);
Serial.println("Turned off heating!");
});
mLuefterTicker.once((int) (((float) refreshperiod * (float) percentOn / 100.0) + 30.0), []() {
digitalWrite(LuefterPin, LOW);
Serial.println("Turned off fan!");
});
};
func();
2020-11-17 18:37:47 +01:00
mHeizungTicker.attach(refreshperiod, func);
break;
}
case HUMIDITY:
mHeatingStatus = false;
2020-12-05 12:45:07 +00:00
sensor.setPin(TempSensorPin);
sensor.onData([this, settings](float hum, float temp) {
schedule_function([hum, temp, settings, this]() {
2020-12-05 12:45:07 +00:00
Serial.printf("Temp: %gdegC\n", temp);
Serial.printf("Humid: %g%%\n", hum);
2020-12-05 12:45:07 +00:00
this->lasttemp = temp;
this->lasthum = hum;
if (hum > (float)settings->heatUp) {
// turn off active turnoff timers
mLuefterTicker.detach();
Serial.println("heating should run now!");
// turn on heating and fan
digitalWrite(LuefterPin, HIGH);
digitalWrite(HeizungPin, HIGH);
mHeatingStatus = true;
} else if (hum < (float)settings->heatLow) {
// if humidity too low turn off heating and fan after 60secs
digitalWrite(HeizungPin, LOW);
Serial.println("heating should NOT run now!");
// if heating status in on set ticker to turn of fan in 60sec
if (mHeatingStatus) {
mLuefterTicker.once((float)settings->fanRuntime, []() {
// turn off fan
digitalWrite(LuefterPin, LOW);
2020-12-05 12:45:07 +00:00
schedule_function([]() {
Serial.println("turning off fan");
});
});
}
mHeatingStatus = false;
}
});
});
2020-12-05 12:45:07 +00:00
sensor.onError([this](uint8_t e) {
this->lasttemp = -1.0;
this->lasthum = -1.0;
schedule_function([e]() {
Serial.printf("Error: %d\n", e);
});
});
mHeizungTicker.attach(10, [this]() {
sensor.read();
});
break;
default:
break;
}
2020-11-10 17:38:33 +01:00
}
2020-12-05 12:45:07 +00:00
float *Heating::getLastTemp() {
return &lasttemp;
}
float *Heating::getLastHum() {
return &lasthum;
}
Heating::Heating() : mHeizungTicker(),
mLuefterTicker(),
mTurnOffTicker(),
mHeatingStatus(),
lasttemp(0),
lasthum(0),
sensor() {}