From d9c22f14abc229d2f339a340007c198cd424da00 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sat, 10 Jun 2017 17:41:23 +0200 Subject: [PATCH] Add DHT Sensor --- esp-volumeknob/esp-volumeknob.ino | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/esp-volumeknob/esp-volumeknob.ino b/esp-volumeknob/esp-volumeknob.ino index 0263943..cb72a5a 100644 --- a/esp-volumeknob/esp-volumeknob.ino +++ b/esp-volumeknob/esp-volumeknob.ino @@ -5,6 +5,13 @@ #include #include //Library download: https://github.com/PaulStoffregen/Encoder +#include "DHT.h" +DHT dht(D1,DHT22); +float temperature=0; +float humidity=0; +double lastSensorMillis=0; +#define SENSORPERIOD 5000 + //D4 auf ground macht led leuchten #define PIN_ENCA D6 @@ -45,7 +52,24 @@ void loopHandler() { lastPositionCheck=millis(); } - + if ((lastSensorMillis+SENSORPERIOD)-40){ + + temperature=_currentTemperature; + Homie.getLogger() << "temperature= " << temperature << endl; + char tempstring[16]; + dtostrf(temperature,1,1,tempstring); + knobNode.setProperty("temperature").send(tempstring); + } + float _currentHumidity=dht.readHumidity(); + if (humidity!=_currentHumidity && (!isnan(_currentHumidity) ==1) && _currentHumidity>0){ + humidity=_currentHumidity; + Homie.getLogger() << "humidity= " << humidity << endl; + knobNode.setProperty("humidity").send(String(humidity)); + } + lastSensorMillis=millis(); + } ArduinoOTA.handle(); @@ -70,6 +94,8 @@ void setup() { knobNode.advertise("encoder"); knobNode.advertise("btnknob"); + knobNode.advertise("temperature"); + knobNode.advertise("humidity"); Homie.setup(); ArduinoOTA.setHostname(Homie.getConfiguration().deviceId);