Add DHT Sensor

This commit is contained in:
interfisch 2017-06-10 17:41:23 +02:00
parent 366b221ea3
commit d9c22f14ab
1 changed files with 27 additions and 1 deletions

View File

@ -5,6 +5,13 @@
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include <Encoder.h> //Library download: https://github.com/PaulStoffregen/Encoder #include <Encoder.h> //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 //D4 auf ground macht led leuchten
#define PIN_ENCA D6 #define PIN_ENCA D6
@ -45,7 +52,24 @@ void loopHandler() {
lastPositionCheck=millis(); lastPositionCheck=millis();
} }
if ((lastSensorMillis+SENSORPERIOD)<millis()){
float _currentTemperature=dht.readTemperature();
if (temperature!=_currentTemperature && (!isnan(_currentTemperature) ==1) && _currentTemperature>-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(); ArduinoOTA.handle();
@ -70,6 +94,8 @@ void setup() {
knobNode.advertise("encoder"); knobNode.advertise("encoder");
knobNode.advertise("btnknob"); knobNode.advertise("btnknob");
knobNode.advertise("temperature");
knobNode.advertise("humidity");
Homie.setup(); Homie.setup();
ArduinoOTA.setHostname(Homie.getConfiguration().deviceId); ArduinoOTA.setHostname(Homie.getConfiguration().deviceId);