-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Add Internal Temperature usermod #3246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Internal Temperature Usermod | ||
| This usermod adds the temperature readout to the Info tab and also publishes that over the topic `mcutemp` topic. | ||
|
|
||
| ## Important | ||
| A shown temp of 53,33°C might indicate that the internal temp is not supported. | ||
|
|
||
| ESP8266 does not have a internal temp sensor | ||
|
|
||
| ESP32S2 seems to crash on reading the sensor -> disabled | ||
|
|
||
| ## Installation | ||
| Add a build flag `-D USERMOD_INTERNAL_TEMPERATURE` to your `platformio.ini` (or `platformio_override.ini`). | ||
|
|
||
| ## Authors | ||
| Soeren Willrodt [@lost-hope](https://github.com/lost-hope) | ||
|
|
||
| Dimitry Zhemkov [@dima-zhemkov](https://github.com/dima-zhemkov) |
117 changes: 117 additions & 0 deletions
117
usermods/Internal_Temperature_v2/usermod_internal_temperature.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| #pragma once | ||
|
|
||
| #include "wled.h" | ||
|
|
||
| class InternalTemperatureUsermod : public Usermod | ||
| { | ||
|
|
||
| private: | ||
| unsigned long loopInterval = 10000; | ||
| unsigned long lastTime = 0; | ||
| bool isEnabled = false; | ||
| float temperature = 0; | ||
|
|
||
| static const char _name[]; | ||
| static const char _enabled[]; | ||
| static const char _loopInterval[]; | ||
|
|
||
| // any private methods should go here (non-inline methosd should be defined out of class) | ||
| void publishMqtt(const char *state, bool retain = false); // example for publishing MQTT message | ||
|
|
||
| public: | ||
| void setup() | ||
| { | ||
| } | ||
|
|
||
| void loop() | ||
| { | ||
| // 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 | ||
| if (!isEnabled || strip.isUpdating() || millis() - lastTime <= loopInterval) | ||
| return; | ||
|
|
||
| lastTime = millis(); | ||
|
|
||
| #ifdef ESP8266 // ESP8266 | ||
| // does not seem possible | ||
| temperature = -1; | ||
| #elif defined(CONFIG_IDF_TARGET_ESP32S2) // ESP32S2 | ||
| temperature = -1; | ||
| #else // ESP32 ESP32S3 and ESP32C3 | ||
| temperature = roundf(temperatureRead() * 10) / 10; | ||
| #endif | ||
|
|
||
| #ifndef WLED_DISABLE_MQTT | ||
| if (WLED_MQTT_CONNECTED) | ||
| { | ||
| char array[10]; | ||
| snprintf(array, sizeof(array), "%f", temperature); | ||
| publishMqtt(array); | ||
| } | ||
| #endif | ||
| } | ||
|
|
||
| void addToJsonInfo(JsonObject &root) | ||
| { | ||
| if (!isEnabled) | ||
| return; | ||
|
|
||
| // if "u" object does not exist yet wee need to create it | ||
| JsonObject user = root["u"]; | ||
| if (user.isNull()) | ||
| user = root.createNestedObject("u"); | ||
|
|
||
dima-zhemkov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| JsonArray userTempArr = user.createNestedArray(FPSTR(_name)); | ||
| userTempArr.add(temperature); | ||
| userTempArr.add(F(" °C")); | ||
|
|
||
| // if "sensor" object does not exist yet wee need to create it | ||
| JsonObject sensor = root[F("sensor")]; | ||
| if (sensor.isNull()) | ||
| sensor = root.createNestedObject(F("sensor")); | ||
|
|
||
| JsonArray sensorTempArr = sensor.createNestedArray(FPSTR(_name)); | ||
| sensorTempArr.add(temperature); | ||
| sensorTempArr.add(F("°C")); | ||
| } | ||
|
|
||
| void addToConfig(JsonObject &root) | ||
| { | ||
| JsonObject top = root.createNestedObject(FPSTR(_name)); | ||
| top[FPSTR(_enabled)] = isEnabled; | ||
| top[FPSTR(_loopInterval)] = loopInterval; | ||
| } | ||
|
|
||
| bool readFromConfig(JsonObject &root) | ||
| { | ||
| JsonObject top = root[FPSTR(_name)]; | ||
| bool configComplete = !top.isNull(); | ||
| configComplete &= getJsonValue(top[FPSTR(_enabled)], isEnabled); | ||
| configComplete &= getJsonValue(top[FPSTR(_loopInterval)], loopInterval); | ||
|
|
||
| return configComplete; | ||
| } | ||
|
|
||
| uint16_t getId() | ||
| { | ||
| return USERMOD_ID_INTERNAL_TEMPERATURE; | ||
| } | ||
| }; | ||
|
|
||
| const char InternalTemperatureUsermod::_name[] PROGMEM = "Internal Temperature"; | ||
| const char InternalTemperatureUsermod::_enabled[] PROGMEM = "Enabled"; | ||
| const char InternalTemperatureUsermod::_loopInterval[] PROGMEM = "Loop Interval"; | ||
|
|
||
| void InternalTemperatureUsermod::publishMqtt(const char *state, bool retain) | ||
| { | ||
| #ifndef WLED_DISABLE_MQTT | ||
| // Check if MQTT Connected, otherwise it will crash the 8266 | ||
| if (WLED_MQTT_CONNECTED) | ||
| { | ||
| char subuf[64]; | ||
| strcpy(subuf, mqttDeviceTopic); | ||
| strcat_P(subuf, PSTR("/mcutemp")); | ||
| mqtt->publish(subuf, 0, retain, state); | ||
| } | ||
| #endif | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.