From 213e3e998a54c8ed1a8975c94e0401810c05656e Mon Sep 17 00:00:00 2001 From: Daniel Poelzleithner Date: Thu, 5 May 2022 11:24:41 +0000 Subject: [PATCH 01/12] Enable ESP watchdog by default Use the ESP watchdog to detect hanging ESP and reset the firmware. Can be disable by defining WLED_WATCHDOG_TIMOUT 0 Default timeout is 3 seconds on ESP32 and 8 seconds on ESP2688 --- wled00/wled.cpp | 25 +++++++++++++++++++++++++ wled00/wled.h | 7 +++++++ 2 files changed, 32 insertions(+) diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 54c723c8..a4204732 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -276,6 +276,15 @@ void WLED::loop() loops++; #endif // WLED_DEBUG toki.resetTick(); + +#if WLED_WATCHDOG_TIMEOUT > 0 + // we finished our mainloop, reset the watchdog timer + #ifdef ARDUINO_ARCH_ESP32 + esp_task_wdt_reset(); + #else + ESP.wdtFeed(); + #endif +#endif } void WLED::setup() @@ -302,6 +311,22 @@ void WLED::setup() DEBUG_PRINT(F("heap ")); DEBUG_PRINTLN(ESP.getFreeHeap()); +#if WLED_WATCHDOG_TIMEOUT > 0 +#ifdef ARDUINO_ARCH_ESP32 + esp_err_t watchdog = esp_task_wdt_init(WLED_WATCHDOG_TIMEOUT, true); + DEBUG_PRINT(F("Enable watchdog ")); + if (watchdog == ESP_OK) { + DEBUG_PRINTLN(F(" OK")); + } else { + DEBUG_PRINTLN(watchdog); + } + esp_task_wdt_add(NULL); +#else + // any timeout possible ? + ESP.wdtEnable(WLED_WATCHDOG_TIMEOUT * 1000); +#endif +#endif + #if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_PSRAM) if (psramFound()) { // GPIO16/GPIO17 reserved for SPI RAM diff --git a/wled00/wled.h b/wled00/wled.h index 6dabdd0b..2267f276 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -49,6 +49,12 @@ // filesystem specific debugging //#define WLED_DEBUG_FS +#ifndef WLED_WATCHDOG_TIMEOUT + // 3 seconds should be enough to detect a lockup + // define WLED_WATCHDOG_TIMEOUT=0 to disable watchdog + #define WLED_WATCHDOG_TIMEOUT 3 +#endif + //optionally disable brownout detector on ESP32. //This is generally a terrible idea, but improves boot success on boards with a 3.3v regulator + cap setup that can't provide 400mA peaks //#define WLED_DISABLE_BROWNOUT_DET @@ -78,6 +84,7 @@ #else #include #endif + #include "esp_task_wdt.h" #endif #include "src/dependencies/network/Network.h" From 26fa38d052fe7434848d9f4a919feae039c618b8 Mon Sep 17 00:00:00 2001 From: Daniel Poelzleithner Date: Mon, 23 May 2022 22:30:13 +0000 Subject: [PATCH 02/12] Watchdog: disable watchdog while OTA is running --- wled00/wled.cpp | 50 +++++++++++++++++++++++++++++------------- wled00/wled.h | 2 ++ wled00/wled_server.cpp | 2 ++ 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/wled00/wled.cpp b/wled00/wled.cpp index a4204732..d15cd354 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -287,6 +287,35 @@ void WLED::loop() #endif } +void WLED::enableWatchdog() { +#if WLED_WATCHDOG_TIMEOUT > 0 +#ifdef ARDUINO_ARCH_ESP32 + esp_err_t watchdog = esp_task_wdt_init(WLED_WATCHDOG_TIMEOUT, true); + DEBUG_PRINT(F("Watchdog enabled: ")); + if (watchdog == ESP_OK) { + DEBUG_PRINTLN(F("OK")); + } else { + DEBUG_PRINTLN(watchdog); + return; + } + esp_task_wdt_add(NULL); +#else + ESP.wdtEnable(WLED_WATCHDOG_TIMEOUT * 1000); +#endif +#endif +} + +void WLED::disableWatchdog() { +#if WLED_WATCHDOG_TIMEOUT > 0 +DEBUG_PRINTLN(F("Watchdog: disabled")); +#ifdef ARDUINO_ARCH_ESP32 + esp_task_wdt_delete(NULL); +#else + ESP.wdtDisable(); +#endif +#endif +} + void WLED::setup() { #if defined(ARDUINO_ARCH_ESP32) && defined(WLED_DISABLE_BROWNOUT_DET) @@ -311,21 +340,7 @@ void WLED::setup() DEBUG_PRINT(F("heap ")); DEBUG_PRINTLN(ESP.getFreeHeap()); -#if WLED_WATCHDOG_TIMEOUT > 0 -#ifdef ARDUINO_ARCH_ESP32 - esp_err_t watchdog = esp_task_wdt_init(WLED_WATCHDOG_TIMEOUT, true); - DEBUG_PRINT(F("Enable watchdog ")); - if (watchdog == ESP_OK) { - DEBUG_PRINTLN(F(" OK")); - } else { - DEBUG_PRINTLN(watchdog); - } - esp_task_wdt_add(NULL); -#else - // any timeout possible ? - ESP.wdtEnable(WLED_WATCHDOG_TIMEOUT * 1000); -#endif -#endif + enableWatchdog(); #if defined(ARDUINO_ARCH_ESP32) && defined(WLED_USE_PSRAM) if (psramFound()) { @@ -426,8 +441,13 @@ void WLED::setup() #ifdef ESP8266 wifi_set_sleep_type(NONE_SLEEP_T); #endif + WLED::instance().disableWatchdog(); DEBUG_PRINTLN(F("Start ArduinoOTA")); }); + ArduinoOTA.onError([](ota_error_t error) { + // reenable watchdog on failed update + WLED::instance().enableWatchdog(); + }); if (strlen(cmDNS) > 0) ArduinoOTA.setHostname(cmDNS); } diff --git a/wled00/wled.h b/wled00/wled.h index 2267f276..1a10199d 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -703,5 +703,7 @@ public: void initConnection(); void initInterfaces(); void handleStatusLED(); + void enableWatchdog(); + void disableWatchdog(); }; #endif // WLED_H diff --git a/wled00/wled_server.cpp b/wled00/wled_server.cpp index c4cd0980..d9a58b11 100644 --- a/wled00/wled_server.cpp +++ b/wled00/wled_server.cpp @@ -203,6 +203,7 @@ void initServer() },[](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){ if(!index){ DEBUG_PRINTLN(F("OTA Update Start")); + WLED::instance().disableWatchdog(); #ifdef ESP8266 Update.runAsync(true); #endif @@ -214,6 +215,7 @@ void initServer() DEBUG_PRINTLN(F("Update Success")); } else { DEBUG_PRINTLN(F("Update Failed")); + WLED::instance().enableWatchdog(); } } }); From a8908238d5e8c0aafb2f603168a193b86338e169 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sat, 2 Jul 2022 14:28:09 +0200 Subject: [PATCH 03/12] Prevent race condition when saving bus config. (loop() is executed on different core than handleSet()) --- wled00/cfg.cpp | 4 +++- wled00/set.cpp | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index 9d0eb63f..f50181f6 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -95,6 +95,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { uint8_t s = 0; // bus iterator if (fromFS) busses.removeAll(); // can't safely manipulate busses directly in network callback uint32_t mem = 0; + bool busesChanged = false; for (JsonObject elm : ins) { if (s >= WLED_MAX_BUSSES) break; uint8_t pins[5] = {255, 255, 255, 255, 255}; @@ -123,10 +124,11 @@ bool deserializeConfig(JsonObject doc, bool fromFS) { } else { if (busConfigs[s] != nullptr) delete busConfigs[s]; busConfigs[s] = new BusConfig(ledType, pins, start, length, colorOrder, reversed, skipFirst); - doInitBusses = true; + busesChanged = true; } s++; } + doInitBusses = busesChanged; // finalization done in beginStrip() } if (hw_led["rev"]) busses.getBus(0)->reversed = true; //set 0.11 global reversed setting for first bus diff --git a/wled00/set.cpp b/wled00/set.cpp index 0f57492d..9f427e60 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -90,6 +90,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) Bus::setAutoWhiteMode(strip.autoWhiteMode); strip.setTargetFps(request->arg(F("FR")).toInt()); + bool busesChanged = false; for (uint8_t s = 0; s < WLED_MAX_BUSSES; s++) { char lp[4] = "L0"; lp[2] = 48+s; lp[3] = 0; //ascii 0-9 //strip data pin char lc[4] = "LC"; lc[2] = 48+s; lc[3] = 0; //strip length @@ -121,8 +122,9 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) // actual finalization is done in WLED::loop() (removing old busses and adding new) if (busConfigs[s] != nullptr) delete busConfigs[s]; busConfigs[s] = new BusConfig(type, pins, start, length, colorOrder, request->hasArg(cv), skip); - doInitBusses = true; + busesChanged = true; } + //doInitBusses = busesChanged; // we will do that below to ensure all input data is processed ColorOrderMap com = {}; for (uint8_t s = 0; s < WLED_MAX_COLOR_ORDER_MAPPINGS; s++) { @@ -198,6 +200,8 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) if (t >= 0 && t < 4) strip.paletteBlend = t; t = request->arg(F("BF")).toInt(); if (t > 0) briMultiplier = t; + + doInitBusses = busesChanged; } //UI From e12f7b67e53edb87e167c3a3a3f000325e5cf91c Mon Sep 17 00:00:00 2001 From: albarlow Date: Tue, 19 Jul 2022 14:27:15 +0100 Subject: [PATCH 04/12] Usermod BME280 Enhancements I added a Usermod interface for key settings. I used a PinArray for the SDA/SCL pins, but you can't name these individually. I have also made a display to show the temperature/humidity values in the web interface's Info screen. I had to change the definition of those items in order to allow these new functions to work. I have not noticed any negative side effects to this change. At the moment, I've not figured out how to make Celsius/Farenheit toggleable due to the way the #define setup works. Finally, I have added a routine to publish MQTT Discovery Topics for Home Assistant (toggleable in the Usermod screen). I've been testing this on the only suitable device I have for a few months and haven't noticed any problems. --- usermods/BME280_v2/usermod_bme280.h | 183 ++++++++++++++++++++++++---- 1 file changed, 156 insertions(+), 27 deletions(-) diff --git a/usermods/BME280_v2/usermod_bme280.h b/usermods/BME280_v2/usermod_bme280.h index 82eb0887..4f3853bb 100644 --- a/usermods/BME280_v2/usermod_bme280.h +++ b/usermods/BME280_v2/usermod_bme280.h @@ -1,3 +1,6 @@ +// force the compiler to show a warning to confirm that this file is included +#warning **** Included USERMOD_BME280 version 2.0 **** + #pragma once #include "wled.h" @@ -11,32 +14,13 @@ class UsermodBME280 : public Usermod private: // User-defined configuration #define Celsius // Show temperature mesaurement in Celcius. Comment out for Fahrenheit -#define TemperatureDecimals 1 // Number of decimal places in published temperaure values -#define HumidityDecimals 2 // Number of decimal places in published humidity values -#define PressureDecimals 2 // Number of decimal places in published pressure values -#define TemperatureInterval 5 // Interval to measure temperature (and humidity, dew point if available) in seconds -#define PressureInterval 300 // Interval to measure pressure in seconds -#define PublishAlways 0 // Publish values even when they have not changed - -// Sanity checks -#if !defined(TemperatureDecimals) || TemperatureDecimals < 0 - #define TemperatureDecimals 0 -#endif -#if !defined(HumidityDecimals) || HumidityDecimals < 0 - #define HumidityDecimals 0 -#endif -#if !defined(PressureDecimals) || PressureDecimals < 0 - #define PressureDecimals 0 -#endif -#if !defined(TemperatureInterval) || TemperatureInterval < 0 - #define TemperatureInterval 1 -#endif -#if !defined(PressureInterval) || PressureInterval < 0 - #define PressureInterval TemperatureInterval -#endif -#if !defined(PublishAlways) - #define PublishAlways 0 -#endif +unsigned long TemperatureDecimals = 0; // Number of decimal places in published temperaure values +unsigned long HumidityDecimals = 0; // Number of decimal places in published humidity values +unsigned long PressureDecimals = 0; // Number of decimal places in published pressure values +unsigned long TemperatureInterval = 5; // Interval to measure temperature (and humidity, dew point if available) in seconds +unsigned long PressureInterval = 300; // Interval to measure pressure in seconds +bool PublishAlways = false; // Publish values even when they have not changed +bool HomeAssistantDiscovery = false; // Publish Home Assistant Device Information #ifdef ARDUINO_ARCH_ESP32 // ESP32 boards uint8_t SCL_PIN = 22; @@ -82,6 +66,14 @@ private: float lastDewPoint; float lastPressure; + // MQTT topic strings for publishing Home Assistant discovery topics + bool mqttInitialized = false; + String mqttTemperatureTopic = ""; + String mqttHumidityTopic = ""; + String mqttPressureTopic = ""; + String mqttHeatIndexTopic = ""; + String mqttDewPointTopic = ""; + // Store packet IDs of MQTT publications uint16_t mqttTemperaturePub = 0; uint16_t mqttPressurePub = 0; @@ -110,6 +102,55 @@ private: } } + // Procedure to define all MQTT Topics + void _mqttInitialize() + { + mqttTemperatureTopic = String(mqttDeviceTopic) + "/temperature"; + mqttPressureTopic = String(mqttDeviceTopic) + "/pressure"; + mqttHumidityTopic = String(mqttDeviceTopic) + "/humidity"; + mqttHeatIndexTopic = String(mqttDeviceTopic) + "/heat_index"; + mqttDewPointTopic = String(mqttDeviceTopic) + "/dew_point"; + + String t = String("homeassistant/sensor/") + mqttClientID + "/temperature/config"; + + _createMqttSensor("Temperature", mqttTemperatureTopic, "temperature", "°C"); + _createMqttSensor("Pressure", mqttPressureTopic, "pressure", "hPa"); + _createMqttSensor("Humidity", mqttHumidityTopic, "humidity", "%"); + _createMqttSensor("HeatIndex", mqttHeatIndexTopic, "temperature", "°C"); + _createMqttSensor("DewPoint", mqttDewPointTopic, "temperature", "°C"); + } + + // Create an MQTT Sensor for Home Assistant Discovery purposes, this includes a pointer to the topic that is published to in the Loop. + void _createMqttSensor(const String &name, const String &topic, const String &deviceClass, const String &unitOfMeasurement) + { + String t = String("homeassistant/sensor/") + mqttClientID + "/" + name + "/config"; + + StaticJsonDocument<600> doc; + + doc["name"] = String(serverDescription) + " " + name; + doc["state_topic"] = topic; + doc["unique_id"] = String(mqttClientID) + name; + if (unitOfMeasurement != "") + doc["unit_of_measurement"] = unitOfMeasurement; + if (deviceClass != "") + doc["device_class"] = deviceClass; + doc["expire_after"] = 1800; + + JsonObject device = doc.createNestedObject("device"); // attach the sensor to the same device + device["name"] = serverDescription; + device["identifiers"] = "wled-sensor-" + String(mqttClientID); + device["manufacturer"] = "WLED"; + device["model"] = "FOSS"; + device["sw_version"] = versionString; + + String temp; + serializeJson(doc, temp); + Serial.println(t); + Serial.println(temp); + + mqtt->publish(t.c_str(), 0, true, temp.c_str()); + } + public: void setup() { @@ -143,7 +184,7 @@ public: { // BME280 sensor MQTT publishing // Check if sensor present and MQTT Connected, otherwise it will crash the MCU - if (sensorType != 0 && mqtt != nullptr) + if (sensorType != 0 && WLED_MQTT_CONNECTED) { // Timer to fetch new temperature, humidity and pressure data at intervals timer = millis(); @@ -157,6 +198,12 @@ public: float temperature = roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); float humidity, heatIndex, dewPoint; + if (WLED_MQTT_CONNECTED && !mqttInitialized && HomeAssistantDiscovery) + { + _mqttInitialize(); + mqttInitialized = true; + } + // If temperature has changed since last measure, create string populated with device topic // from the UI and values read from sensor, then publish to broker if (temperature != lastTemperature || PublishAlways) @@ -213,4 +260,86 @@ public: } } } + + // Publish Sensor Information to Info Page + void addToJsonInfo(JsonObject &root) + { + JsonObject user = root[F("u")]; + if (user.isNull()) + user = root.createNestedObject(F("u")); + + if (sensorType==0) //No Sensor + { + // if we sensor not detected, let the user know + JsonArray temperature_json = user.createNestedArray("BME/BMP280 Sensor"); + temperature_json.add(F("Not Found")); + } + else if (sensorType==2) //BMP280 + { + + JsonArray temperature_json = user.createNestedArray("Temperature"); + JsonArray pressure_json = user.createNestedArray("Pressure"); + temperature_json.add(sensorTemperature); + temperature_json.add(F("°C")); + pressure_json.add(sensorPressure); + pressure_json.add(F("°C")); + } + else if (sensorType==1) //BME280 + { + JsonArray temperature_json = user.createNestedArray("Temperature"); + JsonArray humidity_json = user.createNestedArray("Humidity"); + JsonArray pressure_json = user.createNestedArray("Pressure"); + JsonArray heatindex_json = user.createNestedArray("Heat Index"); + JsonArray dewpoint_json = user.createNestedArray("Dew Point"); + temperature_json.add(sensorTemperature); + temperature_json.add(F("°C")); + humidity_json.add(sensorHumidity); + humidity_json.add(F("%")); + pressure_json.add(sensorPressure); + pressure_json.add(F("°C")); + heatindex_json.add(sensorHeatIndex); + heatindex_json.add(F("°C")); + dewpoint_json.add(sensorDewPoint); + dewpoint_json.add(F("°C")); + } + return; + } + + // Save Usermod Config Settings + void addToConfig(JsonObject& root) + { + JsonObject top = root.createNestedObject("BME280/BMP280"); + top["TemperatureDecimals"] = TemperatureDecimals; + top["HumidityDecimals"] = HumidityDecimals; + top["PressureDecimals"] = PressureDecimals; + top["TemperatureInterval"] = TemperatureInterval; + top["PublishAlways"] = PublishAlways; + top["HomeAssistantDiscovery"] = HomeAssistantDiscovery; + JsonArray pinArray = top.createNestedArray("pin-sda-scl"); + pinArray.add(SDA_PIN); + pinArray.add(SCL_PIN); + } + + // Read Usermod Config Settings + bool readFromConfig(JsonObject& root) + { + // default settings values could be set here (or below using the 3-argument getJsonValue()) instead of in the class definition or constructor + // setting them inside readFromConfig() is slightly more robust, handling the rare but plausible use case of single value being missing after boot (e.g. if the cfg.json was manually edited and a value was removed) + + JsonObject top = root["BME280/BMP280"]; + + bool configComplete = !top.isNull(); + + // A 3-argument getJsonValue() assigns the 3rd argument as a default value if the Json value is missing + configComplete &= getJsonValue(top["TemperatureDecimals"], TemperatureDecimals, 1); + configComplete &= getJsonValue(top["HumidityDecimals"], HumidityDecimals, 0); + configComplete &= getJsonValue(top["PressureDecimals"], PressureDecimals, 0); + configComplete &= getJsonValue(top["TemperatureInterval"], TemperatureInterval, 30); + configComplete &= getJsonValue(top["PublishAlways"], PublishAlways, false); + configComplete &= getJsonValue(top["HomeAssistantDiscovery"], HomeAssistantDiscovery, false); + configComplete &= getJsonValue(top["pin-sda-scl"][0], SDA_PIN, 21); //SDA + configComplete &= getJsonValue(top["pin-sda-scl"][1], SCL_PIN, 22); //SCL + + return configComplete; + } }; \ No newline at end of file From bee48dae7e57ea24dc590c4ddf8aeb893d5214a8 Mon Sep 17 00:00:00 2001 From: albarlow Date: Tue, 19 Jul 2022 14:37:05 +0100 Subject: [PATCH 05/12] Update readme --- usermods/BME280_v2/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/usermods/BME280_v2/README.md b/usermods/BME280_v2/README.md index 216ca630..253ae70c 100644 --- a/usermods/BME280_v2/README.md +++ b/usermods/BME280_v2/README.md @@ -15,6 +15,8 @@ or define `USERMOD_BME280` in `my_config.h` #define USERMOD_BME280 ``` +If you are using Home Assistant, there is an option to create a discoverable device with sensors for Pressure, Humidity, Temperature, Dew Point and Heat Index. This can be disabled if required. + Changes include: - Adjustable measure intervals - Temperature and pressure have separate intervals due to pressure not frequently changing at any constant altitude From 9d574397bcc4d062e1a7f20b7c94c89c23ba9fbf Mon Sep 17 00:00:00 2001 From: albarlow Date: Tue, 19 Jul 2022 21:47:56 +0100 Subject: [PATCH 06/12] usermod bme280 Added public variables to the BME280 usermod based on those in the Temperature usermod. Only complication is that this usermod utilises different function calls depending on whether user defines celsius or not. I have handled this for the temperature, but the Dew Point and Heat Index are relative to the temperature. I've also addressed some areas where I'd previously assumed Celsius for reporting purposes as my test case is using Farenheit. --- usermods/BME280_v2/usermod_bme280.h | 71 +++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/usermods/BME280_v2/usermod_bme280.h b/usermods/BME280_v2/usermod_bme280.h index 4f3853bb..353e451b 100644 --- a/usermods/BME280_v2/usermod_bme280.h +++ b/usermods/BME280_v2/usermod_bme280.h @@ -113,11 +113,23 @@ bool HomeAssistantDiscovery = false; // Publish Home Assistant Device Inform String t = String("homeassistant/sensor/") + mqttClientID + "/temperature/config"; - _createMqttSensor("Temperature", mqttTemperatureTopic, "temperature", "°C"); + #ifdef Celsius + _createMqttSensor("Temperature", mqttTemperatureTopic, "temperature", "°C"); + #else + _createMqttSensor("Temperature", mqttTemperatureTopic, "temperature", "°F"); + #endif _createMqttSensor("Pressure", mqttPressureTopic, "pressure", "hPa"); _createMqttSensor("Humidity", mqttHumidityTopic, "humidity", "%"); - _createMqttSensor("HeatIndex", mqttHeatIndexTopic, "temperature", "°C"); - _createMqttSensor("DewPoint", mqttDewPointTopic, "temperature", "°C"); + #ifdef Celsius + _createMqttSensor("HeatIndex", mqttHeatIndexTopic, "temperature", "°C"); + #else + _createMqttSensor("HeatIndex", mqttHeatIndexTopic, "temperature", "°F"); + #endif + #ifdef Celsius + _createMqttSensor("DewPoint", mqttDewPointTopic, "temperature", "°C"); + #else + _createMqttSensor("DewPoint", mqttDewPointTopic, "temperature", "°F"); + #endif } // Create an MQTT Sensor for Home Assistant Discovery purposes, this includes a pointer to the topic that is published to in the Loop. @@ -260,13 +272,43 @@ public: } } } + + /* + * API calls te enable data exchange between WLED modules + */ + inline float getTemperatureC() { + #ifdef Celsius + return (float)sensorTemperature; + #else + return (float)sensorTemperature * 1.8f + 32; + #endif + + } + inline float getTemperatureF() { + #ifdef Celsius + return ((float)sensorTemperature -32) * 0.56f; + #else + return (float)sensorTemperature; + #endif + } + inline float getHumidity() { + return (float)sensorHumidity; + } + inline float getPressure() { + return (float)sensorPressure; + } + inline float getDewPoint() { + return (float)sensorDewPoint; + } + inline float getHeatIndex() { + return (float)sensorHeatIndex; + } // Publish Sensor Information to Info Page void addToJsonInfo(JsonObject &root) { JsonObject user = root[F("u")]; - if (user.isNull()) - user = root.createNestedObject(F("u")); + if (user.isNull()) user = root.createNestedObject(F("u")); if (sensorType==0) //No Sensor { @@ -292,14 +334,27 @@ public: JsonArray heatindex_json = user.createNestedArray("Heat Index"); JsonArray dewpoint_json = user.createNestedArray("Dew Point"); temperature_json.add(sensorTemperature); - temperature_json.add(F("°C")); + #ifdef Celsius + temperature_json.add(F("°C")); + #else + temperature_json.add(F("°F")); + #endif humidity_json.add(sensorHumidity); humidity_json.add(F("%")); pressure_json.add(sensorPressure); - pressure_json.add(F("°C")); + pressure_json.add(F("hPa")); heatindex_json.add(sensorHeatIndex); - heatindex_json.add(F("°C")); + #ifdef Celsius + heatindex_json.add(F("°C")); + #else + heatindex_json.add(F("°F")); + #endif dewpoint_json.add(sensorDewPoint); + #ifdef Celsius + dewpoint_json.add(F("°C")); + #else + dewpoint_json.add(F("°F")); + #endif dewpoint_json.add(F("°C")); } return; From 866296fefd1f62e82343c6c4628c03f1efa43cbd Mon Sep 17 00:00:00 2001 From: albarlow Date: Tue, 19 Jul 2022 22:15:26 +0100 Subject: [PATCH 07/12] Update usermod bme280 rounding Apply rounding per usermod settings --- usermods/BME280_v2/usermod_bme280.h | 33 ++++++++++++++--------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/usermods/BME280_v2/usermod_bme280.h b/usermods/BME280_v2/usermod_bme280.h index 353e451b..dcf74893 100644 --- a/usermods/BME280_v2/usermod_bme280.h +++ b/usermods/BME280_v2/usermod_bme280.h @@ -278,30 +278,30 @@ public: */ inline float getTemperatureC() { #ifdef Celsius - return (float)sensorTemperature; + return (float)roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); #else - return (float)sensorTemperature * 1.8f + 32; + return (float)roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals) * 1.8f + 32; #endif } inline float getTemperatureF() { #ifdef Celsius - return ((float)sensorTemperature -32) * 0.56f; + return ((float)roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals) -32) * 0.56f; #else - return (float)sensorTemperature; + return (float)roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); #endif } inline float getHumidity() { - return (float)sensorHumidity; + return (float)roundf(sensorHumidity * pow(10, HumidityDecimals)); } inline float getPressure() { - return (float)sensorPressure; + return (float)roundf(sensorPressure * pow(10, PressureDecimals)); } inline float getDewPoint() { - return (float)sensorDewPoint; + return (float)roundf(sensorDewPoint * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); } inline float getHeatIndex() { - return (float)sensorHeatIndex; + return (float)roundf(sensorHeatIndex * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); } // Publish Sensor Information to Info Page @@ -321,10 +321,10 @@ public: JsonArray temperature_json = user.createNestedArray("Temperature"); JsonArray pressure_json = user.createNestedArray("Pressure"); - temperature_json.add(sensorTemperature); + temperature_json.add(roundf(sensorTemperature * pow(10, TemperatureDecimals))); temperature_json.add(F("°C")); - pressure_json.add(sensorPressure); - pressure_json.add(F("°C")); + pressure_json.add(roundf(sensorPressure * pow(10, PressureDecimals))); + pressure_json.add(F("hPa")); } else if (sensorType==1) //BME280 { @@ -333,29 +333,28 @@ public: JsonArray pressure_json = user.createNestedArray("Pressure"); JsonArray heatindex_json = user.createNestedArray("Heat Index"); JsonArray dewpoint_json = user.createNestedArray("Dew Point"); - temperature_json.add(sensorTemperature); + temperature_json.add(roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals)); #ifdef Celsius temperature_json.add(F("°C")); #else temperature_json.add(F("°F")); #endif - humidity_json.add(sensorHumidity); + humidity_json.add(roundf(sensorHumidity * pow(10, HumidityDecimals))); humidity_json.add(F("%")); - pressure_json.add(sensorPressure); + pressure_json.add(roundf(sensorPressure * pow(10, PressureDecimals))); pressure_json.add(F("hPa")); - heatindex_json.add(sensorHeatIndex); + heatindex_json.add(roundf(sensorHeatIndex * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals)); #ifdef Celsius heatindex_json.add(F("°C")); #else heatindex_json.add(F("°F")); #endif - dewpoint_json.add(sensorDewPoint); + dewpoint_json.add(roundf(sensorDewPoint * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals)); #ifdef Celsius dewpoint_json.add(F("°C")); #else dewpoint_json.add(F("°F")); #endif - dewpoint_json.add(F("°C")); } return; } From 22ac12dc3664b480aa7733f9896a059fa970475c Mon Sep 17 00:00:00 2001 From: albarlow Date: Wed, 20 Jul 2022 13:06:27 +0100 Subject: [PATCH 08/12] Remove compile-time variables and revamp readme.md --- usermods/BME280_v2/README.md | 76 +++++++----- usermods/BME280_v2/usermod_bme280.h | 174 ++++++++++++++++------------ 2 files changed, 149 insertions(+), 101 deletions(-) diff --git a/usermods/BME280_v2/README.md b/usermods/BME280_v2/README.md index 253ae70c..3d41c54b 100644 --- a/usermods/BME280_v2/README.md +++ b/usermods/BME280_v2/README.md @@ -1,42 +1,66 @@ -Hello! I have written a v2 usermod for the BME280/BMP280 sensor based on the [existing v1 usermod](https://github.com/Aircoookie/WLED/blob/master/usermods/Wemos_D1_mini%2BWemos32_mini_shield/usermod_bme280.cpp). It is not just a refactor, there are many changes which I made to fit my use case, and I hope they will fit the use cases of others as well! Most notably, this usermod is *just* for the BME280 and does not control a display like in the v1 usermod designed for the WeMos shield. +# Usermod BME280 +This Usermod is designed to read a `BME280` or `BMP280` sensor and output the following: +- Temperature +- Humidity (`BME280` only) +- Pressure +- Heat Index (`BME280` only) +- Dew Point (`BME280` only) -- Requires libraries `BME280@~3.0.0` (by [finitespace](https://github.com/finitespace/BME280)) and `Wire`. Please add these under `lib_deps` in your `platform.ini` (or `platform_override.ini`). -- Data is published over MQTT so make sure you've enabled the MQTT sync interface. +Configuration is all completed via the Usermod menu. There are no settings to set in code! The following settings can be configured in the Usermod Menu (remember to reboot for any changes made here to take effect): +- Temperature Decimals (number of decimal places to output) +- Humidity Decimals +- Pressure Decimals +- Temperature Interval (how many seconds between reads of temperature and humidity) +- Pressure Interval +- Publish Always (turn off to only publish changes, on to publish whether or not value changed) +- Use Celsius (turn off to use Farenheit) +- Home Assistant Discovery (turn on to sent MQTT Discovery entries for Home Assistant) +- SDA/SCL GPIO Pins + +Dependencies +- Libraries + - `BME280@~3.0.0` (by [finitespace](https://github.com/finitespace/BME280)) + - `Wire` + - These must be added under `lib_deps` in your `platform.ini` (or `platform_override.ini`). +- Data is published over MQTT - make sure you've enabled the MQTT sync interface. - This usermod also writes to serial (GPIO1 on ESP8266). Please make sure nothing else listening on the serial TX pin of your board will get confused by log messages! -To enable, compile with `USERMOD_BME280` defined (i.e. `platformio_override.ini`) +In addition to outputting via MQTT, you can read the values from the Info Screen on the dashboard page of the device's web interface. + +Methods also exist to read the read/calculated values from other WLED modules through code. +- `getTemperatureC()` +- `getTemperatureF()` +- `getHumidity()` +- `getPressure()` +- `getDewPointC()` +- `getDewPointF()` +- `getHeatIndexC()` +- `getHeatIndexF()` + +# Complilation + +To enable, compile with `USERMOD_BME280` defined (e.g. in `platformio_override.ini`) ```ini +[env:usermod_bme280_d1_mini] +extends = env:d1_mini build_flags = ${common.build_flags_esp8266} -D USERMOD_BME280 -``` -or define `USERMOD_BME280` in `my_config.h` -```c++ -#define USERMOD_BME280 +lib_deps = + ${esp8266.lib_deps} + BME280@~3.0.0 + Wire ``` -If you are using Home Assistant, there is an option to create a discoverable device with sensors for Pressure, Humidity, Temperature, Dew Point and Heat Index. This can be disabled if required. -Changes include: -- Adjustable measure intervals - - Temperature and pressure have separate intervals due to pressure not frequently changing at any constant altitude -- Adjustment of number of decimal places in published sensor values - - Separate adjustment for temperature, humidity and pressure values - - Values are rounded to the specified number of decimal places -- Pressure measured in units of hPa instead of Pa -- Calculation of heat index (apparent temperature) and dew point - - These, along with humidity measurements, are disabled if the sensor is a BMP280 -- 16x oversampling of sensor during measurement -- Values are only published if they are different from the previous value -- Values are published on startup (continually until the MQTT broker acknowledges a successful publication) - -Adjustments are made through preprocessor definitions at the start of the class definition. - -MQTT topics are as follows: +# MQTT +MQTT topics are as follows (`` is set in MQTT section of Sync Setup menu): Measurement type | MQTT topic --- | --- Temperature | `/temperature` Humidity | `/humidity` Pressure | `/pressure` Heat index | `/heat_index` -Dew point | `/dew_point` \ No newline at end of file +Dew point | `/dew_point` + +If you are using Home Assistant, and `Home Assistant Discovery` is turned on, Home Assistant should automatically detect a new device, provided you have the MQTT integration installed. The device is seperate from the main WLED device and will contain sensors for Pressure, Humidity, Temperature, Dew Point and Heat Index. \ No newline at end of file diff --git a/usermods/BME280_v2/usermod_bme280.h b/usermods/BME280_v2/usermod_bme280.h index dcf74893..399cc274 100644 --- a/usermods/BME280_v2/usermod_bme280.h +++ b/usermods/BME280_v2/usermod_bme280.h @@ -12,24 +12,28 @@ class UsermodBME280 : public Usermod { private: -// User-defined configuration -#define Celsius // Show temperature mesaurement in Celcius. Comment out for Fahrenheit -unsigned long TemperatureDecimals = 0; // Number of decimal places in published temperaure values -unsigned long HumidityDecimals = 0; // Number of decimal places in published humidity values -unsigned long PressureDecimals = 0; // Number of decimal places in published pressure values -unsigned long TemperatureInterval = 5; // Interval to measure temperature (and humidity, dew point if available) in seconds -unsigned long PressureInterval = 300; // Interval to measure pressure in seconds -bool PublishAlways = false; // Publish values even when they have not changed -bool HomeAssistantDiscovery = false; // Publish Home Assistant Device Information + + // NOTE: Do not implement any compile-time variables, anything the user needs to configure + // should be configurable from the Usermod menu using the methods below + // key settings set via usermod menu + unsigned long TemperatureDecimals = 0; // Number of decimal places in published temperaure values + unsigned long HumidityDecimals = 0; // Number of decimal places in published humidity values + unsigned long PressureDecimals = 0; // Number of decimal places in published pressure values + unsigned long TemperatureInterval = 5; // Interval to measure temperature (and humidity, dew point if available) in seconds + unsigned long PressureInterval = 300; // Interval to measure pressure in seconds + bool PublishAlways = false; // Publish values even when they have not changed + bool UseCelsius = true; // Use Celsius for Reporting + bool HomeAssistantDiscovery = false; // Publish Home Assistant Device Information -#ifdef ARDUINO_ARCH_ESP32 // ESP32 boards - uint8_t SCL_PIN = 22; - uint8_t SDA_PIN = 21; -#else // ESP8266 boards - uint8_t SCL_PIN = 5; - uint8_t SDA_PIN = 4; - //uint8_t RST_PIN = 16; // Uncoment for Heltec WiFi-Kit-8 -#endif + // set the default pins based on the architecture, these get overridden by Usermod menu settings + #ifdef ARDUINO_ARCH_ESP32 // ESP32 boards + uint8_t SCL_PIN = 22; + uint8_t SDA_PIN = 21; + #else // ESP8266 boards + uint8_t SCL_PIN = 5; + uint8_t SDA_PIN = 4; + //uint8_t RST_PIN = 16; // Uncoment for Heltec WiFi-Kit-8 + #endif // BME280 sensor settings BME280I2C::Settings settings{ @@ -59,6 +63,7 @@ bool HomeAssistantDiscovery = false; // Publish Home Assistant Device Inform float sensorHeatIndex; float sensorDewPoint; float sensorPressure; + String tempScale; // Track previous sensor values float lastTemperature; float lastHumidity; @@ -78,31 +83,47 @@ bool HomeAssistantDiscovery = false; // Publish Home Assistant Device Inform uint16_t mqttTemperaturePub = 0; uint16_t mqttPressurePub = 0; + // Read the BME280/BMP280 Sensor (which one runs depends on whether Celsius or Farenheit being set in Usermod Menu) void UpdateBME280Data(int SensorType) { float _temperature, _humidity, _pressure; - #ifdef Celsius + + if (UseCelsius) { BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); EnvironmentCalculations::TempUnit envTempUnit(EnvironmentCalculations::TempUnit_Celsius); - #else + BME280::PresUnit presUnit(BME280::PresUnit_hPa); + + bme.read(_pressure, _temperature, _humidity, tempUnit, presUnit); + + sensorTemperature = _temperature; + sensorHumidity = _humidity; + sensorPressure = _pressure; + tempScale = "°C"; + if (sensorType == 1) + { + sensorHeatIndex = EnvironmentCalculations::HeatIndex(_temperature, _humidity, envTempUnit); + sensorDewPoint = EnvironmentCalculations::DewPoint(_temperature, _humidity, envTempUnit); + } + } else { BME280::TempUnit tempUnit(BME280::TempUnit_Fahrenheit); EnvironmentCalculations::TempUnit envTempUnit(EnvironmentCalculations::TempUnit_Fahrenheit); - #endif - BME280::PresUnit presUnit(BME280::PresUnit_hPa); + BME280::PresUnit presUnit(BME280::PresUnit_hPa); - bme.read(_pressure, _temperature, _humidity, tempUnit, presUnit); + bme.read(_pressure, _temperature, _humidity, tempUnit, presUnit); - sensorTemperature = _temperature; - sensorHumidity = _humidity; - sensorPressure = _pressure; - if (sensorType == 1) - { - sensorHeatIndex = EnvironmentCalculations::HeatIndex(_temperature, _humidity, envTempUnit); - sensorDewPoint = EnvironmentCalculations::DewPoint(_temperature, _humidity, envTempUnit); + sensorTemperature = _temperature; + sensorHumidity = _humidity; + sensorPressure = _pressure; + tempScale = "°F"; + if (sensorType == 1) + { + sensorHeatIndex = EnvironmentCalculations::HeatIndex(_temperature, _humidity, envTempUnit); + sensorDewPoint = EnvironmentCalculations::DewPoint(_temperature, _humidity, envTempUnit); + } } } - // Procedure to define all MQTT Topics + // Procedure to define all MQTT discovery Topics void _mqttInitialize() { mqttTemperatureTopic = String(mqttDeviceTopic) + "/temperature"; @@ -111,25 +132,15 @@ bool HomeAssistantDiscovery = false; // Publish Home Assistant Device Inform mqttHeatIndexTopic = String(mqttDeviceTopic) + "/heat_index"; mqttDewPointTopic = String(mqttDeviceTopic) + "/dew_point"; - String t = String("homeassistant/sensor/") + mqttClientID + "/temperature/config"; + if (HomeAssistantDiscovery) { + String t = String("homeassistant/sensor/") + mqttClientID + "/temperature/config"; - #ifdef Celsius - _createMqttSensor("Temperature", mqttTemperatureTopic, "temperature", "°C"); - #else - _createMqttSensor("Temperature", mqttTemperatureTopic, "temperature", "°F"); - #endif - _createMqttSensor("Pressure", mqttPressureTopic, "pressure", "hPa"); - _createMqttSensor("Humidity", mqttHumidityTopic, "humidity", "%"); - #ifdef Celsius - _createMqttSensor("HeatIndex", mqttHeatIndexTopic, "temperature", "°C"); - #else - _createMqttSensor("HeatIndex", mqttHeatIndexTopic, "temperature", "°F"); - #endif - #ifdef Celsius - _createMqttSensor("DewPoint", mqttDewPointTopic, "temperature", "°C"); - #else - _createMqttSensor("DewPoint", mqttDewPointTopic, "temperature", "°F"); - #endif + _createMqttSensor("Temperature", mqttTemperatureTopic, "temperature", tempScale); + _createMqttSensor("Pressure", mqttPressureTopic, "pressure", "hPa"); + _createMqttSensor("Humidity", mqttHumidityTopic, "humidity", "%"); + _createMqttSensor("HeatIndex", mqttHeatIndexTopic, "temperature", tempScale); + _createMqttSensor("DewPoint", mqttDewPointTopic, "temperature", tempScale); + } } // Create an MQTT Sensor for Home Assistant Discovery purposes, this includes a pointer to the topic that is published to in the Loop. @@ -210,7 +221,7 @@ public: float temperature = roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); float humidity, heatIndex, dewPoint; - if (WLED_MQTT_CONNECTED && !mqttInitialized && HomeAssistantDiscovery) + if (WLED_MQTT_CONNECTED && !mqttInitialized) { _mqttInitialize(); mqttInitialized = true; @@ -277,19 +288,19 @@ public: * API calls te enable data exchange between WLED modules */ inline float getTemperatureC() { - #ifdef Celsius + if (UseCelsius) { return (float)roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); - #else + } else { return (float)roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals) * 1.8f + 32; - #endif + } } inline float getTemperatureF() { - #ifdef Celsius + if (UseCelsius) { return ((float)roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals) -32) * 0.56f; - #else + } else { return (float)roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); - #endif + } } inline float getHumidity() { return (float)roundf(sensorHumidity * pow(10, HumidityDecimals)); @@ -297,11 +308,32 @@ public: inline float getPressure() { return (float)roundf(sensorPressure * pow(10, PressureDecimals)); } - inline float getDewPoint() { - return (float)roundf(sensorDewPoint * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); + inline float getDewPointC() { + if (UseCelsius) { + return (float)roundf(sensorDewPoint * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); + } else { + return (float)roundf(sensorDewPoint * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals) * 1.8f + 32; + } } - inline float getHeatIndex() { - return (float)roundf(sensorHeatIndex * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); + inline float getDewPointF() { + if (UseCelsius) { + return ((float)roundf(sensorDewPoint * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals) -32) * 0.56f; + } else { + return (float)roundf(sensorDewPoint * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); + } + } + inline float getHeatIndexC() { + if (UseCelsius) { + return (float)roundf(sensorHeatIndex * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); + } else { + return (float)roundf(sensorHeatIndex * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals) * 1.8f + 32; + } + }inline float getHeatIndexF() { + if (UseCelsius) { + return ((float)roundf(sensorHeatIndex * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals) -32) * 0.56f; + } else { + return (float)roundf(sensorHeatIndex * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); + } } // Publish Sensor Information to Info Page @@ -322,7 +354,7 @@ public: JsonArray temperature_json = user.createNestedArray("Temperature"); JsonArray pressure_json = user.createNestedArray("Pressure"); temperature_json.add(roundf(sensorTemperature * pow(10, TemperatureDecimals))); - temperature_json.add(F("°C")); + temperature_json.add(tempScale); pressure_json.add(roundf(sensorPressure * pow(10, PressureDecimals))); pressure_json.add(F("hPa")); } @@ -334,27 +366,15 @@ public: JsonArray heatindex_json = user.createNestedArray("Heat Index"); JsonArray dewpoint_json = user.createNestedArray("Dew Point"); temperature_json.add(roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals)); - #ifdef Celsius - temperature_json.add(F("°C")); - #else - temperature_json.add(F("°F")); - #endif + temperature_json.add(tempScale); humidity_json.add(roundf(sensorHumidity * pow(10, HumidityDecimals))); humidity_json.add(F("%")); pressure_json.add(roundf(sensorPressure * pow(10, PressureDecimals))); pressure_json.add(F("hPa")); heatindex_json.add(roundf(sensorHeatIndex * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals)); - #ifdef Celsius - heatindex_json.add(F("°C")); - #else - heatindex_json.add(F("°F")); - #endif + heatindex_json.add(tempScale); dewpoint_json.add(roundf(sensorDewPoint * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals)); - #ifdef Celsius - dewpoint_json.add(F("°C")); - #else - dewpoint_json.add(F("°F")); - #endif + dewpoint_json.add(tempScale); } return; } @@ -367,7 +387,9 @@ public: top["HumidityDecimals"] = HumidityDecimals; top["PressureDecimals"] = PressureDecimals; top["TemperatureInterval"] = TemperatureInterval; + top["PressureInterval"] = PressureInterval; top["PublishAlways"] = PublishAlways; + top["UseCelsius"] = UseCelsius; top["HomeAssistantDiscovery"] = HomeAssistantDiscovery; JsonArray pinArray = top.createNestedArray("pin-sda-scl"); pinArray.add(SDA_PIN); @@ -389,7 +411,9 @@ public: configComplete &= getJsonValue(top["HumidityDecimals"], HumidityDecimals, 0); configComplete &= getJsonValue(top["PressureDecimals"], PressureDecimals, 0); configComplete &= getJsonValue(top["TemperatureInterval"], TemperatureInterval, 30); + configComplete &= getJsonValue(top["PressureInterval"], PressureInterval, 30); configComplete &= getJsonValue(top["PublishAlways"], PublishAlways, false); + configComplete &= getJsonValue(top["UseCelsius"], UseCelsius, true); configComplete &= getJsonValue(top["HomeAssistantDiscovery"], HomeAssistantDiscovery, false); configComplete &= getJsonValue(top["pin-sda-scl"][0], SDA_PIN, 21); //SDA configComplete &= getJsonValue(top["pin-sda-scl"][1], SCL_PIN, 22); //SCL From 7308f5993c788842db4d5aa2b34aa92b5a0e52a9 Mon Sep 17 00:00:00 2001 From: albarlow Date: Wed, 20 Jul 2022 22:53:45 +0100 Subject: [PATCH 09/12] Added changelog --- usermods/BME280_v2/README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/usermods/BME280_v2/README.md b/usermods/BME280_v2/README.md index 3d41c54b..395c2039 100644 --- a/usermods/BME280_v2/README.md +++ b/usermods/BME280_v2/README.md @@ -63,4 +63,25 @@ Pressure | `/pressure` Heat index | `/heat_index` Dew point | `/dew_point` -If you are using Home Assistant, and `Home Assistant Discovery` is turned on, Home Assistant should automatically detect a new device, provided you have the MQTT integration installed. The device is seperate from the main WLED device and will contain sensors for Pressure, Humidity, Temperature, Dew Point and Heat Index. \ No newline at end of file +If you are using Home Assistant, and `Home Assistant Discovery` is turned on, Home Assistant should automatically detect a new device, provided you have the MQTT integration installed. The device is seperate from the main WLED device and will contain sensors for Pressure, Humidity, Temperature, Dew Point and Heat Index. + +# Revision History +Jul 2022 +- Added Home Assistant Discovery +- Added API interface to output data +- Removed compile-time variables +- Added usermod menu interface +- Added value outputs to info screen +- Updated `readme.md` + +Apr 2021 +- Added `Publish Always` option + +Dec 2020 +- Ported to V2 Usermod format +- Customisable `measure intervals` +- Customisable number of `decimal places` in published sensor values +- Pressure measured in units of hPa instead of Pa +- Calculation of heat index (apparent temperature) and dew point +- `16x oversampling` of sensor during measurement +- Values only published if they are different from the previous value \ No newline at end of file From c96f83b0766efb47ae57ef56fe85432bcab2e786 Mon Sep 17 00:00:00 2001 From: albarlow Date: Thu, 21 Jul 2022 09:48:37 +0100 Subject: [PATCH 10/12] Registered Usermod and Pins Implemented PinManager and rerun setup of usermod after updating pins/ Registered in const.h and pin_manager.h I tried to follow the existing formatting/numbering in these files. Wrapped any strings I could in F() --- usermods/BME280_v2/README.md | 7 +- usermods/BME280_v2/usermod_bme280.h | 143 ++++++++++++++++++---------- wled00/const.h | 1 + wled00/pin_manager.h | 3 +- 4 files changed, 99 insertions(+), 55 deletions(-) diff --git a/usermods/BME280_v2/README.md b/usermods/BME280_v2/README.md index 395c2039..b8718e0e 100644 --- a/usermods/BME280_v2/README.md +++ b/usermods/BME280_v2/README.md @@ -6,7 +6,7 @@ This Usermod is designed to read a `BME280` or `BMP280` sensor and output the fo - Heat Index (`BME280` only) - Dew Point (`BME280` only) -Configuration is all completed via the Usermod menu. There are no settings to set in code! The following settings can be configured in the Usermod Menu (remember to reboot for any changes made here to take effect): +Configuration is all completed via the Usermod menu. There are no settings to set in code! The following settings can be configured in the Usermod Menu: - Temperature Decimals (number of decimal places to output) - Humidity Decimals - Pressure Decimals @@ -15,7 +15,7 @@ Configuration is all completed via the Usermod menu. There are no settings to s - Publish Always (turn off to only publish changes, on to publish whether or not value changed) - Use Celsius (turn off to use Farenheit) - Home Assistant Discovery (turn on to sent MQTT Discovery entries for Home Assistant) -- SDA/SCL GPIO Pins +- SCL/SDA GPIO Pins Dependencies - Libraries @@ -73,6 +73,9 @@ Jul 2022 - Added usermod menu interface - Added value outputs to info screen - Updated `readme.md` +- Registered usermod +- Implemented PinManager for usermod +- Implemented reallocation of pins without reboot Apr 2021 - Added `Publish Always` option diff --git a/usermods/BME280_v2/usermod_bme280.h b/usermods/BME280_v2/usermod_bme280.h index 399cc274..912dce8a 100644 --- a/usermods/BME280_v2/usermod_bme280.h +++ b/usermods/BME280_v2/usermod_bme280.h @@ -27,13 +27,15 @@ private: // set the default pins based on the architecture, these get overridden by Usermod menu settings #ifdef ARDUINO_ARCH_ESP32 // ESP32 boards - uint8_t SCL_PIN = 22; - uint8_t SDA_PIN = 21; + #define HW_PIN_SCL 22 + #define HW_PIN_SDA 21 #else // ESP8266 boards - uint8_t SCL_PIN = 5; - uint8_t SDA_PIN = 4; + #define HW_PIN_SCL 5 + #define HW_PIN_SDA 4 //uint8_t RST_PIN = 16; // Uncoment for Heltec WiFi-Kit-8 #endif + int8_t ioPin[2] = {HW_PIN_SCL, HW_PIN_SDA}; // I2C pins: SCL, SDA...defaults to Arch hardware pins but overridden at setup() + bool initDone = false; // BME280 sensor settings BME280I2C::Settings settings{ @@ -133,13 +135,11 @@ private: mqttDewPointTopic = String(mqttDeviceTopic) + "/dew_point"; if (HomeAssistantDiscovery) { - String t = String("homeassistant/sensor/") + mqttClientID + "/temperature/config"; - - _createMqttSensor("Temperature", mqttTemperatureTopic, "temperature", tempScale); - _createMqttSensor("Pressure", mqttPressureTopic, "pressure", "hPa"); - _createMqttSensor("Humidity", mqttHumidityTopic, "humidity", "%"); - _createMqttSensor("HeatIndex", mqttHeatIndexTopic, "temperature", tempScale); - _createMqttSensor("DewPoint", mqttDewPointTopic, "temperature", tempScale); + _createMqttSensor(F("Temperature"), mqttTemperatureTopic, F("temperature"), tempScale); + _createMqttSensor(F("Pressure"), mqttPressureTopic, F("pressure"), F("hPa")); + _createMqttSensor(F("Humidity"), mqttHumidityTopic, F("humidity"), F("%")); + _createMqttSensor(F("HeatIndex"), mqttHeatIndexTopic, F("temperature"), tempScale); + _createMqttSensor(F("DewPoint"), mqttDewPointTopic, F("temperature"), tempScale); } } @@ -150,21 +150,21 @@ private: StaticJsonDocument<600> doc; - doc["name"] = String(serverDescription) + " " + name; - doc["state_topic"] = topic; - doc["unique_id"] = String(mqttClientID) + name; + doc[F("name")] = String(serverDescription) + " " + name; + doc[F("state_topic")] = topic; + doc[F("unique_id")] = String(mqttClientID) + name; if (unitOfMeasurement != "") - doc["unit_of_measurement"] = unitOfMeasurement; + doc[F("unit_of_measurement")] = unitOfMeasurement; if (deviceClass != "") - doc["device_class"] = deviceClass; - doc["expire_after"] = 1800; + doc[F("device_class")] = deviceClass; + doc[F("expire_after")] = 1800; - JsonObject device = doc.createNestedObject("device"); // attach the sensor to the same device - device["name"] = serverDescription; - device["identifiers"] = "wled-sensor-" + String(mqttClientID); - device["manufacturer"] = "WLED"; - device["model"] = "FOSS"; - device["sw_version"] = versionString; + JsonObject device = doc.createNestedObject(F("device")); // attach the sensor to the same device + device[F("name")] = serverDescription; + device[F("identifiers")] = "wled-sensor-" + String(mqttClientID); + device[F("manufacturer")] = F("WLED"); + device[F("model")] = F("FOSS"); + device[F("sw_version")] = versionString; String temp; serializeJson(doc, temp); @@ -177,12 +177,18 @@ private: public: void setup() { - Wire.begin(SDA_PIN, SCL_PIN); + bool HW_Pins_Used = (ioPin[0]==HW_PIN_SCL && ioPin[1]==HW_PIN_SDA); // note whether architecture-based hardware SCL/SDA pins used + PinOwner po = PinOwner::UM_BME280; // defaults to being pinowner for SCL/SDA pins + PinManagerPinType pins[2] = { { ioPin[0], true }, { ioPin[1], true } }; // allocate pins + if (HW_Pins_Used) po = PinOwner::HW_I2C; // allow multiple allocations of HW I2C bus pins + if (!pinManager.allocateMultiplePins(pins, 2, po)) { sensorType=0; return; } + + Wire.begin(ioPin[1], ioPin[0]); if (!bme.begin()) { sensorType = 0; - Serial.println("Could not find BME280I2C sensor!"); + Serial.println(F("Could not find BME280I2C sensor!")); } else { @@ -190,17 +196,18 @@ public: { case BME280::ChipModel_BME280: sensorType = 1; - Serial.println("Found BME280 sensor! Success."); + Serial.println(F("Found BME280 sensor! Success.")); break; case BME280::ChipModel_BMP280: sensorType = 2; - Serial.println("Found BMP280 sensor! No Humidity available."); + Serial.println(F("Found BMP280 sensor! No Humidity available.")); break; default: sensorType = 0; - Serial.println("Found UNKNOWN sensor! Error!"); + Serial.println(F("Found UNKNOWN sensor! Error!")); } } + initDone=true; } void loop() @@ -345,7 +352,7 @@ public: if (sensorType==0) //No Sensor { // if we sensor not detected, let the user know - JsonArray temperature_json = user.createNestedArray("BME/BMP280 Sensor"); + JsonArray temperature_json = user.createNestedArray(F("BME/BMP280 Sensor")); temperature_json.add(F("Not Found")); } else if (sensorType==2) //BMP280 @@ -382,18 +389,19 @@ public: // Save Usermod Config Settings void addToConfig(JsonObject& root) { - JsonObject top = root.createNestedObject("BME280/BMP280"); - top["TemperatureDecimals"] = TemperatureDecimals; - top["HumidityDecimals"] = HumidityDecimals; - top["PressureDecimals"] = PressureDecimals; - top["TemperatureInterval"] = TemperatureInterval; - top["PressureInterval"] = PressureInterval; - top["PublishAlways"] = PublishAlways; - top["UseCelsius"] = UseCelsius; - top["HomeAssistantDiscovery"] = HomeAssistantDiscovery; - JsonArray pinArray = top.createNestedArray("pin-sda-scl"); - pinArray.add(SDA_PIN); - pinArray.add(SCL_PIN); + JsonObject top = root.createNestedObject(F("BME280/BMP280")); + top[F("TemperatureDecimals")] = TemperatureDecimals; + top[F("HumidityDecimals")] = HumidityDecimals; + top[F("PressureDecimals")] = PressureDecimals; + top[F("TemperatureInterval")] = TemperatureInterval; + top[F("PressureInterval")] = PressureInterval; + top[F("PublishAlways")] = PublishAlways; + top[F("UseCelsius")] = UseCelsius; + top[F("HomeAssistantDiscovery")] = HomeAssistantDiscovery; + JsonArray io_pin = top.createNestedArray(F("pin")); + for (byte i=0; i<2; i++) io_pin.add(ioPin[i]); + top[F("help4Pins")] = F("SCL,SDA"); // help for Settings page + DEBUG_PRINTLN(F("BME280 config saved.")); } // Read Usermod Config Settings @@ -402,22 +410,53 @@ public: // default settings values could be set here (or below using the 3-argument getJsonValue()) instead of in the class definition or constructor // setting them inside readFromConfig() is slightly more robust, handling the rare but plausible use case of single value being missing after boot (e.g. if the cfg.json was manually edited and a value was removed) - JsonObject top = root["BME280/BMP280"]; + int8_t newPin[2]; for (byte i=0; i<2; i++) newPin[i] = ioPin[i]; // prepare to note changed pins + + JsonObject top = root[F("BME280/BMP280")]; + if (top.isNull()) { + DEBUG_PRINT(F("BME280/BMP280")); + DEBUG_PRINTLN(F(": No config found. (Using defaults.)")); + return false; + } bool configComplete = !top.isNull(); // A 3-argument getJsonValue() assigns the 3rd argument as a default value if the Json value is missing - configComplete &= getJsonValue(top["TemperatureDecimals"], TemperatureDecimals, 1); - configComplete &= getJsonValue(top["HumidityDecimals"], HumidityDecimals, 0); - configComplete &= getJsonValue(top["PressureDecimals"], PressureDecimals, 0); - configComplete &= getJsonValue(top["TemperatureInterval"], TemperatureInterval, 30); - configComplete &= getJsonValue(top["PressureInterval"], PressureInterval, 30); - configComplete &= getJsonValue(top["PublishAlways"], PublishAlways, false); - configComplete &= getJsonValue(top["UseCelsius"], UseCelsius, true); - configComplete &= getJsonValue(top["HomeAssistantDiscovery"], HomeAssistantDiscovery, false); - configComplete &= getJsonValue(top["pin-sda-scl"][0], SDA_PIN, 21); //SDA - configComplete &= getJsonValue(top["pin-sda-scl"][1], SCL_PIN, 22); //SCL + configComplete &= getJsonValue(top[F("TemperatureDecimals")], TemperatureDecimals, 1); + configComplete &= getJsonValue(top[F("HumidityDecimals")], HumidityDecimals, 0); + configComplete &= getJsonValue(top[F("PressureDecimals")], PressureDecimals, 0); + configComplete &= getJsonValue(top[F("TemperatureInterval")], TemperatureInterval, 30); + configComplete &= getJsonValue(top[F("PressureInterval")], PressureInterval, 30); + configComplete &= getJsonValue(top[F("PublishAlways")], PublishAlways, false); + configComplete &= getJsonValue(top[F("UseCelsius")], UseCelsius, true); + configComplete &= getJsonValue(top[F("HomeAssistantDiscovery")], HomeAssistantDiscovery, false); + for (byte i=0; i<2; i++) configComplete &= getJsonValue(top[F("pin")][i], newPin[i], ioPin[i]); + + DEBUG_PRINT(FPSTR(F("BME280/BMP280"))); + if (!initDone) { + // first run: reading from cfg.json + for (byte i=0; i<2; i++) ioPin[i] = newPin[i]; + DEBUG_PRINTLN(F(" config loaded.")); + } else { + DEBUG_PRINTLN(F(" config (re)loaded.")); + // changing parameters from settings page + bool pinsChanged = false; + for (byte i=0; i<2; i++) if (ioPin[i] != newPin[i]) { pinsChanged = true; break; } // check if any pins changed + if (pinsChanged) { //if pins changed, deallocate old pins and allocate new ones + PinOwner po = PinOwner::UM_BME280; + if (ioPin[0]==HW_PIN_SCL && ioPin[1]==HW_PIN_SDA) po = PinOwner::HW_I2C; // allow multiple allocations of HW I2C bus pins + pinManager.deallocateMultiplePins((const uint8_t *)ioPin, 2, po); // deallocate pins + for (byte i=0; i<2; i++) ioPin[i] = newPin[i]; + setup(); + } + // use "return !top["newestParameter"].isNull();" when updating Usermod with new features + return !top[F("pin")].isNull(); + } return configComplete; } + + uint16_t getId() { + return USERMOD_ID_BME280; + } }; \ No newline at end of file diff --git a/wled00/const.h b/wled00/const.h index 6b9517d9..bf119a58 100644 --- a/wled00/const.h +++ b/wled00/const.h @@ -76,6 +76,7 @@ #define USERMOD_ID_WORDCLOCK 27 //Usermod "usermod_v2_word_clock.h" #define USERMOD_ID_MY9291 28 //Usermod "usermod_MY9291.h" #define USERMOD_ID_SI7021_MQTT_HA 29 //Usermod "usermod_si7021_mqtt_ha.h" +#define USERMOD_ID_BME280 30 //Usermod "usermod_bme280.h //Access point behavior #define AP_BEHAVIOR_BOOT_NO_CONN 0 //Open AP when no connection after boot diff --git a/wled00/pin_manager.h b/wled00/pin_manager.h index 5f009425..e9e77c14 100644 --- a/wled00/pin_manager.h +++ b/wled00/pin_manager.h @@ -53,7 +53,8 @@ enum struct PinOwner : uint8_t { // #define USERMOD_ID_ELEKSTUBE_IPS // 0x10 // Usermod "usermod_elekstube_ips.h" -- Uses quite a few pins ... see Hardware.h and User_Setup.h // #define USERMOD_ID_SN_PHOTORESISTOR // 0x11 // Usermod "usermod_sn_photoresistor.h" -- Uses hard-coded pin (PHOTORESISTOR_PIN == A0), but could be easily updated to use pinManager UM_RGBRotaryEncoder = USERMOD_RGB_ROTARY_ENCODER, // 0x16 // Usermod "rgb-rotary-encoder.h" - UM_QuinLEDAnPenta = USERMOD_ID_QUINLED_AN_PENTA // 0x17 // Usermod "quinled-an-penta.h" + UM_QuinLEDAnPenta = USERMOD_ID_QUINLED_AN_PENTA, // 0x17 // Usermod "quinled-an-penta.h" + UM_BME280 = USERMOD_ID_BME280 // 0x18 // Usermod "usermod_bme280.h -- Uses "standard" HW_I2C pins }; static_assert(0u == static_cast(PinOwner::None), "PinOwner::None must be zero, so default array initialization works as expected"); From 51d935f419b858bb76d2ddecdb33b26b27cafeed Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 21 Jul 2022 11:24:54 +0200 Subject: [PATCH 11/12] Bump terser from 4.8.0 to 4.8.1 (#2726) Bumps [terser](https://github.com/terser/terser) from 4.8.0 to 4.8.1. - [Release notes](https://github.com/terser/terser/releases) - [Changelog](https://github.com/terser/terser/blob/master/CHANGELOG.md) - [Commits](https://github.com/terser/terser/commits) --- updated-dependencies: - dependency-name: terser dependency-type: indirect ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- package-lock.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 80b11afe..082645d4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2067,9 +2067,9 @@ "integrity": "sha512-a6sumDlzyHVJWb8+YofY4TW112G6p2FCPEAFk+59gIYHv3XHRhm9ltVQ9kli4hNWeQBwSpe8cRN25x0ROunMOw==" }, "terser": { - "version": "4.8.0", - "resolved": "https://registry.npmjs.org/terser/-/terser-4.8.0.tgz", - "integrity": "sha512-EAPipTNeWsb/3wLPeup1tVPaXfIaU68xMnVdPafIL1TV05OhASArYyIfFvnvJCNrR2NIOvDVNNTFRa+Re2MWyw==", + "version": "4.8.1", + "resolved": "https://registry.npmjs.org/terser/-/terser-4.8.1.tgz", + "integrity": "sha512-4GnLC0x667eJG0ewJTa6z/yXrbLGv80D9Ru6HIpCQmO+Q4PfEtBFi0ObSckqwL6VyQv/7ENJieXHo2ANmdQwgw==", "requires": { "commander": "^2.20.0", "source-map": "~0.6.1", From bda3c4ab7a7ecaa6cf5c8849f248235caec30bc6 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Thu, 21 Jul 2022 18:38:07 +0200 Subject: [PATCH 12/12] Minor optimisations. --- usermods/BME280_v2/usermod_bme280.h | 98 ++++++++++++++--------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/usermods/BME280_v2/usermod_bme280.h b/usermods/BME280_v2/usermod_bme280.h index 912dce8a..742066f7 100644 --- a/usermods/BME280_v2/usermod_bme280.h +++ b/usermods/BME280_v2/usermod_bme280.h @@ -128,11 +128,11 @@ private: // Procedure to define all MQTT discovery Topics void _mqttInitialize() { - mqttTemperatureTopic = String(mqttDeviceTopic) + "/temperature"; - mqttPressureTopic = String(mqttDeviceTopic) + "/pressure"; - mqttHumidityTopic = String(mqttDeviceTopic) + "/humidity"; - mqttHeatIndexTopic = String(mqttDeviceTopic) + "/heat_index"; - mqttDewPointTopic = String(mqttDeviceTopic) + "/dew_point"; + mqttTemperatureTopic = String(mqttDeviceTopic) + F("/temperature"); + mqttPressureTopic = String(mqttDeviceTopic) + F("/pressure"); + mqttHumidityTopic = String(mqttDeviceTopic) + F("/humidity"); + mqttHeatIndexTopic = String(mqttDeviceTopic) + F("/heat_index"); + mqttDewPointTopic = String(mqttDeviceTopic) + F("/dew_point"); if (HomeAssistantDiscovery) { _createMqttSensor(F("Temperature"), mqttTemperatureTopic, F("temperature"), tempScale); @@ -146,7 +146,7 @@ private: // Create an MQTT Sensor for Home Assistant Discovery purposes, this includes a pointer to the topic that is published to in the Loop. void _createMqttSensor(const String &name, const String &topic, const String &deviceClass, const String &unitOfMeasurement) { - String t = String("homeassistant/sensor/") + mqttClientID + "/" + name + "/config"; + String t = String(F("homeassistant/sensor/")) + mqttClientID + F("/") + name + F("/config"); StaticJsonDocument<600> doc; @@ -168,8 +168,8 @@ private: String temp; serializeJson(doc, temp); - Serial.println(t); - Serial.println(temp); + DEBUG_PRINTLN(t); + DEBUG_PRINTLN(temp); mqtt->publish(t.c_str(), 0, true, temp.c_str()); } @@ -188,7 +188,7 @@ public: if (!bme.begin()) { sensorType = 0; - Serial.println(F("Could not find BME280I2C sensor!")); + DEBUG_PRINTLN(F("Could not find BME280I2C sensor!")); } else { @@ -196,15 +196,15 @@ public: { case BME280::ChipModel_BME280: sensorType = 1; - Serial.println(F("Found BME280 sensor! Success.")); + DEBUG_PRINTLN(F("Found BME280 sensor! Success.")); break; case BME280::ChipModel_BMP280: sensorType = 2; - Serial.println(F("Found BMP280 sensor! No Humidity available.")); + DEBUG_PRINTLN(F("Found BMP280 sensor! No Humidity available.")); break; default: sensorType = 0; - Serial.println(F("Found UNKNOWN sensor! Error!")); + DEBUG_PRINTLN(F("Found UNKNOWN sensor! Error!")); } } initDone=true; @@ -225,7 +225,7 @@ public: UpdateBME280Data(sensorType); - float temperature = roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); + float temperature = roundf(sensorTemperature * powf(10, TemperatureDecimals)) / powf(10, TemperatureDecimals); float humidity, heatIndex, dewPoint; if (WLED_MQTT_CONNECTED && !mqttInitialized) @@ -246,25 +246,25 @@ public: if (sensorType == 1) // Only if sensor is a BME280 { - humidity = roundf(sensorHumidity * pow(10, HumidityDecimals)) / pow(10, HumidityDecimals); - heatIndex = roundf(sensorHeatIndex * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); - dewPoint = roundf(sensorDewPoint * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); + humidity = roundf(sensorHumidity * powf(10, HumidityDecimals)) / powf(10, HumidityDecimals); + heatIndex = roundf(sensorHeatIndex * powf(10, TemperatureDecimals)) / powf(10, TemperatureDecimals); + dewPoint = roundf(sensorDewPoint * powf(10, TemperatureDecimals)) / powf(10, TemperatureDecimals); if (humidity != lastHumidity || PublishAlways) { - String topic = String(mqttDeviceTopic) + "/humidity"; + String topic = String(mqttDeviceTopic) + F("/humidity"); mqtt->publish(topic.c_str(), 0, false, String(humidity, HumidityDecimals).c_str()); } if (heatIndex != lastHeatIndex || PublishAlways) { - String topic = String(mqttDeviceTopic) + "/heat_index"; + String topic = String(mqttDeviceTopic) + F("/heat_index"); mqtt->publish(topic.c_str(), 0, false, String(heatIndex, TemperatureDecimals).c_str()); } if (dewPoint != lastDewPoint || PublishAlways) { - String topic = String(mqttDeviceTopic) + "/dew_point"; + String topic = String(mqttDeviceTopic) + F("/dew_point"); mqtt->publish(topic.c_str(), 0, false, String(dewPoint, TemperatureDecimals).c_str()); } @@ -278,11 +278,11 @@ public: { lastPressureMeasure = timer; - float pressure = roundf(sensorPressure * pow(10, PressureDecimals)) / pow(10, PressureDecimals); + float pressure = roundf(sensorPressure * powf(10, PressureDecimals)) / powf(10, PressureDecimals); if (pressure != lastPressure || PublishAlways) { - String topic = String(mqttDeviceTopic) + "/pressure"; + String topic = String(mqttDeviceTopic) + F("/pressure"); mqttPressurePub = mqtt->publish(topic.c_str(), 0, true, String(pressure, PressureDecimals).c_str()); } @@ -296,50 +296,50 @@ public: */ inline float getTemperatureC() { if (UseCelsius) { - return (float)roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); + return (float)roundf(sensorTemperature * powf(10, TemperatureDecimals)) / powf(10, TemperatureDecimals); } else { - return (float)roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals) * 1.8f + 32; + return (float)roundf(sensorTemperature * powf(10, TemperatureDecimals)) / powf(10, TemperatureDecimals) * 1.8f + 32; } } inline float getTemperatureF() { if (UseCelsius) { - return ((float)roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals) -32) * 0.56f; + return ((float)roundf(sensorTemperature * powf(10, TemperatureDecimals)) / powf(10, TemperatureDecimals) -32) * 0.56f; } else { - return (float)roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); + return (float)roundf(sensorTemperature * powf(10, TemperatureDecimals)) / powf(10, TemperatureDecimals); } } inline float getHumidity() { - return (float)roundf(sensorHumidity * pow(10, HumidityDecimals)); + return (float)roundf(sensorHumidity * powf(10, HumidityDecimals)); } inline float getPressure() { - return (float)roundf(sensorPressure * pow(10, PressureDecimals)); + return (float)roundf(sensorPressure * powf(10, PressureDecimals)); } inline float getDewPointC() { if (UseCelsius) { - return (float)roundf(sensorDewPoint * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); + return (float)roundf(sensorDewPoint * powf(10, TemperatureDecimals)) / powf(10, TemperatureDecimals); } else { - return (float)roundf(sensorDewPoint * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals) * 1.8f + 32; + return (float)roundf(sensorDewPoint * powf(10, TemperatureDecimals)) / powf(10, TemperatureDecimals) * 1.8f + 32; } } inline float getDewPointF() { if (UseCelsius) { - return ((float)roundf(sensorDewPoint * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals) -32) * 0.56f; + return ((float)roundf(sensorDewPoint * powf(10, TemperatureDecimals)) / powf(10, TemperatureDecimals) -32) * 0.56f; } else { - return (float)roundf(sensorDewPoint * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); + return (float)roundf(sensorDewPoint * powf(10, TemperatureDecimals)) / powf(10, TemperatureDecimals); } } inline float getHeatIndexC() { if (UseCelsius) { - return (float)roundf(sensorHeatIndex * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); + return (float)roundf(sensorHeatIndex * powf(10, TemperatureDecimals)) / powf(10, TemperatureDecimals); } else { - return (float)roundf(sensorHeatIndex * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals) * 1.8f + 32; + return (float)roundf(sensorHeatIndex * powf(10, TemperatureDecimals)) / powf(10, TemperatureDecimals) * 1.8f + 32; } }inline float getHeatIndexF() { if (UseCelsius) { - return ((float)roundf(sensorHeatIndex * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals) -32) * 0.56f; + return ((float)roundf(sensorHeatIndex * powf(10, TemperatureDecimals)) / powf(10, TemperatureDecimals) -32) * 0.56f; } else { - return (float)roundf(sensorHeatIndex * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals); + return (float)roundf(sensorHeatIndex * powf(10, TemperatureDecimals)) / powf(10, TemperatureDecimals); } } @@ -358,29 +358,29 @@ public: else if (sensorType==2) //BMP280 { - JsonArray temperature_json = user.createNestedArray("Temperature"); - JsonArray pressure_json = user.createNestedArray("Pressure"); - temperature_json.add(roundf(sensorTemperature * pow(10, TemperatureDecimals))); + JsonArray temperature_json = user.createNestedArray(F("Temperature")); + JsonArray pressure_json = user.createNestedArray(F("Pressure")); + temperature_json.add(roundf(sensorTemperature * powf(10, TemperatureDecimals))); temperature_json.add(tempScale); - pressure_json.add(roundf(sensorPressure * pow(10, PressureDecimals))); + pressure_json.add(roundf(sensorPressure * powf(10, PressureDecimals))); pressure_json.add(F("hPa")); } else if (sensorType==1) //BME280 { - JsonArray temperature_json = user.createNestedArray("Temperature"); - JsonArray humidity_json = user.createNestedArray("Humidity"); - JsonArray pressure_json = user.createNestedArray("Pressure"); - JsonArray heatindex_json = user.createNestedArray("Heat Index"); - JsonArray dewpoint_json = user.createNestedArray("Dew Point"); - temperature_json.add(roundf(sensorTemperature * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals)); + JsonArray temperature_json = user.createNestedArray(F("Temperature")); + JsonArray humidity_json = user.createNestedArray(F("Humidity")); + JsonArray pressure_json = user.createNestedArray(F("Pressure")); + JsonArray heatindex_json = user.createNestedArray(F("Heat Index")); + JsonArray dewpoint_json = user.createNestedArray(F("Dew Point")); + temperature_json.add(roundf(sensorTemperature * powf(10, TemperatureDecimals)) / powf(10, TemperatureDecimals)); temperature_json.add(tempScale); - humidity_json.add(roundf(sensorHumidity * pow(10, HumidityDecimals))); + humidity_json.add(roundf(sensorHumidity * powf(10, HumidityDecimals))); humidity_json.add(F("%")); - pressure_json.add(roundf(sensorPressure * pow(10, PressureDecimals))); + pressure_json.add(roundf(sensorPressure * powf(10, PressureDecimals))); pressure_json.add(F("hPa")); - heatindex_json.add(roundf(sensorHeatIndex * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals)); + heatindex_json.add(roundf(sensorHeatIndex * powf(10, TemperatureDecimals)) / powf(10, TemperatureDecimals)); heatindex_json.add(tempScale); - dewpoint_json.add(roundf(sensorDewPoint * pow(10, TemperatureDecimals)) / pow(10, TemperatureDecimals)); + dewpoint_json.add(roundf(sensorDewPoint * powf(10, TemperatureDecimals)) / powf(10, TemperatureDecimals)); dewpoint_json.add(tempScale); } return;