PWM fan JSON API control.

This commit is contained in:
Blaz Kristan 2022-05-18 19:46:31 +02:00
parent 9f71a6ab18
commit c6578870f0
2 changed files with 28 additions and 4 deletions

View File

@ -30,7 +30,16 @@ _NOTE:_ You may also need to tweak Dallas Temperature usermod sampling frequency
No special requirements. No special requirements.
## Control PWM fan speed using JSON API
You can use e.g. `{"PWM-fan":{"speed":30,"lock":true}}` to set fan speed to 30 percent of maximum speed (replace 30 with arbitrary value between 0 and 100) and lock the speed.
If you include `speed` property you can set fan speed in percent (%) of maximum speed.
If you include `lock` property you can lock (_true_) or unlock (_false_) fan speed.
If the fan speed is unlocked it will revert to temperature controlled speed on next update cycle. Once fan speed is locked it will remain so until it is unlocked by next API call.
## Change Log ## Change Log
2021-10 2021-10
* First public release * First public release
2022-05
* Added JSON API call to allow changing of speed

View File

@ -38,6 +38,7 @@ class PWMFanUsermod : public Usermod {
#ifdef ARDUINO_ARCH_ESP32 #ifdef ARDUINO_ARCH_ESP32
uint8_t pwmChannel = 255; uint8_t pwmChannel = 255;
#endif #endif
bool lockFan = false;
#ifdef USERMOD_DALLASTEMPERATURE #ifdef USERMOD_DALLASTEMPERATURE
UsermodTemperature* tempUM; UsermodTemperature* tempUM;
@ -60,6 +61,8 @@ class PWMFanUsermod : public Usermod {
static const char _tachoUpdateSec[]; static const char _tachoUpdateSec[];
static const char _minPWMValuePct[]; static const char _minPWMValuePct[];
static const char _IRQperRotation[]; static const char _IRQperRotation[];
static const char _speed[];
static const char _lock[];
void initTacho(void) { void initTacho(void) {
if (tachoPin < 0 || !pinManager.allocatePin(tachoPin, false, PinOwner::UM_Unspecified)){ if (tachoPin < 0 || !pinManager.allocatePin(tachoPin, false, PinOwner::UM_Unspecified)){
@ -205,7 +208,7 @@ class PWMFanUsermod : public Usermod {
if ((now - msLastTachoMeasurement) < (tachoUpdateSec * 1000)) return; if ((now - msLastTachoMeasurement) < (tachoUpdateSec * 1000)) return;
updateTacho(); updateTacho();
setFanPWMbasedOnTemperature(); if (!lockFan) setFanPWMbasedOnTemperature();
} }
/* /*
@ -233,9 +236,19 @@ class PWMFanUsermod : public Usermod {
* readFromJsonState() can be used to receive data clients send to the /json/state part of the JSON API (state object). * readFromJsonState() can be used to receive data clients send to the /json/state part of the JSON API (state object).
* Values in the state object may be modified by connected clients * Values in the state object may be modified by connected clients
*/ */
//void readFromJsonState(JsonObject& root) { void readFromJsonState(JsonObject& root) {
// if (!initDone) return; // prevent crash on boot applyPreset() if (!initDone || !enabled) return; // prevent crash on boot applyPreset()
//} JsonObject usermod = root[FPSTR(_name)];
if (!usermod.isNull()) {
if (!usermod[FPSTR(_speed)].isNull() && usermod[FPSTR(_speed)].is<int>()) {
int pwmValuePct = usermod[FPSTR(_speed)].as<int>();
updateFanSpeed((MAX(0,MIN(100,pwmValuePct)) * 255) / 100);
}
if (!usermod[FPSTR(_lock)].isNull() && usermod[FPSTR(_lock)].is<bool>()) {
lockFan = usermod[FPSTR(_lock)].as<bool>();
}
}
}
/* /*
* addToConfig() can be used to add custom persistent settings to the cfg.json file in the "um" (usermod) object. * addToConfig() can be used to add custom persistent settings to the cfg.json file in the "um" (usermod) object.
@ -337,3 +350,5 @@ const char PWMFanUsermod::_temperature[] PROGMEM = "target-temp-C";
const char PWMFanUsermod::_tachoUpdateSec[] PROGMEM = "tacho-update-s"; const char PWMFanUsermod::_tachoUpdateSec[] PROGMEM = "tacho-update-s";
const char PWMFanUsermod::_minPWMValuePct[] PROGMEM = "min-PWM-percent"; const char PWMFanUsermod::_minPWMValuePct[] PROGMEM = "min-PWM-percent";
const char PWMFanUsermod::_IRQperRotation[] PROGMEM = "IRQs-per-rotation"; const char PWMFanUsermod::_IRQperRotation[] PROGMEM = "IRQs-per-rotation";
const char PWMFanUsermod::_speed[] PROGMEM = "speed";
const char PWMFanUsermod::_lock[] PROGMEM = "lock";