Skip to content

Commit

Permalink
Fix up PWM_fan
Browse files Browse the repository at this point in the history
Use a custom setup script to check for the dependencies and pass along
the required compile flags to the module; also split the object
definitions for the target modules from their source so as to allow
#including them.
  • Loading branch information
willmmiles committed Jan 31, 2025
1 parent b3f9983 commit 7a40ef7
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 195 deletions.
10 changes: 8 additions & 2 deletions usermods/PWM_fan/PWM_fan.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#if !defined(USERMOD_DALLASTEMPERATURE) && !defined(USERMOD_SHT)
#include "wled.h"

#if defined(USERMOD_DALLASTEMPERATURE)
#include "UsermodTemperature.h"
#elif defined(USERMOD_SHT)
#include "ShtUsermod.h"
#else
#error The "PWM fan" usermod requires "Dallas Temeprature" or "SHT" usermod to function properly.
#endif

#include "wled.h"


// PWM & tacho code curtesy of @KlausMu
// https://github.com/KlausMu/esp32-fan-controller/tree/main/src
Expand Down
6 changes: 6 additions & 0 deletions usermods/PWM_fan/library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name:": "PWM_fan",
"build": {
"extraScript": "setup_deps.py"
}
}
3 changes: 0 additions & 3 deletions usermods/PWM_fan/library.json.disabled

This file was deleted.

4 changes: 2 additions & 2 deletions usermods/PWM_fan/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ If the _tachometer_ is supported, the current speed (in RPM) will be displayed o

## Installation

Add the compile-time option `-D USERMOD_PWM_FAN` to your `platformio.ini` (or `platformio_override.ini`) or use `#define USERMOD_PWM_FAN` in `myconfig.h`.
You will also need `-D USERMOD_DALLASTEMPERATURE`.
Add the `PWM_fan` to `custom_usermods` in your `platformio.ini` (or `platformio_override.ini`)
You will also need `Temperature` or `sht`.

### Define Your Options

Expand Down
12 changes: 12 additions & 0 deletions usermods/PWM_fan/setup_deps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Import('env')


usermods = env.GetProjectOption("custom_usermods","").split(" ")
# Check for dependencies
if "Temperature" in usermods:
env.Append(CPPDEFINES=[("USERMOD_DALLASTEMPERATURE")])
elif "sht" in usermods:
env.Append(CPPDEFINES=[("USERMOD_SHT")])
else:
raise RuntimeError("PWM_fan usermod requires Temperature or sht to be enabled")

107 changes: 1 addition & 106 deletions usermods/Temperature/Temperature.cpp
Original file line number Diff line number Diff line change
@@ -1,112 +1,7 @@
#include "wled.h"
#include "OneWire.h"

//Pin defaults for QuinLed Dig-Uno if not overriden
#ifndef TEMPERATURE_PIN
#ifdef ARDUINO_ARCH_ESP32
#define TEMPERATURE_PIN 18
#else //ESP8266 boards
#define TEMPERATURE_PIN 14
#endif
#endif

// the frequency to check temperature, 1 minute
#ifndef USERMOD_DALLASTEMPERATURE_MEASUREMENT_INTERVAL
#define USERMOD_DALLASTEMPERATURE_MEASUREMENT_INTERVAL 60000
#endif
#include "UsermodTemperature.h"

static uint16_t mode_temperature();

class UsermodTemperature : public Usermod {

private:

bool initDone = false;
OneWire *oneWire;
// GPIO pin used for sensor (with a default compile-time fallback)
int8_t temperaturePin = TEMPERATURE_PIN;
// measurement unit (true==°C, false==°F)
bool degC = true;
// using parasite power on the sensor
bool parasite = false;
int8_t parasitePin = -1;
// how often do we read from sensor?
unsigned long readingInterval = USERMOD_DALLASTEMPERATURE_MEASUREMENT_INTERVAL;
// set last reading as "40 sec before boot", so first reading is taken after 20 sec
unsigned long lastMeasurement = UINT32_MAX - USERMOD_DALLASTEMPERATURE_MEASUREMENT_INTERVAL;
// last time requestTemperatures was called
// used to determine when we can read the sensors temperature
// we have to wait at least 93.75 ms after requestTemperatures() is called
unsigned long lastTemperaturesRequest;
float temperature;
// indicates requestTemperatures has been called but the sensor measurement is not complete
bool waitingForConversion = false;
// flag set at startup if DS18B20 sensor not found, avoids trying to keep getting
// temperature if flashed to a board without a sensor attached
byte sensorFound;

bool enabled = true;

bool HApublished = false;
int16_t idx = -1; // Domoticz virtual sensor idx

// strings to reduce flash memory usage (used more than twice)
static const char _name[];
static const char _enabled[];
static const char _readInterval[];
static const char _parasite[];
static const char _parasitePin[];
static const char _domoticzIDX[];
static const char _sensor[];
static const char _temperature[];
static const char _Temperature[];
static const char _data_fx[];

//Dallas sensor quick (& dirty) reading. Credit to - Author: Peter Scargill, August 17th, 2013
float readDallas();
void requestTemperatures();
void readTemperature();
bool findSensor();
#ifndef WLED_DISABLE_MQTT
void publishHomeAssistantAutodiscovery();
#endif

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

public:

UsermodTemperature() { _instance = this; }
static UsermodTemperature *getInstance() { return UsermodTemperature::_instance; }

/*
* API calls te enable data exchange between WLED modules
*/
inline float getTemperatureC() { return temperature; }
inline float getTemperatureF() { return temperature * 1.8f + 32.0f; }
float getTemperature();
const char *getTemperatureUnit();
uint16_t getId() override { return USERMOD_ID_TEMPERATURE; }

void setup() override;
void loop() override;
//void connected() override;
#ifndef WLED_DISABLE_MQTT
void onMqttConnect(bool sessionPresent) override;
#endif
//void onUpdateBegin(bool init) override;

//bool handleButton(uint8_t b) override;
//void handleOverlayDraw() override;

void addToJsonInfo(JsonObject& root) override;
//void addToJsonState(JsonObject &root) override;
//void readFromJsonState(JsonObject &root) override;
void addToConfig(JsonObject &root) override;
bool readFromConfig(JsonObject &root) override;

void appendConfigData() override;
};

