diff --git a/src/Heating.cpp b/src/Heating.cpp new file mode 100644 index 0000000..051b6e2 --- /dev/null +++ b/src/Heating.cpp @@ -0,0 +1,50 @@ +// +// Created by lukas on 10.11.20. +// + +#include "Heating.h" +#include "Pins.h" + +void Heating::init(Temperature *tempsensor) { + mFanWaiting = false; + + 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) { + // turn off active turnoff timers + mLuefterTicker.detach(); + + // turn on heating and fan + digitalWrite(LuefterPin, HIGH); + digitalWrite(HeizungPin, HIGH); + + // if fan waiting detach its ticker + if (mFanWaiting) { + mLuefterTicker.detach(); + mFanWaiting = false; + } + } else if (hum < 60) { + // if humidity too low turn off heating and fan after 60secs + digitalWrite(HeizungPin, LOW); + + if (!mFanWaiting) { + mFanWaiting = true; + mLuefterTicker.once(60, []() { + // turn off fan + digitalWrite(LuefterPin, LOW); + Serial.println("turning off fan"); + }); + } + } + }); + }); +} diff --git a/src/Heating.h b/src/Heating.h new file mode 100644 index 0000000..7bacbe8 --- /dev/null +++ b/src/Heating.h @@ -0,0 +1,18 @@ +// +// Created by lukas on 10.11.20. +// + +#pragma once + +#include +#include "Temperature.h" + +class Heating { +public: + void init(Temperature* tempsensor); +private: + Ticker mHeizungTicker; + Ticker mLuefterTicker; + + bool mFanWaiting; +}; diff --git a/src/Pins.h b/src/Pins.h new file mode 100644 index 0000000..9dd3fe8 --- /dev/null +++ b/src/Pins.h @@ -0,0 +1,18 @@ +// +// Created by lukas on 10.11.20. +// + +#ifndef PUMPENSTEUERUNG_PINS_H +#define PUMPENSTEUERUNG_PINS_H + +#include + +/** pin config */ +#define WasserSensorPin D5 // GPIO 14 +#define DruckSensorPin D6 // GPIO 12 +#define SchuetzPin D2 // GPIO 4 +#define LuefterPin D3 // GPIO 0 +#define HeizungPin D1 +#define TempSensorPin D4 + +#endif //PUMPENSTEUERUNG_PINS_H diff --git a/src/Temperature.h b/src/Temperature.h index a657a87..9c1bfc2 100644 --- a/src/Temperature.h +++ b/src/Temperature.h @@ -6,10 +6,11 @@ #include #include +#include "Pins.h" class Temperature { public: - Temperature() : dht(2, DHT22){}; + Temperature() : dht(TempSensorPin, DHT22){}; /** * initialize the temperature sensor diff --git a/src/WifiManager.cpp b/src/WifiManager.cpp index c9e8ec7..7173347 100644 --- a/src/WifiManager.cpp +++ b/src/WifiManager.cpp @@ -5,7 +5,7 @@ #include #include "WifiManager.h" -void WifiManager::init() { +void WifiManager::init(Temperature* tempsensor) { Serial.print("Setting up Access Point"); // start softap const bool result = WiFi.softAP("PumpenSteuerung-Heiligenbrunner", "1qayxsw2"); @@ -32,7 +32,7 @@ void WifiManager::init() { } // init temperature sensor - temp.init(); + temp = *tempsensor; } WifiManager::WifiManager() : server(80), lastPumpTime(0), lastWaterOutage(0), lastPumpDuration(0) {} @@ -170,3 +170,7 @@ void WifiManager::setlastWaterOutage(unsigned long lastWaterOutage) { void WifiManager::setPumpDuration(unsigned long lastPumpDuration) { this->lastPumpDuration = lastPumpDuration; } + +Temperature *WifiManager::getTemperatureSensor() { + return &temp; +} diff --git a/src/WifiManager.h b/src/WifiManager.h index 2201d24..f5127af 100644 --- a/src/WifiManager.h +++ b/src/WifiManager.h @@ -16,7 +16,7 @@ public: /** * initialize the webserver */ - void init(); + void init(Temperature* tempsensor); /** * handles new web requests and holds the webserver alive @@ -48,6 +48,8 @@ public: */ void setPumpDuration(unsigned long lastPumpDuration); + Temperature* getTemperatureSensor(); + private: // todo more dynamic static const uint8_t WasserSensorPin = 14; diff --git a/src/main.cpp b/src/main.cpp index f66cc1a..a6e799f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,18 +3,17 @@ #include #include "WifiManager.h" +#include "Heating.h" + +#include "Pins.h" +#include "Temperature.h" #define VERSION "v1.1" bool allow; bool error = false; -/** pin config */ -static const uint8_t WasserSensorPin = D5; // GPIO 14 -static const uint8_t DruckSensorPin = D6; // GPIO 12 -static const uint8_t SchuetzPin = D2; // GPIO 4 -static const uint8_t LuefterPin = D3; // GPIO 0 -static const uint8_t HeizungPin = D1; + /** time config */ static const int maxpumpdauer = 600; //sek @@ -25,10 +24,9 @@ Ticker status; //pumpendauer maximum ticker Ticker pumpendauer; -Ticker heizung; -Ticker luefter; - WifiManager mang; +Heating mHeat; +Temperature temp; long turnontime = -1; @@ -179,7 +177,11 @@ void setup() { // initialize wifi - mang.init(); + temp.init(); + + mang.init(&temp); + + mHeat.init(&temp); Serial.println("startup sequence complete!\n"); digitalWrite(LED_BUILTIN, HIGH);