modularized DHT stuff -- still not working, though
This commit is contained in:
parent
8523833139
commit
fe1b381924
95
things/DeviceDht.h
Normal file
95
things/DeviceDht.h
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
#ifndef DEVICEDHT_H
|
||||||
|
#define DEVICEDHT_H
|
||||||
|
|
||||||
|
#include "Device.h"
|
||||||
|
#include <Homie.h>
|
||||||
|
#include <DHT.h> // https://github.com/adafruit/DHT-sensor-library
|
||||||
|
|
||||||
|
class DeviceDht : public Device {
|
||||||
|
public:
|
||||||
|
inline DeviceDht(byte dhtPin, byte dhtType) {
|
||||||
|
pin = dhtPin;
|
||||||
|
type = dhtType;
|
||||||
|
}
|
||||||
|
virtual void setup();
|
||||||
|
virtual void homieRegister();
|
||||||
|
virtual void loop();
|
||||||
|
private:
|
||||||
|
byte pin;
|
||||||
|
byte type;
|
||||||
|
DHT dht{pin, type};
|
||||||
|
const int INTERVAL_DHT = 60;
|
||||||
|
unsigned long lastSentDHT = 0;
|
||||||
|
float humidity, temperature; // raw values from the sensor
|
||||||
|
float heatindex; // computed value from the sensor
|
||||||
|
HomieNode humidityNode{"humidity", "humidity"};
|
||||||
|
HomieNode temperatureNode{"temperature", "temperature"};
|
||||||
|
HomieNode heatindexNode{"heatindex", "heatindex"};
|
||||||
|
};
|
||||||
|
|
||||||
|
void DeviceDht::setup() {
|
||||||
|
pinMode(pin, INPUT);
|
||||||
|
dht.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
void DeviceDht::homieRegister() {
|
||||||
|
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::loop() {
|
||||||
|
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!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isEqual(humidity, previousHumidity)) {
|
||||||
|
Serial.print("humidity: ");
|
||||||
|
Serial.println(humidity);
|
||||||
|
if (!Homie.setNodeProperty(humidityNode, "value", String(humidity), true)) {
|
||||||
|
Serial.println("Sending failed");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Serial.println("humidity unchanged");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isEqual(temperature, previousTemperature)) {
|
||||||
|
Serial.print("temperature: ");
|
||||||
|
Serial.println(temperature);
|
||||||
|
if (!Homie.setNodeProperty(temperatureNode, "value", String(temperature), true)) {
|
||||||
|
Serial.println("Sending failed");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Serial.println("temperature unchanged");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isEqual(heatindex, previousHeatindex)) {
|
||||||
|
Serial.print("heatindex: ");
|
||||||
|
Serial.println(heatindex);
|
||||||
|
if (!Homie.setNodeProperty(heatindexNode, "value", String(heatindex), true)) {
|
||||||
|
Serial.println("Sending failed");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Serial.println("heatindex unchanged");
|
||||||
|
}
|
||||||
|
|
||||||
|
lastSentDHT = millis();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -1,12 +1,12 @@
|
|||||||
#include <Homie.h> // https://github.com/marvinroger/homie-esp8266
|
#include <Homie.h> // https://github.com/marvinroger/homie-esp8266
|
||||||
#include <DHT.h> // https://github.com/adafruit/DHT-sensor-library
|
|
||||||
|
|
||||||
#include "DeviceLed.h"
|
#include "DeviceLed.h"
|
||||||
#include "DeviceLdr.h"
|
#include "DeviceLdr.h"
|
||||||
|
#include "DeviceDht.h"
|
||||||
|
|
||||||
#define HAS_LDR
|
#undef HAS_LDR
|
||||||
#define HAS_LED
|
#undef HAS_LED
|
||||||
#undef HAS_DHT
|
#define HAS_DHT
|
||||||
|
|
||||||
// HAS_LED
|
// HAS_LED
|
||||||
#define PIN_LED_RED D8
|
#define PIN_LED_RED D8
|
||||||
@ -21,14 +21,7 @@ DeviceLdr deviceLdr(PIN_LDR);
|
|||||||
// HAS_DHT
|
// HAS_DHT
|
||||||
#define PIN_DHT D4
|
#define PIN_DHT D4
|
||||||
#define TYPE_DHT DHT22
|
#define TYPE_DHT DHT22
|
||||||
HomieNode humidityNode("humidity", "humidity");
|
DeviceDht deviceDht(PIN_DHT, TYPE_DHT);
|
||||||
HomieNode temperatureNode("temperature", "temperature");
|
|
||||||
HomieNode heatindexNode("heatindex", "heatindex");
|
|
||||||
const int INTERVAL_DHT = 60;
|
|
||||||
unsigned long lastSentDHT = 0;
|
|
||||||
DHT dht(PIN_DHT, TYPE_DHT);
|
|
||||||
float humidity, temperature; // raw values from the sensor
|
|
||||||
float heatindex; // computed value from the sensor
|
|
||||||
|
|
||||||
void setupHandler() {
|
void setupHandler() {
|
||||||
#ifdef HAS_LDR
|
#ifdef HAS_LDR
|
||||||
@ -38,64 +31,10 @@ void setupHandler() {
|
|||||||
deviceLed.setup();
|
deviceLed.setup();
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAS_DHT
|
#ifdef HAS_DHT
|
||||||
dht.begin();
|
deviceDht.setup();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// compare float values
|
|
||||||
bool isEqual(float a, float b, float epsilon=0.001) {
|
|
||||||
return fabs(a - b) <= epsilon * fabs(a);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loopHandlerDHT() {
|
|
||||||
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!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isEqual(humidity, previousHumidity)) {
|
|
||||||
Serial.print("humidity: ");
|
|
||||||
Serial.println(humidity);
|
|
||||||
if (!Homie.setNodeProperty(humidityNode, "value", String(humidity), true)) {
|
|
||||||
Serial.println("Sending failed");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Serial.println("humidity unchanged");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isEqual(temperature, previousTemperature)) {
|
|
||||||
Serial.print("temperature: ");
|
|
||||||
Serial.println(temperature);
|
|
||||||
if (!Homie.setNodeProperty(temperatureNode, "value", String(temperature), true)) {
|
|
||||||
Serial.println("Sending failed");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Serial.println("temperature unchanged");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isEqual(heatindex, previousHeatindex)) {
|
|
||||||
Serial.print("heatindex: ");
|
|
||||||
Serial.println(heatindex);
|
|
||||||
if (!Homie.setNodeProperty(heatindexNode, "value", String(heatindex), true)) {
|
|
||||||
Serial.println("Sending failed");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Serial.println("heatindex unchanged");
|
|
||||||
}
|
|
||||||
|
|
||||||
lastSentDHT = millis();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void loopHandler() {
|
void loopHandler() {
|
||||||
#ifdef HAS_LDR
|
#ifdef HAS_LDR
|
||||||
deviceLdr.loop();
|
deviceLdr.loop();
|
||||||
@ -104,7 +43,7 @@ void loopHandler() {
|
|||||||
deviceLed.loop();
|
deviceLed.loop();
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAS_DHT
|
#ifdef HAS_DHT
|
||||||
loopHandlerDHT();
|
deviceDht.loop();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,9 +56,7 @@ void setup() {
|
|||||||
deviceLed.homieRegister();
|
deviceLed.homieRegister();
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAS_DHT
|
#ifdef HAS_DHT
|
||||||
Homie.registerNode(humidityNode);
|
deviceDht.homieRegister();
|
||||||
Homie.registerNode(temperatureNode);
|
|
||||||
Homie.registerNode(heatindexNode);
|
|
||||||
#endif
|
#endif
|
||||||
Homie.setSetupFunction(setupHandler);
|
Homie.setSetupFunction(setupHandler);
|
||||||
Homie.setLoopFunction(loopHandler);
|
Homie.setLoopFunction(loopHandler);
|
||||||
|
Loading…
Reference in New Issue
Block a user