use a non blocking dht22 library

This commit is contained in:
Lukas Heiligenbrunner 2020-12-05 12:45:07 +00:00
parent 5943ffc97b
commit f2138d2378
9 changed files with 78 additions and 113 deletions

View File

@ -10,7 +10,7 @@ set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_C_COMPILER_WORKS 1) set(CMAKE_C_COMPILER_WORKS 1)
set(CMAKE_CXX_COMPILER_WORKS 1) set(CMAKE_CXX_COMPILER_WORKS 1)
project("PumpenSteuerung" C CXX) project("pumpensteuerung" C CXX)
include(CMakeListsPrivate.txt) include(CMakeListsPrivate.txt)

View File

@ -12,8 +12,7 @@
platform = espressif8266 platform = espressif8266
board = esp07 board = esp07
framework = arduino framework = arduino
lib_deps = lib_deps =
SPI SPI
Wire Wire
DHT sensor library bertmelis/DHT@1.0.1

View File

@ -5,7 +5,7 @@
#include "Heating.h" #include "Heating.h"
#include "Pins.h" #include "Pins.h"
void Heating::init(Temperature *tempsensor, unsigned mode) { void Heating::init(unsigned mode) {
switch (mode) { switch (mode) {
case TIME: { case TIME: {
const unsigned percentOn = 20; const unsigned percentOn = 20;
@ -40,18 +40,17 @@ void Heating::init(Temperature *tempsensor, unsigned mode) {
case HUMIDITY: case HUMIDITY:
mHeatingStatus = false; mHeatingStatus = false;
mHeizungTicker.attach(10, [&tempsensor, this]() { sensor.setPin(TempSensorPin);
// 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: "); sensor.onData([this](float hum, float temp) {
Serial.println(hum); 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 // turn off active turnoff timers
mLuefterTicker.detach(); mLuefterTicker.detach();
Serial.println("heating should run now!"); Serial.println("heating should run now!");
@ -59,7 +58,7 @@ void Heating::init(Temperature *tempsensor, unsigned mode) {
digitalWrite(LuefterPin, HIGH); digitalWrite(LuefterPin, HIGH);
digitalWrite(HeizungPin, HIGH); digitalWrite(HeizungPin, HIGH);
mHeatingStatus = true; mHeatingStatus = true;
} else if (hum < 60.0) { } else if (hum < 65.0) {
// if humidity too low turn off heating and fan after 60secs // if humidity too low turn off heating and fan after 60secs
digitalWrite(HeizungPin, LOW); digitalWrite(HeizungPin, LOW);
Serial.println("heating should NOT run now!"); Serial.println("heating should NOT run now!");
@ -69,15 +68,47 @@ void Heating::init(Temperature *tempsensor, unsigned mode) {
mLuefterTicker.once(60, []() { mLuefterTicker.once(60, []() {
// turn off fan // turn off fan
digitalWrite(LuefterPin, LOW); digitalWrite(LuefterPin, LOW);
Serial.println("turning off fan"); schedule_function([]() {
Serial.println("turning off fan");
});
}); });
} }
mHeatingStatus = false; 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; break;
default: default:
break; break;
} }
} }
float *Heating::getLastTemp() {
return &lasttemp;
}
float *Heating::getLastHum() {
return &lasthum;
}
Heating::Heating() : mHeizungTicker(),
mLuefterTicker(),
mTurnOffTicker(),
mHeatingStatus(),
lasttemp(0),
lasthum(0),
sensor() {}

View File

@ -5,11 +5,17 @@
#pragma once #pragma once
#include <Ticker.h> #include <Ticker.h>
#include "Temperature.h" #include <DHT.h>
#include "Pins.h"
class Heating { class Heating {
public: public:
void init(Temperature* tempsensor, unsigned mode); Heating();
void init(unsigned mode);
float* getLastHum();
float* getLastTemp();
enum MODES {TIME, HUMIDITY}; enum MODES {TIME, HUMIDITY};
private: private:
@ -18,4 +24,9 @@ private:
Ticker mTurnOffTicker; Ticker mTurnOffTicker;
bool mHeatingStatus; bool mHeatingStatus;
float lasttemp;
float lasthum;
DHT22 sensor;
}; };

View File

@ -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;
}
}

View File

@ -1,34 +0,0 @@
//
// Created by lukas on 22.08.20.
//
#pragma once
#include <Adafruit_Sensor.h>
#include <DHT.h>
#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;
};

View File

@ -6,7 +6,10 @@
#include "WifiManager.h" #include "WifiManager.h"
#include "Pins.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"); Serial.print("Setting up Access Point");
// start softap // start softap
const bool result = WiFi.softAP("PumpenSteuerung-Heiligenbrunner", "1qayxsw2"); const bool result = WiFi.softAP("PumpenSteuerung-Heiligenbrunner", "1qayxsw2");
@ -31,9 +34,6 @@ void WifiManager::init(Temperature* tempsensor) {
} else { } else {
Serial.println("Wifi Setup failed!"); Serial.println("Wifi Setup failed!");
} }
// init temperature sensor
temp = *tempsensor;
} }
WifiManager::WifiManager() : server(80), lastPumpTime(0), lastWaterOutage(0), lastPumpDuration(0) {} WifiManager::WifiManager() : server(80), lastPumpTime(0), lastWaterOutage(0), lastPumpDuration(0) {}
@ -108,8 +108,8 @@ void WifiManager::handleRoot() {
index_html += "<div>Drucksensor: " + String((digitalRead(DruckSensorPin) ? "EIN" : "AUS")) + "</div>"; index_html += "<div>Drucksensor: " + String((digitalRead(DruckSensorPin) ? "EIN" : "AUS")) + "</div>";
index_html += "<div>Wassersensor: " + String((digitalRead(WasserSensorPin) ? "EIN" : "AUS")) + "</div>"; index_html += "<div>Wassersensor: " + String((digitalRead(WasserSensorPin) ? "EIN" : "AUS")) + "</div>";
index_html += "</br><div>Temperatur: " + String(this->temp.getTemp()) + "C</div>"; index_html += "</br><div>Temperatur: " + String(*temp) + "C</div>";
index_html += "<div>Relative Luftfeuchtigkeit: " + String(this->temp.getHum()) + "%</div>"; index_html += "<div>Relative Luftfeuchtigkeit: " + String(*hum) + "%</div>";
index_html += "</body></html>"; index_html += "</body></html>";
@ -171,7 +171,3 @@ void WifiManager::setlastWaterOutage(unsigned long lastWaterOutage) {
void WifiManager::setPumpDuration(unsigned long lastPumpDuration) { void WifiManager::setPumpDuration(unsigned long lastPumpDuration) {
this->lastPumpDuration = lastPumpDuration; this->lastPumpDuration = lastPumpDuration;
} }
Temperature *WifiManager::getTemperatureSensor() {
return &temp;
}

View File

@ -7,7 +7,6 @@
#include <Arduino.h> #include <Arduino.h>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
#include "Temperature.h"
class WifiManager { class WifiManager {
public: public:
@ -16,7 +15,7 @@ public:
/** /**
* initialize the webserver * initialize the webserver
*/ */
void init(Temperature* tempsensor); void init(float* hum, float* temp);
/** /**
* handles new web requests and holds the webserver alive * handles new web requests and holds the webserver alive
@ -48,12 +47,9 @@ public:
*/ */
void setPumpDuration(unsigned long lastPumpDuration); void setPumpDuration(unsigned long lastPumpDuration);
Temperature* getTemperatureSensor();
private: private:
ESP8266WebServer server; ESP8266WebServer server;
Temperature temp;
void handleRoot(); void handleRoot();
@ -62,4 +58,7 @@ private:
void handleGet(); void handleGet();
unsigned long lastPumpTime, lastWaterOutage, lastPumpDuration; unsigned long lastPumpTime, lastWaterOutage, lastPumpDuration;
float* hum;
float* temp;
}; };

View File

@ -1,12 +1,10 @@
#include <Arduino.h> #include <Arduino.h>
#include <Ticker.h> #include <Ticker.h>
#include <EEPROM.h>
#include "WifiManager.h" #include "WifiManager.h"
#include "Heating.h" #include "Heating.h"
#include "Pins.h" #include "Pins.h"
#include "Temperature.h"
#define VERSION "v1.1.1" #define VERSION "v1.1.1"
@ -26,7 +24,6 @@ Ticker pumpendauer;
WifiManager mang; WifiManager mang;
Heating mHeat; Heating mHeat;
Temperature temp;
long turnontime = -1; long turnontime = -1;
@ -151,7 +148,7 @@ void setup() {
digitalWrite(HeizungPin, LOW); digitalWrite(HeizungPin, LOW);
// initilize serial connection // initilize serial connection
Serial.begin(9600); Serial.begin(115200);
Serial.println("\n\n\n\nstartup of ESP"); Serial.println("\n\n\n\nstartup of ESP");
Serial.print("Version: "); Serial.print("Version: ");
Serial.println(VERSION); Serial.println(VERSION);
@ -176,18 +173,13 @@ void setup() {
attachInterrupt(digitalPinToInterrupt(WasserSensorPin), WasserSensorInt, CHANGE); 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 // initialize heating control
Serial.println("initializing heating service"); 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"); Serial.println("startup sequence complete!\n");
digitalWrite(LED_BUILTIN, HIGH); digitalWrite(LED_BUILTIN, HIGH);