40 lines
770 B
C++
40 lines
770 B
C++
#if DHT_SENSOR
|
|
|
|
#include <ArduinoJson.h>
|
|
#include <Adafruit_Sensor.h>
|
|
#include <DHT.h>
|
|
|
|
#define DHT11_PIN 12
|
|
#define READ_INTERVAL(c) (c*60*1000UL) // read interval in minutes
|
|
|
|
DHT dht = DHT(DHT11_PIN, DHT11);
|
|
uint32_t currentTime = 0;
|
|
|
|
namespace Dht {
|
|
void setup() {
|
|
dht.begin();
|
|
}
|
|
|
|
void read() {
|
|
currentTime = millis();
|
|
static uint32_t lastReadTime = 0;
|
|
if (currentTime > lastReadTime) {
|
|
lastReadTime = currentTime + READ_INTERVAL(5);
|
|
StaticJsonDocument<200> jsonDoc;
|
|
JsonObject dht11 = jsonDoc.createNestedObject("dht11");
|
|
dht11["temperature"] = dht.readTemperature();
|
|
dht11["humidity"] = dht.readHumidity();
|
|
serializeJson(jsonDoc, Serial);
|
|
Serial.println();
|
|
}
|
|
}
|
|
}
|
|
#else
|
|
namespace Dht {
|
|
void setup() {
|
|
}
|
|
|
|
void read() {
|
|
}
|
|
}
|
|
#endif |