From f2138d2378f260cc8f94cbc117e6be8f52ebfc22 Mon Sep 17 00:00:00 2001 From: Lukas Heiligenbrunner Date: Sat, 5 Dec 2020 12:45:07 +0000 Subject: [PATCH] use a non blocking dht22 library --- CMakeLists.txt | 2 +- platformio.ini | 9 ++++--- src/Heating.cpp | 57 ++++++++++++++++++++++++++++++++++----------- src/Heating.h | 15 ++++++++++-- src/Temperature.cpp | 29 ----------------------- src/Temperature.h | 34 --------------------------- src/WifiManager.cpp | 16 +++++-------- src/WifiManager.h | 9 ++++--- src/main.cpp | 20 +++++----------- 9 files changed, 78 insertions(+), 113 deletions(-) delete mode 100644 src/Temperature.cpp delete mode 100644 src/Temperature.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ada0a9..e9f00e0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ set(CMAKE_SYSTEM_NAME Generic) set(CMAKE_C_COMPILER_WORKS 1) set(CMAKE_CXX_COMPILER_WORKS 1) -project("PumpenSteuerung" C CXX) +project("pumpensteuerung" C CXX) include(CMakeListsPrivate.txt) diff --git a/platformio.ini b/platformio.ini index bf247d0..80adb2e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,8 +12,7 @@ platform = espressif8266 board = esp07 framework = arduino - -lib_deps = - SPI - Wire - DHT sensor library \ No newline at end of file +lib_deps = + SPI + Wire + bertmelis/DHT@1.0.1 diff --git a/src/Heating.cpp b/src/Heating.cpp index 2769667..723db27 100644 --- a/src/Heating.cpp +++ b/src/Heating.cpp @@ -5,7 +5,7 @@ #include "Heating.h" #include "Pins.h" -void Heating::init(Temperature *tempsensor, unsigned mode) { +void Heating::init(unsigned mode) { switch (mode) { case TIME: { const unsigned percentOn = 20; @@ -40,18 +40,17 @@ void Heating::init(Temperature *tempsensor, unsigned mode) { case HUMIDITY: mHeatingStatus = 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; + sensor.setPin(TempSensorPin); - Serial.print("humidity read: "); - Serial.println(hum); + sensor.onData([this](float hum, float temp) { + schedule_function([hum, temp, this]() { + Serial.printf("Temp: %gdegC\n", temp); + Serial.printf("Humid: %g%%\n", hum); - if (hum > 65.0) { + this->lasttemp = temp; + this->lasthum = hum; + + if (hum > 70.0) { // turn off active turnoff timers mLuefterTicker.detach(); Serial.println("heating should run now!"); @@ -59,7 +58,7 @@ void Heating::init(Temperature *tempsensor, unsigned mode) { digitalWrite(LuefterPin, HIGH); digitalWrite(HeizungPin, HIGH); mHeatingStatus = true; - } else if (hum < 60.0) { + } else if (hum < 65.0) { // if humidity too low turn off heating and fan after 60secs digitalWrite(HeizungPin, LOW); Serial.println("heating should NOT run now!"); @@ -69,15 +68,47 @@ void Heating::init(Temperature *tempsensor, unsigned mode) { mLuefterTicker.once(60, []() { // turn off fan digitalWrite(LuefterPin, LOW); - Serial.println("turning off fan"); + schedule_function([]() { + Serial.println("turning off fan"); + }); }); } mHeatingStatus = false; } }); }); + + 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; } } + +float *Heating::getLastTemp() { + return &lasttemp; +} + +float *Heating::getLastHum() { + return &lasthum; +} + +Heating::Heating() : mHeizungTicker(), + mLuefterTicker(), + mTurnOffTicker(), + mHeatingStatus(), + lasttemp(0), + lasthum(0), + sensor() {} diff --git a/src/Heating.h b/src/Heating.h index 7a1baba..64985d8 100644 --- a/src/Heating.h +++ b/src/Heating.h @@ -5,11 +5,17 @@ #pragma once #include -#include "Temperature.h" +#include +#include "Pins.h" class Heating { public: - void init(Temperature* tempsensor, unsigned mode); + Heating(); + + void init(unsigned mode); + + float* getLastHum(); + float* getLastTemp(); enum MODES {TIME, HUMIDITY}; private: @@ -18,4 +24,9 @@ private: Ticker mTurnOffTicker; bool mHeatingStatus; + + float lasttemp; + float lasthum; + + DHT22 sensor; }; diff --git a/src/Temperature.cpp b/src/Temperature.cpp deleted file mode 100644 index 4d3c42c..0000000 --- a/src/Temperature.cpp +++ /dev/null @@ -1,29 +0,0 @@ -// -// Created by lukas on 22.08.20. -// - -#include "Temperature.h" - -void Temperature::init() { - dht.begin(); -} - -float Temperature::getTemp() { - float temp = dht.readTemperature(); - Serial.println("read temp is: " + String(temp)); - if (isnan(temp)) { - return -1; - } else { - return temp; - } -} - -float Temperature::getHum() { - float hum = dht.readHumidity(); - Serial.println("read hum is: " + String(hum)); - if (isnan(hum)) { - return -1; - } else { - return hum; - } -} diff --git a/src/Temperature.h b/src/Temperature.h deleted file mode 100644 index 9c1bfc2..0000000 --- a/src/Temperature.h +++ /dev/null @@ -1,34 +0,0 @@ -// -// Created by lukas on 22.08.20. -// - -#pragma once - -#include -#include -#include "Pins.h" - -class Temperature { -public: - Temperature() : dht(TempSensorPin, DHT22){}; - - /** - * initialize the temperature sensor - */ - void init(); - - /** - * read the temperature value in C - * @return float value of temperature - */ - float getTemp(); - - /** - * read current humidity value in % - * @return float value of humidity - */ - float getHum(); - -private: - DHT dht; -}; diff --git a/src/WifiManager.cpp b/src/WifiManager.cpp index 0dfd7b6..4fa4d97 100644 --- a/src/WifiManager.cpp +++ b/src/WifiManager.cpp @@ -6,7 +6,10 @@ #include "WifiManager.h" #include "Pins.h" -void WifiManager::init(Temperature* tempsensor) { +void WifiManager::init(float* hum, float* temp) { + this->hum = hum; + this->temp = temp; + Serial.print("Setting up Access Point"); // start softap const bool result = WiFi.softAP("PumpenSteuerung-Heiligenbrunner", "1qayxsw2"); @@ -31,9 +34,6 @@ void WifiManager::init(Temperature* tempsensor) { } else { Serial.println("Wifi Setup failed!"); } - - // init temperature sensor - temp = *tempsensor; } WifiManager::WifiManager() : server(80), lastPumpTime(0), lastWaterOutage(0), lastPumpDuration(0) {} @@ -108,8 +108,8 @@ void WifiManager::handleRoot() { index_html += "
Drucksensor: " + String((digitalRead(DruckSensorPin) ? "EIN" : "AUS")) + "
"; index_html += "
Wassersensor: " + String((digitalRead(WasserSensorPin) ? "EIN" : "AUS")) + "
"; - index_html += "
Temperatur: " + String(this->temp.getTemp()) + "C
"; - index_html += "
Relative Luftfeuchtigkeit: " + String(this->temp.getHum()) + "%
"; + index_html += "
Temperatur: " + String(*temp) + "C
"; + index_html += "
Relative Luftfeuchtigkeit: " + String(*hum) + "%
"; index_html += ""; @@ -171,7 +171,3 @@ 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 92aa825..979d0d0 100644 --- a/src/WifiManager.h +++ b/src/WifiManager.h @@ -7,7 +7,6 @@ #include #include #include -#include "Temperature.h" class WifiManager { public: @@ -16,7 +15,7 @@ public: /** * initialize the webserver */ - void init(Temperature* tempsensor); + void init(float* hum, float* temp); /** * handles new web requests and holds the webserver alive @@ -48,12 +47,9 @@ public: */ void setPumpDuration(unsigned long lastPumpDuration); - Temperature* getTemperatureSensor(); - private: ESP8266WebServer server; - Temperature temp; void handleRoot(); @@ -62,4 +58,7 @@ private: void handleGet(); unsigned long lastPumpTime, lastWaterOutage, lastPumpDuration; + + float* hum; + float* temp; }; diff --git a/src/main.cpp b/src/main.cpp index d25d514..6d8b120 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,10 @@ #include #include -#include #include "WifiManager.h" #include "Heating.h" #include "Pins.h" -#include "Temperature.h" #define VERSION "v1.1.1" @@ -26,7 +24,6 @@ Ticker pumpendauer; WifiManager mang; Heating mHeat; -Temperature temp; long turnontime = -1; @@ -151,7 +148,7 @@ void setup() { digitalWrite(HeizungPin, LOW); // initilize serial connection - Serial.begin(9600); + Serial.begin(115200); Serial.println("\n\n\n\nstartup of ESP"); Serial.print("Version: "); Serial.println(VERSION); @@ -176,18 +173,13 @@ void setup() { attachInterrupt(digitalPinToInterrupt(WasserSensorPin), WasserSensorInt, CHANGE); - // initialize wifi - Serial.println("initializing temperature sensor!"); - temp.init(); - Serial.print("initial read temperature: "); - Serial.println(temp.getTemp()); - - Serial.println("Initializing wifi"); - mang.init(&temp); - // initialize heating control Serial.println("initializing heating service"); - mHeat.init(&temp, Heating::HUMIDITY); + mHeat.init(Heating::HUMIDITY); + + // initialize wifi + Serial.println("Initializing wifi"); + mang.init(mHeat.getLastHum(), mHeat.getLastTemp()); Serial.println("startup sequence complete!\n"); digitalWrite(LED_BUILTIN, HIGH);