mcuTemp improvements
* allow users to en/disable usermod * limit rate of reading the sensor (once in 8 seconds) * add slight filtering * skip invalid readings
This commit is contained in:
@@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
#include "wled.h"
|
#include "wled.h"
|
||||||
|
|
||||||
|
// constants
|
||||||
|
#define MCUT_READ_TIME_MS 7500 // read once in 7.5 seconds
|
||||||
|
|
||||||
// class name. Use something descriptive and leave the ": public Usermod" part :)
|
// class name. Use something descriptive and leave the ": public Usermod" part :)
|
||||||
class mcuTemp : public Usermod
|
class mcuTemp : public Usermod
|
||||||
{
|
{
|
||||||
@@ -25,27 +28,34 @@ public:
|
|||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
|
static unsigned long lastMQQTTime = 0;
|
||||||
// if usermod is disabled or called during strip updating just exit
|
// if usermod is disabled or called during strip updating just exit
|
||||||
// NOTE: on very long strips strip.isUpdating() may always return true so update accordingly
|
// NOTE: on very long strips strip.isUpdating() may always return true so update accordingly
|
||||||
if (!enabled || strip.isUpdating())
|
if (!enabled || (strip.isUpdating() && (millis() - lastTime < MCUT_READ_TIME_MS)))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (millis() - lastTime < MCUT_READ_TIME_MS) return; // reading each 8 seconds should be enough
|
||||||
|
|
||||||
#ifdef ESP8266 // ESP8266
|
#ifdef ESP8266 // ESP8266
|
||||||
// does not seem possible
|
// does not seem possible
|
||||||
mcutemp = -1;
|
mcutemp = -1;
|
||||||
#elif defined(CONFIG_IDF_TARGET_ESP32S2) // ESP32S2
|
#elif defined(CONFIG_IDF_TARGET_ESP32S2) // ESP32S2
|
||||||
mcutemp = -1;
|
mcutemp = -1;
|
||||||
#else // ESP32 ESP32S3 and ESP32C3
|
#else // ESP32 ESP32S3 and ESP32C3
|
||||||
mcutemp = roundf(temperatureRead() * 100) / 100;
|
float newmcutemp = roundf(temperatureRead() * 10) / 10;
|
||||||
|
if (abs(newmcutemp - 53.3f) > 0.05f) mcutemp = (mcutemp + 2.0f * newmcutemp) / 3.0f; // skip error value (128 => 53.3deg), apply some filtering
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (millis() - lastTime > 10000)
|
#ifndef WLED_DISABLE_MQTT
|
||||||
|
if (millis() - lastMQQTTime > 15000)
|
||||||
{
|
{
|
||||||
char array[10];
|
char array[10];
|
||||||
snprintf(array, sizeof(array), "%f", mcutemp);
|
snprintf(array, sizeof(array), "%3.1f", mcutemp);
|
||||||
publishMqtt(array);
|
publishMqtt(array);
|
||||||
lastTime = millis();
|
lastMQQTTime = millis();
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
lastTime = millis();
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API.
|
* addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API.
|
||||||
@@ -61,8 +71,9 @@ public:
|
|||||||
|
|
||||||
// this code adds "u":{"ExampleUsermod":[20," lux"]} to the info object
|
// this code adds "u":{"ExampleUsermod":[20," lux"]} to the info object
|
||||||
// int reading = 20;
|
// int reading = 20;
|
||||||
|
if (!enabled) return;
|
||||||
JsonArray lightArr = user.createNestedArray(FPSTR(_name)); // name
|
JsonArray lightArr = user.createNestedArray(FPSTR(_name)); // name
|
||||||
lightArr.add(mcutemp); // value
|
lightArr.add(roundf(10.0f * mcutemp)/10.0f); // value, rounded to 1 decimal
|
||||||
lightArr.add(F(" °C")); // unit
|
lightArr.add(F(" °C")); // unit
|
||||||
|
|
||||||
// if you are implementing a sensor usermod, you may publish sensor data
|
// if you are implementing a sensor usermod, you may publish sensor data
|
||||||
@@ -72,7 +83,7 @@ public:
|
|||||||
// temp.add(reading);
|
// temp.add(reading);
|
||||||
// temp.add(F("lux"));
|
// temp.add(F("lux"));
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
void addToJsonState(JsonObject &root)
|
void addToJsonState(JsonObject &root)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -96,7 +107,7 @@ public:
|
|||||||
void handleOverlayDraw()
|
void handleOverlayDraw()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
/*
|
/*
|
||||||
* getId() allows you to optionally give your V2 usermod an unique ID (please define it in const.h!).
|
* getId() allows you to optionally give your V2 usermod an unique ID (please define it in const.h!).
|
||||||
* This could be used in the future for the system to determine whether your usermod is installed.
|
* This could be used in the future for the system to determine whether your usermod is installed.
|
||||||
|
|||||||
Reference in New Issue
Block a user