Add Internal Temperature usermod (#3246)
* Add Internal Temperature usermod * Fix line endings * Update readme * Format readme
This commit is contained in:
parent
262ed38429
commit
822dd24756
17
usermods/Internal_Temperature_v2/readme.md
Normal file
17
usermods/Internal_Temperature_v2/readme.md
Normal file
@ -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
usermods/Internal_Temperature_v2/usermod_internal_temperature.h
Normal file
117
usermods/Internal_Temperature_v2/usermod_internal_temperature.h
Normal file
@ -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");
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
@ -149,6 +149,7 @@
|
|||||||
#define USERMOD_ID_SHT 39 //Usermod "usermod_sht.h
|
#define USERMOD_ID_SHT 39 //Usermod "usermod_sht.h
|
||||||
#define USERMOD_ID_KLIPPER 40 //Usermod Klipper percentage
|
#define USERMOD_ID_KLIPPER 40 //Usermod Klipper percentage
|
||||||
#define USERMOD_ID_WIREGUARD 41 //Usermod "wireguard.h"
|
#define USERMOD_ID_WIREGUARD 41 //Usermod "wireguard.h"
|
||||||
|
#define USERMOD_ID_INTERNAL_TEMPERATURE 42 //Usermod "usermod_internal_temperature.h"
|
||||||
|
|
||||||
//Access point behavior
|
//Access point behavior
|
||||||
#define AP_BEHAVIOR_BOOT_NO_CONN 0 //Open AP when no connection after boot
|
#define AP_BEHAVIOR_BOOT_NO_CONN 0 //Open AP when no connection after boot
|
||||||
|
@ -177,6 +177,10 @@
|
|||||||
#include "../usermods/boblight/boblight.h"
|
#include "../usermods/boblight/boblight.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef USERMOD_INTERNAL_TEMPERATURE
|
||||||
|
#include "../usermods/Internal_Temperature_v2/usermod_internal_temperature.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(WLED_USE_SD_MMC) || defined(WLED_USE_SD_SPI)
|
#if defined(WLED_USE_SD_MMC) || defined(WLED_USE_SD_SPI)
|
||||||
// This include of SD.h and SD_MMC.h must happen here, else they won't be
|
// This include of SD.h and SD_MMC.h must happen here, else they won't be
|
||||||
// resolved correctly (when included in mod's header only)
|
// resolved correctly (when included in mod's header only)
|
||||||
@ -365,4 +369,8 @@ void registerUsermods()
|
|||||||
#ifdef USERMOD_SHT
|
#ifdef USERMOD_SHT
|
||||||
usermods.add(new ShtUsermod());
|
usermods.add(new ShtUsermod());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef USERMOD_INTERNAL_TEMPERATURE
|
||||||
|
usermods.add(new InternalTemperatureUsermod());
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user