use a non blocking dht22 library
This commit is contained in:
		@@ -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() {}
 | 
			
		||||
 
 | 
			
		||||
@@ -5,11 +5,17 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <Ticker.h>
 | 
			
		||||
#include "Temperature.h"
 | 
			
		||||
#include <DHT.h>
 | 
			
		||||
#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;
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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;
 | 
			
		||||
};
 | 
			
		||||
@@ -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 += "<div>Drucksensor: " + String((digitalRead(DruckSensorPin) ? "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 += "<div>Relative Luftfeuchtigkeit: " + String(this->temp.getHum()) + "%</div>";
 | 
			
		||||
    index_html += "</br><div>Temperatur: " + String(*temp) + "C</div>";
 | 
			
		||||
    index_html += "<div>Relative Luftfeuchtigkeit: " + String(*hum) + "%</div>";
 | 
			
		||||
 | 
			
		||||
    index_html += "</body></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;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,6 @@
 | 
			
		||||
#include <Arduino.h>
 | 
			
		||||
#include <ESP8266WiFi.h>
 | 
			
		||||
#include <ESP8266WebServer.h>
 | 
			
		||||
#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;
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										20
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								src/main.cpp
									
									
									
									
									
								
							@@ -1,12 +1,10 @@
 | 
			
		||||
#include <Arduino.h>
 | 
			
		||||
#include <Ticker.h>
 | 
			
		||||
#include <EEPROM.h>
 | 
			
		||||
 | 
			
		||||
#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);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user