Skip to content
Open
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
18 changes: 14 additions & 4 deletions usermods/DHT/DHT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#define DHTTYPE DHT_TYPE_21
#elif USERMOD_DHT_DHTTYPE == 22
#define DHTTYPE DHT_TYPE_22
#else
#error Invalid USERMOD_DHT_DHTTYPE
#endif

// Connect pin 1 (on the left) of the sensor to +5V
Expand Down Expand Up @@ -57,11 +59,11 @@

DHT_nonblocking dht_sensor(DHTPIN, DHTTYPE);

class UsermodDHT : public Usermod {
class UsermodDHT : public Usermod, public TemperatureSensor, public HumiditySensor {
private:
unsigned long nextReadTime = 0;
unsigned long lastReadTime = 0;
float humidity, temperature = 0;
float tempC = 0, humidity = 0, temperature = 0;
bool initializing = true;
bool disabled = false;
#ifdef USERMOD_DHT_MQTT
Expand Down Expand Up @@ -89,6 +91,8 @@ class UsermodDHT : public Usermod {
#ifdef USERMOD_DHT_STATS
nextResetStatsTime = millis() + 60*60*1000;
#endif
pluginManager.registerTemperatureSensor(*this, "DHT");
pluginManager.registerHumiditySensor(*this, "DHT");
}

void loop() {
Expand All @@ -112,13 +116,14 @@ class UsermodDHT : public Usermod {
}
#endif

float tempC;
if (dht_sensor.measure(&tempC, &humidity)) {
#ifdef USERMOD_DHT_CELSIUS
temperature = tempC;
#else
temperature = tempC * 9 / 5 + 32;
#endif
_isTemperatureValid = true;
_isHumidityValid = true;

#ifdef USERMOD_DHT_MQTT
// 10^n where n is number of decimal places to display in mqtt message. Please adjust buff size together with this constant
Expand Down Expand Up @@ -168,6 +173,8 @@ class UsermodDHT : public Usermod {

if (((millis() - lastReadTime) > 10*USERMOD_DHT_MEASUREMENT_INTERVAL)) {
disabled = true;
_isTemperatureValid = false;
_isHumidityValid = false;
}
}

Expand Down Expand Up @@ -242,8 +249,11 @@ class UsermodDHT : public Usermod {
return USERMOD_ID_DHT;
}

private:
float do_getTemperatureC() override { return tempC; }
float do_getHumidity() override { return humidity; }
};


static UsermodDHT dht;
REGISTER_USERMOD(dht);
REGISTER_USERMOD(dht);
9 changes: 7 additions & 2 deletions usermods/Temperature/Temperature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ void UsermodTemperature::setup() {
}
lastMeasurement = millis() - readingInterval + 10000;
initDone = true;
pluginManager.registerTemperatureSensor(*this, _name);
}

void UsermodTemperature::loop() {
Expand All @@ -165,11 +166,15 @@ void UsermodTemperature::loop() {
if (now - lastTemperaturesRequest >= 750 /* 93.75ms per the datasheet but can be up to 750ms */) {
readTemperature();
if (getTemperatureC() < -100.0f) {
if (++errorCount > 10) sensorFound = 0;
if (++errorCount > 10) {
sensorFound = 0;
_isTemperatureValid = false;
}
lastMeasurement = now - readingInterval + 300; // force new measurement in 300ms
return;
}
errorCount = 0;
_isTemperatureValid = true;

#ifndef WLED_DISABLE_MQTT
if (WLED_MQTT_CONNECTED) {
Expand Down Expand Up @@ -380,4 +385,4 @@ static uint16_t mode_temperature() {


static UsermodTemperature temperature;
REGISTER_USERMOD(temperature);
REGISTER_USERMOD(temperature);
4 changes: 2 additions & 2 deletions usermods/Temperature/UsermodTemperature.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#define USERMOD_DALLASTEMPERATURE_MEASUREMENT_INTERVAL 60000
#endif

class UsermodTemperature : public Usermod {
class UsermodTemperature : public Usermod, public TemperatureSensor {

private:

Expand Down Expand Up @@ -71,6 +71,7 @@ class UsermodTemperature : public Usermod {
#ifndef WLED_DISABLE_MQTT
void publishHomeAssistantAutodiscovery();
#endif
float do_getTemperatureC() override { return temperature; }

static UsermodTemperature* _instance; // to overcome nonstatic getTemperatureC() method and avoid UsermodManager::lookup(USERMOD_ID_TEMPERATURE);

Expand Down Expand Up @@ -107,4 +108,3 @@ class UsermodTemperature : public Usermod {

void appendConfigData() override;
};

4 changes: 4 additions & 0 deletions usermods/UM_DummySensor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Dummy usermod to simulate random sensor readings.

Use `UM_PluginDemo` and `UM_DummySensor` together as an example for how direct interactions between
usermods can be realized through the plugin API.
104 changes: 104 additions & 0 deletions usermods/UM_DummySensor/UM_DummySensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* (c) 2026 Joachim Dick
* Licensed under the EUPL v. 1.2 or later
*/

#include "wled.h"
#include "PluginAPI/custom/DummySensor/DummySensor.h"

//--------------------------------------------------------------------------------------------------

static constexpr char _name[] = "Dummy-Sensor";

/** Dummy usermod implementation that simulates random sensor readings.
*/
class UM_DummySensor : public Usermod, public DummySensor, public TemperatureSensor, public HumiditySensor
{

// ----- usermod functions -----

void setup() override
{
// register sensor plugins
pluginManager.registerTemperatureSensor(*this, _name);
pluginManager.registerHumiditySensor(*this, _name);
// this dummy can deliver readings instantly
_isTemperatureValid = true;
_isHumidityValid = true;
}

void loop() override {}

// ----- DummySensor (our own) custom plugin functions -----

void enableTemperatureSensor() override
{
if (!_isTemperatureSensorEnabled)
pluginManager.registerTemperatureSensor(*this, _name);
_isTemperatureSensorEnabled = true;
}

void disableTemperatureSensor() override
{
if (_isTemperatureSensorEnabled)
pluginManager.unregisterTemperatureSensor(*this);
_isTemperatureSensorEnabled = false;
}

bool isTemperatureSensorEnabled() override { return _isTemperatureSensorEnabled; }

void enableHumiditySensor() override
{
if (!_isHumiditySensorEnabled)
pluginManager.registerHumiditySensor(*this, _name);
_isHumiditySensorEnabled = true;
}

void disableHumiditySensor() override
{
if (_isHumiditySensorEnabled)
pluginManager.unregisterHumiditySensor(*this);
_isHumiditySensorEnabled = false;
}

bool isHumiditySensorEnabled() override { return _isHumiditySensorEnabled; }

// ----- TemperatureSensor plugin functions -----

/// @copydoc TemperatureSensor::do_getTemperatureC()
float do_getTemperatureC() override { return readTemperature(); }

// ----- HumiditySensor plugin functions -----

/// @copydoc HumiditySensor::do_getHumidity()
float do_getHumidity() override { return readHumidity(); }

// ----- internal processing functions -----

/// The dummy implementation to simulate temperature values (based on perlin noise).
float readTemperature()
{
const int32_t raw = perlin16(strip.now * 8) - 0x8000;
// simulate some random temperature around 20°C
return 20.0f + raw / 65535.0f * 30.0f;
}

/// The dummy implementation to simulate humidity values (a sine wave).
float readHumidity()
{
const int32_t raw = beatsin16_t(1);
// simulate some random humidity between 10% and 90%
return 10.0f + raw / 65535.0f * 80.0f;
}

// ----- member variables -----

bool _isTemperatureSensorEnabled = false;
bool _isHumiditySensorEnabled = false;
};

//--------------------------------------------------------------------------------------------------

static UM_DummySensor um_DummySensor;
REGISTER_USERMOD(um_DummySensor);
DEFINE_PLUGIN_API(DummySensor, um_DummySensor);
4 changes: 4 additions & 0 deletions usermods/UM_DummySensor/library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "UM_DummySensor",
"build": { "libArchive": false }
}
4 changes: 4 additions & 0 deletions usermods/UM_PluginDemo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Examples for writing custom plugins.

Use `UM_PluginDemo` and `UM_DummySensor` together as an example for how direct interactions between
usermods can be realized through the plugin API.
Loading
Loading