diff --git a/include/config.h b/include/config.h index 9ccf7dd..8610d5f 100644 --- a/include/config.h +++ b/include/config.h @@ -28,3 +28,18 @@ // Anzahl der angeschlossenen LEDs am Ring #define NUMPIXELS 8 +// ######## DHT Temperatur Sensor ######## + +// Kommentiere enable_DHT aus um das laden zu deaktivieren +#define enable_DHT + +// Pins für Temperatursensor DHT22 +#define DHT_PIN 18 +// DHT22 Typ +#define DHT_TYPE DHT22 +//#define DHT_TYPE DHT11 // DHT 11 +//#define DHT_TYPE DHT22 // DHT 22 (AM2302), AM2321 +//#define DHT_TYPE DHT21 // DHT 21 (AM2301) + +// DHT Sensor Wartezeit zwischen messungen +unsigned long sampletime_ms = 10000; // 10 Sekunden \ No newline at end of file diff --git a/include/dht_sensor.h b/include/dht_sensor.h new file mode 100644 index 0000000..186be22 --- /dev/null +++ b/include/dht_sensor.h @@ -0,0 +1,50 @@ +#include "DHT.h" + +#ifndef DHT_PIN + #define DHT_PIN 18 +#endif +#ifndef DHT_TYPE + #define DHT_TYPE DHT22 +#endif + +String dht_s_temperature; +String dht_s_humidity; +String dht_s_hic; + +DHT dht(DHT_PIN, DHT_TYPE); +String Float2String(float value) +{ + // Convert a float to String with two decimals. + String s; + s = String(int(value)); + s += '.'; + s += int((value - int(value)) * 100); + + return s; +} + +void sensorDHT(){ + String data; + float h = dht.readHumidity(); + float t = dht.readTemperature(); + + // check if Not a Number + if (isnan(t) || isnan(h)) { + Serial.println("DHT Sensordaten konnten nicht gelesen werden"); + } else { + //false = Celcius | true = Farenheit + float hic = dht.computeHeatIndex(t, h, false); + dht_s_temperature=Float2String(t); + dht_s_humidity=Float2String(h); + dht_s_hic=Float2String(hic); + Serial.print("Luftfeuchte: "); + Serial.print(dht_s_humidity); + Serial.print(" %\n"); + Serial.print("Temperatur: "); + Serial.print(dht_s_temperature); + Serial.print(" °C\n"); + Serial.print("Gefühlte Temperatur: "); + Serial.print(dht_s_hic); + Serial.print(" °C\n"); + } +} \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index e375d7a..b9200db 100644 --- a/platformio.ini +++ b/platformio.ini @@ -17,5 +17,7 @@ lib_deps = squix78/ESP8266 and ESP32 OLED driver for SSD1306 displays @^4.2.0 yiannisbourkelis/Uptime Library @ ^1.0.0 wifwaf/MH-Z19@^1.5.3 + adafruit/DHT sensor library@^1.4.1 + adafruit/Adafruit Unified Sensor@^1.1.4 monitor_speed = 115200 upload_speed = 921600 diff --git a/src/ebk_co2ampel.cpp b/src/ebk_co2ampel.cpp index 304a24c..e78cabe 100644 --- a/src/ebk_co2ampel.cpp +++ b/src/ebk_co2ampel.cpp @@ -7,6 +7,9 @@ #include #include "uptime_formatter.h" +#ifdef enable_DHT + #include "dht_sensor.h" + long lastMsg = 0; #endif @@ -85,6 +88,10 @@ void setup() { Serial.println("Starte..."); Serial.print("CO2-Ampel Firmware: ");Serial.println(ampelversion); +#ifdef enable_DHT + dht.begin(); +#endif + // Ab hier Bootmodus initialisieren und festlegen preferences.begin("co2", false); currentBootMode = preferences.getUInt("cal", BOOT_UNKNOWN); // Aktuellen Boot-Mode lesen und speichern @@ -309,5 +316,10 @@ void loop() { if(currentBootMode == BOOT_NORMAL) { set_led_color(co2); } } } + long now = millis(); + if (now - lastMsg > sampletime_ms) { + lastMsg = now; + sensorDHT(); // Temperatur, gefühlte Temperatur und Luftfeuchte + } }