Upgrade DHT usermod (#2833)

* Implent publishing DHT data to MQTT broker

* Fix naming and add description
This commit is contained in:
Bartłomiej Wiśniewski 2022-10-20 01:02:52 +02:00 committed by GitHub
parent d7f6cd944c
commit b0037c75a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 8 deletions

View File

@ -6,12 +6,13 @@
; USERMOD_DHT_CELSIUS - define this to report temperatures in degrees celsious, otherwise fahrenheit will be reported
; USERMOD_DHT_MEASUREMENT_INTERVAL - the number of milliseconds between measurements, defaults to 60 seconds
; USERMOD_DHT_FIRST_MEASUREMENT_AT - the number of milliseconds after boot to take first measurement, defaults to 90 seconds
; USERMOD_DHT_MQTT - publish measurements to the MQTT broker
; USERMOD_DHT_STATS - For debug, report delay stats
[env:d1_mini_usermod_dht_C]
extends = env:d1_mini
build_flags = ${env:d1_mini.build_flags} -D USERMOD_DHT -D USERMOD_DHT_CELSIUS
lib_deps = ${env.lib_deps}
lib_deps = ${env:d1_mini.lib_deps}
https://github.com/alwynallan/DHT_nonblocking
[env:custom32_LEDPIN_16_usermod_dht_C]

View File

@ -1,10 +1,14 @@
# DHT Temperature/Humidity sensor usermod
This usermod will read from an attached DHT22 or DHT11 humidity and temperature sensor.
The sensor readings are displayed in the Info section of the web UI.
The sensor readings are displayed in the Info section of the web UI (and optionally send to a MQTT broker).
If sensor is not detected after a while (10 update intervals), this usermod will be disabled.
If enabled measured temperature and humidity will be published to the following MQTT topics
* `{devceTopic}/dht/temperature`
* `{devceTopic}/dht/humidity`
## Installation
Copy the example `platformio_override.ini` to the root directory. This file should be placed in the same directory as `platformio.ini`.
@ -17,6 +21,7 @@ Copy the example `platformio_override.ini` to the root directory. This file sho
* `USERMOD_DHT_CELSIUS` - define this to report temperatures in degrees celsious, otherwise fahrenheit will be reported
* `USERMOD_DHT_MEASUREMENT_INTERVAL` - the number of milliseconds between measurements, defaults to 60 seconds
* `USERMOD_DHT_FIRST_MEASUREMENT_AT` - the number of milliseconds after boot to take first measurement, defaults to 90 seconds
* `USERMOD_DHT_MQTT` - publish measurements to the MQTT broker
* `USERMOD_DHT_STATS` - For debug, report delay stats
## Project link
@ -29,7 +34,9 @@ If you are using `platformio_override.ini`, you should be able to refresh the ta
## Change Log
2022-10-15
* Add possibility to publish sensor readings to an MQTT broker
* fix compilation error for sample [env:d1_mini_usermod_dht_C] task
2020-02-04
* Change default QuinLed pin to Q2
* Instead of trying to keep updates at constant cadence, space readings out by measurement interval; hope this helps to avoid occasional bursts of readings with errors

View File

@ -62,6 +62,10 @@ class UsermodDHT : public Usermod {
float humidity, temperature = 0;
bool initializing = true;
bool disabled = false;
#ifdef USERMOD_DHT_MQTT
char dhtMqttTopic[64];
size_t dhtMqttTopicLen;
#endif
#ifdef USERMOD_DHT_STATS
unsigned long nextResetStatsTime = 0;
uint16_t updates = 0;
@ -76,6 +80,10 @@ class UsermodDHT : public Usermod {
void setup() {
nextReadTime = millis() + USERMOD_DHT_FIRST_MEASUREMENT_AT;
lastReadTime = millis();
#ifdef USERMOD_DHT_MQTT
sprintf(dhtMqttTopic, "%s/dht", mqttDeviceTopic);
dhtMqttTopicLen = strlen(dhtMqttTopic);
#endif
#ifdef USERMOD_DHT_STATS
nextResetStatsTime = millis() + 60*60*1000;
#endif
@ -110,6 +118,25 @@ class UsermodDHT : public Usermod {
temperature = tempC * 9 / 5 + 32;
#endif
#ifdef USERMOD_DHT_MQTT
// 10^n where n is number of decimal places to display in mqtt message. Please adjust buff size together with this constant
#define FLOAT_PREC 100
if (WLED_MQTT_CONNECTED) {
char buff[10];
strcpy(dhtMqttTopic + dhtMqttTopicLen, "/temperature");
sprintf(buff, "%d.%d", (int)temperature, ((int)(temperature * FLOAT_PREC)) % FLOAT_PREC);
mqtt->publish(dhtMqttTopic, 0, false, buff);
sprintf(buff, "%d.%d", (int)humidity, ((int)(humidity * FLOAT_PREC)) % FLOAT_PREC);
strcpy(dhtMqttTopic + dhtMqttTopicLen, "/humidity");
mqtt->publish(dhtMqttTopic, 0, false, buff);
dhtMqttTopic[dhtMqttTopicLen] = '\0';
}
#undef FLOAT_PREC
#endif
nextReadTime = millis() + USERMOD_DHT_MEASUREMENT_INTERVAL;
lastReadTime = millis();
initializing = false;