Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
122 changes: 122 additions & 0 deletions usermods/mcu_temp/mcuTemp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#pragma once

#include "wled.h"

// class name. Use something descriptive and leave the ": public Usermod" part :)
class mcuTemp : public Usermod
{

private:
float mcutemp = 0;

// 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:
mcuTemp(const char *name, bool enabled) : Usermod(name, enabled) {} // WLEDMM

void setup()
{
}

void connected()
{
}

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 (!enabled || strip.isUpdating())
return;

#ifdef ESP8266 // ESP8266
// does not seem possible
mcutemp = -1;
#elif defined(CONFIG_IDF_TARGET_ESP32S2) // ESP32S2
mcutemp = -1;
#else // ESP32 ESP32S3 and ESP32C3
mcutemp = roundf(temperatureRead() * 100) / 100;
#endif

if (millis() - lastTime > 10000)
{
char array[10];
snprintf(array, sizeof(array), "%f", mcutemp);
publishMqtt(array);
lastTime = millis();
}
}
/*
* addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API.
* Creating an "u" object allows you to add custom key/value pairs to the Info section of the WLED web UI.
* Below it is shown how this could be used for e.g. a light sensor
*/
void addToJsonInfo(JsonObject &root)
{
// if "u" object does not exist yet wee need to create it
JsonObject user = root["u"];
if (user.isNull())
user = root.createNestedObject("u");

// this code adds "u":{"ExampleUsermod":[20," lux"]} to the info object
// int reading = 20;
JsonArray lightArr = user.createNestedArray(FPSTR(_name)); // name
lightArr.add(mcutemp); // value
lightArr.add(F(" °C")); // unit

// if you are implementing a sensor usermod, you may publish sensor data
// JsonObject sensor = root[F("sensor")];
// if (sensor.isNull()) sensor = root.createNestedObject(F("sensor"));
// temp = sensor.createNestedArray(F("light"));
// temp.add(reading);
// temp.add(F("lux"));
}

void addToJsonState(JsonObject &root)
{
}
void readFromJsonState(JsonObject &root)
{
}

void addToConfig(JsonObject &root)
{
}

bool readFromConfig(JsonObject &root)
{
return true;
}

void appendConfigData()
{
}

void handleOverlayDraw()
{
}

/*
* 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.
*/
uint16_t getId()
{
return USERMOD_ID_MCUTEMP;
}
};

void mcuTemp::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
}
11 changes: 11 additions & 0 deletions usermods/mcu_temp/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# MCU Temp Usermod
This usermod adds the temperature readout to the Info tab and also publishes that over the topic `mcutemp` topic.


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

Buildflag: `-D USERMOD_MCUTEMP`
Loading