Skip to content
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

Usermod Updated: Internal Temperature V2 #4033

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 40 additions & 9 deletions usermods/Internal_Temperature_v2/readme.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,48 @@
# 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
<p align="left">
adamsthws marked this conversation as resolved.
Show resolved Hide resolved
<img width="700" src="assets/screenshot-info.png">
</p>

ESP32S2 seems to crash on reading the sensor -> disabled
<p align="left">
<img width="700" src="assets/screenshot-settings.png">
</p>

## Features
- &nbsp;🌡️&nbsp; Adds the internal temperature readout of the chip to the `Info` tab
- 🥵 High temperature indicator/action. (Configurable threshold and preset)
- 📣 Publishes the internal temperature over the MQTT topic: `mcutemp`
<br><br>

## Use Examples
- Warn of excessive/damaging temperatures by the triggering of a 'warning' preset
- Activate a cooling fan (when used with the multi-relay usermod)
<br><br>

## Compatibility
- A shown temp of 53,33°C might indicate that the internal temp is not supported
- ESP8266 does not have a internal temp sensor -> Disabled (Indicated with a readout of '-1')
- ESP32S2 seems to crash on reading the sensor -> Disabled (Indicated with a readout of '-1')
<br><br>

## Installation
Add a build flag `-D USERMOD_INTERNAL_TEMPERATURE` to your `platformio.ini` (or `platformio_override.ini`).
- Add a build flag `-D USERMOD_INTERNAL_TEMPERATURE` to your `platformio.ini` (or `platformio_override.ini`).
<br><br>

## Authors
Soeren Willrodt [@lost-hope](https://github.com/lost-hope)
## 📝 Change Log

2024-06-26

Dimitry Zhemkov [@dima-zhemkov](https://github.com/dima-zhemkov)
- Added "high-temperature-indication" feature
- Documentation updated

2023-09-01

* "Internal Temperature" usermod created
<br><br>

## Authors
- Soeren Willrodt [@lost-hope](https://github.com/lost-hope)
- Dimitry Zhemkov [@dima-zhemkov](https://github.com/dima-zhemkov)
- Adam Matthews [@adamsthws](https://github.com/adamsthws)
57 changes: 56 additions & 1 deletion usermods/Internal_Temperature_v2/usermod_internal_temperature.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,34 @@ class InternalTemperatureUsermod : public Usermod
{

private:
static const unsigned long minLoopInterval = 1000; // minimum allowable interval (ms)
adamsthws marked this conversation as resolved.
Show resolved Hide resolved
unsigned long loopInterval = 10000;
unsigned long lastTime = 0;
bool isEnabled = false;
float temperature = 0;
int presetToActivate = -1; // Preset to activate when temp goes above threshold (-1 = disabled)
float activationThreshold = 95.0; // Temperature threshold to trigger high-temperature actions
adamsthws marked this conversation as resolved.
Show resolved Hide resolved
float resetMargin = 2.0; // Margin below the activation threshold (Prevents frequent toggling when close to threshold)
bool isAboveThreshold = false; // Flag to track if the high temperature preset is currently active

static const char _name[];
static const char _enabled[];
static const char _loopInterval[];
static const char _activationThreshold[];
static const char _presetToActivate[];

// any private methods should go here (non-inline method should be defined out of class)
void publishMqtt(const char *state, bool retain = false); // example for publishing MQTT message

// Makes sure the measurement interval can't be set too low
void setSafeLoopInterval(unsigned long newInterval) {
adamsthws marked this conversation as resolved.
Show resolved Hide resolved
loopInterval = max(newInterval, minLoopInterval);
}

public:
void setup()
{
setSafeLoopInterval(loopInterval); // Initialize with a safe loop interval
adamsthws marked this conversation as resolved.
Show resolved Hide resolved
}

void loop()
Expand All @@ -32,6 +45,7 @@ class InternalTemperatureUsermod : public Usermod

lastTime = millis();

// Measure the temperature
#ifdef ESP8266 // ESP8266
// does not seem possible
temperature = -1;
Expand All @@ -41,6 +55,30 @@ class InternalTemperatureUsermod : public Usermod
temperature = roundf(temperatureRead() * 10) / 10;
#endif

// Check if temperature has gone above the threshold
if (temperature >= activationThreshold) {
// Update the state flag if not already set
if (!isAboveThreshold){
isAboveThreshold = true;
}
// Activate the 'over-threshold' preset if it's not already active
if (presetToActivate != -1 && currentPreset != presetToActivate) {
saveTemporaryPreset(); // Save the current preset to allow re-activation later
adamsthws marked this conversation as resolved.
Show resolved Hide resolved
applyPreset(presetToActivate);
}
}
// Check if temperature is back below the threshold
else if (temperature <= (activationThreshold - resetMargin)) {
// Update the state flag if not already set
if (isAboveThreshold){
isAboveThreshold = false;
}
// Revert back to the original preset
if (currentPreset == presetToActivate){
applyTemporaryPreset(); // Restore the previously stored active preset
adamsthws marked this conversation as resolved.
Show resolved Hide resolved
}
}

#ifndef WLED_DISABLE_MQTT
if (WLED_MQTT_CONNECTED)
{
Expand Down Expand Up @@ -80,15 +118,30 @@ class InternalTemperatureUsermod : public Usermod
JsonObject top = root.createNestedObject(FPSTR(_name));
top[FPSTR(_enabled)] = isEnabled;
top[FPSTR(_loopInterval)] = loopInterval;
top[FPSTR(_activationThreshold)] = activationThreshold;
top[FPSTR(_presetToActivate)] = presetToActivate;
}

// Append useful info to the usermod settings gui
void appendConfigData()
{
// Display 'ms' next to the 'Loop Interval' setting
oappend(SET_F("addInfo('Internal Temperature:Loop Interval', 1, 'ms');"));
// Display '°C' next to the 'Activation Threshold' setting
oappend(SET_F("addInfo('Internal Temperature:Activation Threshold', 1, '°C');"));
// Display '-1 = Disabled' next to the 'Preset To Activate' setting
oappend(SET_F("addInfo('Internal Temperature:Preset To Activate', 1, '-1 = disabled');"));
adamsthws marked this conversation as resolved.
Show resolved Hide resolved
}

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);

setSafeLoopInterval(loopInterval); // Makes sure the loop interval isn't too small.
configComplete &= getJsonValue(top[FPSTR(_presetToActivate)], presetToActivate);
configComplete &= getJsonValue(top[FPSTR(_activationThreshold)], activationThreshold);
return configComplete;
}

Expand All @@ -101,6 +154,8 @@ class InternalTemperatureUsermod : public Usermod
const char InternalTemperatureUsermod::_name[] PROGMEM = "Internal Temperature";
const char InternalTemperatureUsermod::_enabled[] PROGMEM = "Enabled";
const char InternalTemperatureUsermod::_loopInterval[] PROGMEM = "Loop Interval";
const char InternalTemperatureUsermod::_activationThreshold[] PROGMEM = "Activation Threshold";
const char InternalTemperatureUsermod::_presetToActivate[] PROGMEM = "Preset To Activate";

void InternalTemperatureUsermod::publishMqtt(const char *state, bool retain)
{
Expand Down