//Dallas sensor quick (& dirty) reading. Credit to - Author: Peter Scargill, August 17th, 2013
float UsermodTemperature::readDallas() {
byte data[9];
Expand Down
108 changes: 108 additions & 0 deletions usermods/Temperature/UsermodTemperature.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#pragma once
#include "wled.h"
#include "OneWire.h"

//Pin defaults for QuinLed Dig-Uno if not overriden
#ifndef TEMPERATURE_PIN
#ifdef ARDUINO_ARCH_ESP32
#define TEMPERATURE_PIN 18
#else //ESP8266 boards
#define TEMPERATURE_PIN 14
#endif
#endif

// the frequency to check temperature, 1 minute
#ifndef USERMOD_DALLASTEMPERATURE_MEASUREMENT_INTERVAL
#define USERMOD_DALLASTEMPERATURE_MEASUREMENT_INTERVAL 60000
#endif

class UsermodTemperature : public Usermod {

private:

bool initDone = false;
OneWire *oneWire;
// GPIO pin used for sensor (with a default compile-time fallback)
int8_t temperaturePin = TEMPERATURE_PIN;
// measurement unit (true==°C, false==°F)
bool degC = true;
// using parasite power on the sensor
bool parasite = false;
int8_t parasitePin = -1;
// how often do we read from sensor?
unsigned long readingInterval = USERMOD_DALLASTEMPERATURE_MEASUREMENT_INTERVAL;
// set last reading as "40 sec before boot", so first reading is taken after 20 sec
unsigned long lastMeasurement = UINT32_MAX - USERMOD_DALLASTEMPERATURE_MEASUREMENT_INTERVAL;
// last time requestTemperatures was called
// used to determine when we can read the sensors temperature
// we have to wait at least 93.75 ms after requestTemperatures() is called
unsigned long lastTemperaturesRequest;
float temperature;
// indicates requestTemperatures has been called but the sensor measurement is not complete
bool waitingForConversion = false;
// flag set at startup if DS18B20 sensor not found, avoids trying to keep getting
// temperature if flashed to a board without a sensor attached
byte sensorFound;

bool enabled = true;

bool HApublished = false;
int16_t idx = -1; // Domoticz virtual sensor idx

// strings to reduce flash memory usage (used more than twice)
static const char _name[];
static const char _enabled[];
static const char _readInterval[];
static const char _parasite[];
static const char _parasitePin[];
static const char _domoticzIDX[];
static const char _sensor[];
static const char _temperature[];
static const char _Temperature[];
static const char _data_fx[];

//Dallas sensor quick (& dirty) reading. Credit to - Author: Peter Scargill, August 17th, 2013
float readDallas();
void requestTemperatures();
void readTemperature();
bool findSensor();
#ifndef WLED_DISABLE_MQTT
void publishHomeAssistantAutodiscovery();
#endif

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

public:

UsermodTemperature() { _instance = this; }
static UsermodTemperature *getInstance() { return UsermodTemperature::_instance; }

/*
* API calls te enable data exchange between WLED modules
*/
inline float getTemperatureC() { return temperature; }
inline float getTemperatureF() { return temperature * 1.8f + 32.0f; }
float getTemperature();
const char *getTemperatureUnit();
uint16_t getId() override { return USERMOD_ID_TEMPERATURE; }

void setup() override;
void loop() override;
//void connected() override;
#ifndef WLED_DISABLE_MQTT
void onMqttConnect(bool sessionPresent) override;
#endif
//void onUpdateBegin(bool init) override;

//bool handleButton(uint8_t b) override;
//void handleOverlayDraw() override;

void addToJsonInfo(JsonObject& root) override;
//void addToJsonState(JsonObject &root) override;
//void readFromJsonState(JsonObject &root) override;
void addToConfig(JsonObject &root) override;
bool readFromConfig(JsonObject &root) override;

void appendConfigData() override;
};

1 change: 0 additions & 1 deletion usermods/Temperature/platformio_override.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
; Options
; -------
; USERMOD_DALLASTEMPERATURE - define this to have this user mod included wled00\usermods_list.cpp
; USERMOD_DALLASTEMPERATURE_MEASUREMENT_INTERVAL - the number of milliseconds between measurements, defaults to 60 seconds
;

71 changes: 71 additions & 0 deletions usermods/sht/ShtUsermod.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once
#include "wled.h"

#ifdef WLED_DISABLE_MQTT
#error "This user mod requires MQTT to be enabled."
#endif

#define USERMOD_SHT_TYPE_SHT30 0
#define USERMOD_SHT_TYPE_SHT31 1
#define USERMOD_SHT_TYPE_SHT35 2
#define USERMOD_SHT_TYPE_SHT85 3

class SHT;

class ShtUsermod : public Usermod
{
private:
bool enabled = false; // Is usermod enabled or not
bool firstRunDone = false; // Remembers if the first config load run had been done
bool initDone = false; // Remembers if the mod has been completely initialised
bool haMqttDiscovery = false; // Is MQTT discovery enabled or not
bool haMqttDiscoveryDone = false; // Remembers if we already published the HA discovery topics

// SHT vars
SHT *shtTempHumidSensor = nullptr; // Instance of SHT lib
byte shtType = 0; // SHT sensor type to be used. Default: SHT30
byte unitOfTemp = 0; // Temperature unit to be used. Default: Celsius (0 = Celsius, 1 = Fahrenheit)
bool shtInitDone = false; // Remembers if SHT sensor has been initialised
bool shtReadDataSuccess = false; // Did we have a successful data read and is a valid temperature and humidity available?
const byte shtI2cAddress = 0x44; // i2c address of the sensor. 0x44 is the default for all SHT sensors. Change this, if needed
unsigned long shtLastTimeUpdated = 0; // Remembers when we read data the last time
bool shtDataRequested = false; // Reading data is done async. This remembers if we asked the sensor to read data
float shtCurrentTempC = 0.0f; // Last read temperature in Celsius
float shtCurrentHumidity = 0.0f; // Last read humidity in RH%


void initShtTempHumiditySensor();
void cleanupShtTempHumiditySensor();
void cleanup();
inline bool isShtReady() { return shtInitDone; } // Checks if the SHT sensor has been initialised.

void publishTemperatureAndHumidityViaMqtt();
void publishHomeAssistantAutodiscovery();
void appendDeviceToMqttDiscoveryMessage(JsonDocument& root);

public:
// Strings to reduce flash memory usage (used more than twice)
static const char _name[];
static const char _enabled[];
static const char _shtType[];
static const char _unitOfTemp[];
static const char _haMqttDiscovery[];

void setup();
void loop();
void onMqttConnect(bool sessionPresent);
void appendConfigData();
void addToConfig(JsonObject &root);
bool readFromConfig(JsonObject &root);
void addToJsonInfo(JsonObject& root);

bool isEnabled() { return enabled; }

float getTemperature();
float getTemperatureC() { return roundf(shtCurrentTempC * 10.0f) / 10.0f; }
float getTemperatureF() { return (getTemperatureC() * 1.8f) + 32.0f; }
float getHumidity() { return roundf(shtCurrentHumidity * 10.0f) / 10.0f; }
const char* getUnitString();

uint16_t getId() { return USERMOD_ID_SHT; }
};
6 changes: 6 additions & 0 deletions usermods/sht/library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name:": "sht",
"dependencies": {
"robtillaart/SHT85": "~0.3.3"
}
}
3 changes: 0 additions & 3 deletions usermods/sht/library.json.disabled

