142 lines
4.3 KiB
C++
142 lines
4.3 KiB
C++
//
|
|
// Created by lukas on 10.11.20.
|
|
//
|
|
|
|
#include "Heating.h"
|
|
#include "Pins.h"
|
|
#include "Timer.h"
|
|
|
|
Heating::Heating() : mHeizungTicker(),
|
|
mLuefterTicker(),
|
|
mTurnOffTicker(),
|
|
msettings(nullptr),
|
|
mheatingservice(nullptr),
|
|
mHeatingStatus(false),
|
|
lasttemp(0),
|
|
lasthum(0),
|
|
sensor() {}
|
|
|
|
void Heating::init(unsigned mode, const SettingState *settings, HeatingInfoService *heatingservice) {
|
|
msettings = settings;
|
|
mheatingservice = heatingservice;
|
|
|
|
switch (mode) {
|
|
case TIME: {
|
|
this->handleTimeHeater();
|
|
break;
|
|
}
|
|
case HUMIDITY:
|
|
mHeatingStatus = false;
|
|
|
|
sensor.setPin(TempSensorPin);
|
|
|
|
sensor.onData([this](float hum, float temp) {
|
|
schedule_function([hum, temp, this]() {
|
|
Serial.printf("Temp: %gdegC\n", temp);
|
|
Serial.printf("Humid: %g%%\n", hum);
|
|
|
|
mheatingservice->setSensorData(hum, temp);
|
|
|
|
this->lasttemp = temp;
|
|
this->lasthum = hum;
|
|
|
|
this->handleHeatingEvents();
|
|
});
|
|
});
|
|
|
|
sensor.onError([this](uint8_t e) {
|
|
this->lasttemp = -1.0;
|
|
this->lasthum = -1.0;
|
|
|
|
mheatingservice->setSensorData(-1.0, -1.0);
|
|
|
|
// emergency turnoff of heating
|
|
if (mHeatingStatus) {
|
|
digitalWrite(HeizungPin, LOW);
|
|
}
|
|
|
|
schedule_function([e]() {
|
|
Serial.printf("Error: %d\n", e);
|
|
});
|
|
});
|
|
|
|
mHeizungTicker.attach(10, [this]() {
|
|
sensor.read();
|
|
});
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void Heating::handleHeatingEvents() {
|
|
if (this->lasthum > (float) msettings->heatUp) {
|
|
// only turn on if turned off
|
|
if (mHeatingStatus == false) {
|
|
// 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;
|
|
mheatingservice->setLastHeatingStarttime(Timer::getSystemSeconds());
|
|
|
|
mheatingservice->setLastHetingEndtime(0);
|
|
}
|
|
} else if (this->lasthum < (float) msettings->heatLow) {
|
|
// if humidity too low turn off heating and fan after 60secs
|
|
if (mHeatingStatus == true) {
|
|
digitalWrite(HeizungPin, LOW);
|
|
Serial.println("heating should NOT run now!");
|
|
// add the heat time it took to the total
|
|
mheatingservice->addHeatTime(Timer::getSystemSeconds() - mheatingservice->getLastHeatingStartTime());
|
|
|
|
// if heating status in on set ticker to turn of fan in 60sec
|
|
if (mHeatingStatus) {
|
|
mLuefterTicker.once((float) msettings->fanRuntime, []() {
|
|
// turn off fan
|
|
digitalWrite(LuefterPin, LOW);
|
|
schedule_function([]() {
|
|
Serial.println("turning off fan");
|
|
});
|
|
});
|
|
}
|
|
mheatingservice->setLastHetingEndtime(Timer::getSystemSeconds());
|
|
mHeatingStatus = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Heating::handleTimeHeater() {
|
|
const unsigned percentOn = 20;
|
|
const unsigned refreshperiod = 900;
|
|
|
|
const auto func = [this]() {
|
|
mLuefterTicker.detach();
|
|
|
|
// every minute turn on the heating
|
|
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();
|
|
mHeizungTicker.attach(refreshperiod, func);
|
|
}
|