DHT22 Adafruit
This commit is contained in:
parent
422378c0cb
commit
da31e2d40a
@ -6,7 +6,7 @@ framework = arduino
|
||||
lib_deps = SPI
|
||||
https://github.com/adafruit/Adafruit_BusIO
|
||||
https://github.com/milesburton/Arduino-Temperature-Control-Library
|
||||
https://github.com/olewolf/DHT_nonblocking
|
||||
https://github.com/adafruit/DHT-sensor-library
|
||||
https://github.com/adafruit/Adafruit_TSL2561
|
||||
https://github.com/knolleary/pubsubclient
|
||||
https://github.com/adafruit/Adafruit_BME680
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
|
||||
TSL2561 greenhouseTSL("greenhouse");
|
||||
|
||||
DHT22 greenhouseDHT22("greenhouse", D5);
|
||||
DHT22Sensor greenhouseDHT22("greenhouse", D5);
|
||||
|
||||
void patrixSetup() {
|
||||
greenhouseTSL.setup();
|
||||
|
||||
@ -1,15 +1,14 @@
|
||||
#ifndef DHT22_H
|
||||
#define DHT22_H
|
||||
|
||||
#include <dht_nonblocking.h>
|
||||
|
||||
#include "mqtt.h"
|
||||
#include "DHT_U.h"
|
||||
|
||||
class DHT22 {
|
||||
class DHT22Sensor {
|
||||
|
||||
int pin;
|
||||
|
||||
DHT_nonblocking sensor;
|
||||
DHT_Unified dht;
|
||||
|
||||
unsigned long last = 0UL;
|
||||
|
||||
@ -19,23 +18,39 @@ public:
|
||||
|
||||
unsigned long intervalMs;
|
||||
|
||||
explicit DHT22(String name, const int pin, const unsigned long interval_ms = 5000) : pin(pin), sensor(pin, DHT_TYPE_22), name(std::move(name)), intervalMs(interval_ms) {
|
||||
explicit DHT22Sensor(String name, const int pin, const unsigned long interval_ms = 5000) : pin(pin), dht(pin, DHT22), name(std::move(name)), intervalMs(interval_ms) {
|
||||
//
|
||||
}
|
||||
|
||||
void setup() {
|
||||
dht.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
const auto now = max(1UL, millis());
|
||||
float temperature = NAN;
|
||||
if (last == 0 || now - last >= intervalMs) {
|
||||
float temp = NAN;
|
||||
float relHumid = NAN;
|
||||
if (sensor.measure(&temp, &relHumid)) {
|
||||
double absHumid = calculateHumidityAbsolute(temp, relHumid);
|
||||
mqttPublishValue(name + "/temperature", temp, "TEMPERATURE_C");
|
||||
mqttPublishValue(name + "/humidity/relative", relHumid, "HUMIDITY_RELATIVE_PERCENT");
|
||||
mqttPublishValue(name + "/humidity/absolute", absHumid, "HUMIDITY_ABSOLUTE_GM3");
|
||||
sensors_event_t event;
|
||||
|
||||
dht.temperature().getEvent(&event);
|
||||
if (isnan(event.temperature)) {
|
||||
Log.error("Error reading temperature!");
|
||||
} else {
|
||||
Log.error("Failed to read DHT22: %s", name.c_str());
|
||||
temperature = event.temperature;
|
||||
mqttPublishValue(name + "/temperature", temperature, "TEMPERATURE_C");
|
||||
}
|
||||
|
||||
dht.humidity().getEvent(&event);
|
||||
if (isnan(event.relative_humidity)) {
|
||||
Log.error("Error reading humidity!");
|
||||
} else {
|
||||
mqttPublishValue(name + "/humidity/relative", event.relative_humidity, "HUMIDITY_RELATIVE_PERCENT");
|
||||
if (!isnan(temperature)) {
|
||||
double absHumid = calculateHumidityAbsolute(event.temperature, event.relative_humidity);
|
||||
mqttPublishValue(name + "/humidity/absolute", absHumid, "HUMIDITY_ABSOLUTE_GM3");
|
||||
}
|
||||
}
|
||||
|
||||
last = now;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user