Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

67 lines
1.7 KiB

/*
ESPurna
DHT MODULE
Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
*/
#if ENABLE_DHT
#include <DHT.h>
DHT dht(DHT_PIN, DHT_TYPE, DHT_TIMING);
// -----------------------------------------------------------------------------
// DHT
// -----------------------------------------------------------------------------
void dhtSetup() {
dht.begin();
}
void dhtLoop() {
if (!mqttConnected()) return;
// Check if we should read new data
static unsigned long last_update = 0;
if ((millis() - last_update > DHT_UPDATE_INTERVAL) || (last_update == 0)) {
last_update = millis();
// Read sensor data
double h = dht.readHumidity();
double t = dht.readTemperature();
// Check if readings are valid
if (isnan(h) || isnan(t)) {
DEBUG_MSG("[DHT] Error reading sensor\n");
} else {
char temperature[6];
char humidity[6];
dtostrf(t, 4, 1, temperature);
itoa((int) h, humidity, 10);
DEBUG_MSG("[DHT] Temperature: %s\n", temperature);
DEBUG_MSG("[DHT] Humidity: %s\n", humidity);
// Send MQTT messages
mqttSend((char *) getSetting("dhtTemperatureTopic", DHT_TEMPERATURE_TOPIC).c_str(), temperature);
mqttSend((char *) getSetting("dhtHumidityTopic", DHT_HUMIDITY_TOPIC).c_str(), humidity);
// Update websocket clients
char buffer[20];
sprintf_P(buffer, PSTR("{\"temperature\": %s, \"humidity\": %s}"), temperature, humidity);
webSocketSend(buffer);
}
}
}
#endif