22
33#include " wled.h"
44
5+ // constants
6+ #define MCUT_READ_TIME_MS 7500 // read once in 7.5 seconds
7+
58// class name. Use something descriptive and leave the ": public Usermod" part :)
69class mcuTemp : public Usermod
710{
@@ -25,27 +28,34 @@ class mcuTemp : public Usermod
2528
2629 void loop ()
2730 {
31+ static unsigned long lastMQQTTime = 0 ;
2832 // if usermod is disabled or called during strip updating just exit
2933 // NOTE: on very long strips strip.isUpdating() may always return true so update accordingly
30- if (!enabled || strip.isUpdating ())
34+ if (!enabled || ( strip.isUpdating () && ( millis () - lastTime < MCUT_READ_TIME_MS) ))
3135 return ;
3236
37+ if (millis () - lastTime < MCUT_READ_TIME_MS) return ; // reading each 8 seconds should be enough
38+
3339#ifdef ESP8266 // ESP8266
3440 // does not seem possible
3541 mcutemp = -1 ;
3642#elif defined(CONFIG_IDF_TARGET_ESP32S2) // ESP32S2
3743 mcutemp = -1 ;
3844#else // ESP32 ESP32S3 and ESP32C3
39- mcutemp = roundf (temperatureRead () * 100 ) / 100 ;
45+ float newmcutemp = roundf (temperatureRead () * 10 ) / 10 ;
46+ if (abs (newmcutemp - 53 .3f ) > 0 .05f ) mcutemp = (mcutemp + 2 .0f * newmcutemp) / 3 .0f ; // skip error value (128 => 53.3deg), apply some filtering
4047#endif
4148
42- if (millis () - lastTime > 10000 )
49+ #ifndef WLED_DISABLE_MQTT
50+ if (millis () - lastMQQTTime > 15000 )
4351 {
4452 char array[10 ];
45- snprintf (array, sizeof (array), " %f " , mcutemp);
53+ snprintf (array, sizeof (array), " %3.1f " , mcutemp);
4654 publishMqtt (array);
47- lastTime = millis ();
55+ lastMQQTTime = millis ();
4856 }
57+ #endif
58+ lastTime = millis ();
4959 }
5060 /*
5161 * addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API.
@@ -61,8 +71,9 @@ class mcuTemp : public Usermod
6171
6272 // this code adds "u":{"ExampleUsermod":[20," lux"]} to the info object
6373 // int reading = 20;
74+ if (!enabled) return ;
6475 JsonArray lightArr = user.createNestedArray (FPSTR (_name)); // name
65- lightArr.add (mcutemp); // value
76+ lightArr.add (roundf ( 10 . 0f * mcutemp)/ 10 . 0f ) ; // value, rounded to 1 decimal
6677 lightArr.add (F (" °C" )); // unit
6778
6879 // if you are implementing a sensor usermod, you may publish sensor data
@@ -72,7 +83,7 @@ class mcuTemp : public Usermod
7283 // temp.add(reading);
7384 // temp.add(F("lux"));
7485 }
75-
86+ /*
7687 void addToJsonState(JsonObject &root)
7788 {
7889 }
@@ -96,7 +107,7 @@ class mcuTemp : public Usermod
96107 void handleOverlayDraw()
97108 {
98109 }
99-
110+ */
100111 /*
101112 * getId() allows you to optionally give your V2 usermod an unique ID (please define it in const.h!).
102113 * This could be used in the future for the system to determine whether your usermod is installed.
0 commit comments