2016-04-22 22:26:52 +02:00
|
|
|
#include "DeviceDht.h"
|
|
|
|
|
|
|
|
void DeviceDht::deviceSetup() {
|
|
|
|
pinMode(pin, INPUT);
|
|
|
|
dht.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceDht::deviceRegister() {
|
|
|
|
Homie.registerNode(humidityNode);
|
|
|
|
Homie.registerNode(temperatureNode);
|
|
|
|
Homie.registerNode(heatindexNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// compare float values
|
|
|
|
bool isEqual(float a, float b, float epsilon=0.001) {
|
|
|
|
return fabs(a - b) <= epsilon * fabs(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceDht::deviceLoop() {
|
|
|
|
if (millis() - lastSentDHT >= INTERVAL_DHT * 1000UL || lastSentDHT == 0) {
|
|
|
|
float previousHumidity = humidity;
|
|
|
|
float previousTemperature = temperature;
|
|
|
|
float previousHeatindex = heatindex;
|
|
|
|
humidity = dht.readHumidity(); // read humidity as a percent
|
|
|
|
temperature = dht.readTemperature(); // read temperature as Celsius
|
|
|
|
heatindex = dht.computeHeatIndex(temperature, humidity, false);
|
|
|
|
|
|
|
|
// check if any reads failed and exit early
|
|
|
|
if (isnan(humidity) || isnan(temperature)) {
|
|
|
|
Serial.println("Failed to read from DHT sensor!");
|
2016-04-23 00:14:39 +02:00
|
|
|
delay(500);
|
2016-04-22 22:26:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isEqual(humidity, previousHumidity)) {
|
|
|
|
Serial.print("humidity: ");
|
|
|
|
Serial.println(humidity);
|
2016-04-23 01:44:56 +02:00
|
|
|
if (!Homie.setNodeProperty(humidityNode, "value", String(humidity), false)) {
|
2016-04-22 22:26:52 +02:00
|
|
|
Serial.println("Sending failed");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Serial.println("humidity unchanged");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isEqual(temperature, previousTemperature)) {
|
|
|
|
Serial.print("temperature: ");
|
|
|
|
Serial.println(temperature);
|
2016-04-23 01:44:56 +02:00
|
|
|
if (!Homie.setNodeProperty(temperatureNode, "value", String(temperature), false)) {
|
2016-04-22 22:26:52 +02:00
|
|
|
Serial.println("Sending failed");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Serial.println("temperature unchanged");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isEqual(heatindex, previousHeatindex)) {
|
|
|
|
Serial.print("heatindex: ");
|
|
|
|
Serial.println(heatindex);
|
2016-04-23 01:44:56 +02:00
|
|
|
if (!Homie.setNodeProperty(heatindexNode, "value", String(heatindex), false)) {
|
2016-04-22 22:26:52 +02:00
|
|
|
Serial.println("Sending failed");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Serial.println("heatindex unchanged");
|
|
|
|
}
|
|
|
|
|
|
|
|
lastSentDHT = millis();
|
|
|
|
}
|
|
|
|
}
|