This file was deleted.

13 changes: 4 additions & 9 deletions usermods/sht/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,21 @@ Usermod to support various SHT i2c sensors like the SHT30, SHT31, SHT35 and SHT8
* "SHT85" by Rob Tillaart, v0.2 or higher: https://github.com/RobTillaart/SHT85

## Usermod installation
Simply copy the below block (build task) to your `platformio_override.ini` and compile WLED using this new build task. Or use an existing one, add the buildflag `-D USERMOD_SHT` and the below library dependencies.

Simply copy the below block (build task) to your `platformio_override.ini` and compile WLED using this new build task. Or use an existing one, add the custom_usermod `sht`.

ESP32:
```
[env:custom_esp32dev_usermod_sht]
extends = env:esp32dev
build_flags = ${common.build_flags_esp32}
-D USERMOD_SHT
lib_deps = ${esp32.lib_deps}
robtillaart/SHT85@~0.3.3
custom_usermods = ${env:esp32dev.custom_usermods} sht
```

ESP8266:
```
[env:custom_d1_mini_usermod_sht]
extends = env:d1_mini
build_flags = ${common.build_flags_esp8266}
-D USERMOD_SHT
lib_deps = ${esp8266.lib_deps}
robtillaart/SHT85@~0.3.3
custom_usermods = ${env:d1_mini.custom_usermods} sht
```

## MQTT Discovery for Home Assistant
Expand Down
Loading

0 comments on commit 7a40ef7

Please sign in to comment.