From f9467ceaf155b5dcce7add362138eef0074afe23 Mon Sep 17 00:00:00 2001 From: Michael Bisbjerg Date: Wed, 15 May 2024 21:26:30 +0200 Subject: [PATCH] Fix state issue on change reporting, add override sample ini --- .gitignore | 2 +- usermods/AHT10_v2/README.md | 1 - usermods/AHT10_v2/platformio_override.ini | 9 +++++++++ usermods/AHT10_v2/usermod_aht10.h | 13 ++++++++++--- 4 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 usermods/AHT10_v2/platformio_override.ini diff --git a/.gitignore b/.gitignore index 8a2319a72..0a7bb1602 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,7 @@ .vscode esp01-update.sh -platformio_override.ini +/platformio_override.ini replace_fs.py wled-update.sh diff --git a/usermods/AHT10_v2/README.md b/usermods/AHT10_v2/README.md index 650a59532..69fab4671 100644 --- a/usermods/AHT10_v2/README.md +++ b/usermods/AHT10_v2/README.md @@ -33,5 +33,4 @@ build_flags = lib_deps = ${esp32.lib_deps} enjoyneering/AHT10@~1.1.0 - Wire ``` diff --git a/usermods/AHT10_v2/platformio_override.ini b/usermods/AHT10_v2/platformio_override.ini new file mode 100644 index 000000000..30240f222 --- /dev/null +++ b/usermods/AHT10_v2/platformio_override.ini @@ -0,0 +1,9 @@ +[env:aht10_example] +extends = env:esp32dev +build_flags = + ${common.build_flags} ${esp32.build_flags} + -D USERMOD_AHT10 + ; -D USERMOD_AHT10_DEBUG ; -- add a debug status to the info modal +lib_deps = + ${esp32.lib_deps} + enjoyneering/AHT10@~1.1.0 \ No newline at end of file diff --git a/usermods/AHT10_v2/usermod_aht10.h b/usermods/AHT10_v2/usermod_aht10.h index dde27a588..2680c82e2 100644 --- a/usermods/AHT10_v2/usermod_aht10.h +++ b/usermods/AHT10_v2/usermod_aht10.h @@ -32,6 +32,11 @@ private: float _lastHumidity = 0; float _lastTemperature = 0; +#ifndef WLED_MQTT_DISABLE + float _lastHumiditySent = 0; + float _lastTemperatureSent = 0; +#endif + AHT10 *_aht = nullptr; float truncateDecimals(float val) @@ -74,7 +79,7 @@ private: mqttCreateHassSensor(F("Humidity"), topic, F("humidity"), F("%")); } - void mqttPublishIfChanged(const __FlashStringHelper *topic, float lastState, float state, float minChange) + void mqttPublishIfChanged(const __FlashStringHelper *topic, float &lastState, float state, float minChange) { // Check if MQTT Connected, otherwise it will crash the 8266 // Only report if the change is larger than the required diff @@ -83,6 +88,8 @@ private: char subuf[128]; snprintf_P(subuf, 127, PSTR("%s/%s"), mqttDeviceTopic, (const char *)topic); mqtt->publish(subuf, 0, false, String(state).c_str()); + + lastState = state; } } @@ -163,10 +170,10 @@ public: // We can avoid reporting if the change is insignificant. The threshold chosen is below the level of accuracy, but way above 0.01 which is the precision of the value provided. // The AHT10/15/20 has an accuracy of 0.3C in the temperature readings - mqttPublishIfChanged(F("temperature"), _lastTemperature, temperature, 0.1f); + mqttPublishIfChanged(F("temperature"), _lastTemperatureSent, temperature, 0.1f); // The AHT10/15/20 has an accuracy in the humidity sensor of 2% - mqttPublishIfChanged(F("humidity"), _lastHumidity, humidity, 0.5f); + mqttPublishIfChanged(F("humidity"), _lastHumiditySent, humidity, 0.5f); #endif // Store