diff --git a/usermods/PIR_sensor_switch/readme.md b/usermods/PIR_sensor_switch/readme.md index 17c80baae..d6ea0fb0b 100644 --- a/usermods/PIR_sensor_switch/readme.md +++ b/usermods/PIR_sensor_switch/readme.md @@ -10,27 +10,14 @@ The LED strip is switched [using a relay](https://github.com/Aircoookie/WLED/wik ## Webinterface The info page in the web interface shows the items below - -- the state of the sensor. By clicking on the state the sensor can be deactivated/activated. Changes persist after a reboot. -**I recommend to deactivate the sensor before an OTA update and activate it again afterwards**. - the remaining time of the off timer. - -## JSON API - -The usermod supports the following state changes: - -| JSON key | Value range | Description | -|------------|-------------|---------------------------------| -| PIRenabled | bool | Deactivdate/activate the sensor | -| PIRoffSec | 60 to 43200 | Off timer seconds | - - Changes also persist after a reboot. +**I recommend to deactivate the sensor before an OTA update and activate it again afterwards**. ## Sensor connection My setup uses an HC-SR501 sensor, a HC-SR505 should also work. -The usermod uses GPIO13 (D1 mini pin D7) for the sensor signal. +The usermod uses GPIO13 (D1 mini pin D7) by default for the sensor signal but can be changed in the Usermod settings page. [This example page](http://www.esp8266learning.com/wemos-mini-pir-sensor-example.php) describes how to connect the sensor. Use the potentiometers on the sensor to set the time-delay to the minimum and the sensitivity to about half, or slightly above. @@ -76,8 +63,6 @@ void registerUsermods() ## API to enable/disable the PIR sensor from outside. For example from another usermod. -The class provides the static method `PIRsensorSwitch* PIRsensorSwitch::GetInstance()` to get a pointer to the usermod object. - To query or change the PIR sensor state the methods `bool PIRsensorEnabled()` and `void EnablePIRsensor(bool enable)` are available. ### There are two options to get access to the usermod instance: @@ -98,9 +83,12 @@ class MyUsermod : public Usermod { //... void togglePIRSensor() { - if (PIRsensorSwitch::GetInstance() != nullptr) { - PIRsensorSwitch::GetInstance()->EnablePIRsensor(!PIRsensorSwitch::GetInstance()->PIRsensorEnabled()); + #ifdef USERMOD_PIR_SENSOR_SWITCH + PIRsensorSwitch *PIRsensor = (PIRsensorSwitch::*) usermods.lookup(USERMOD_ID_PIRSWITCH); + if (PIRsensor != nullptr) { + PIRsensor->EnablePIRsensor(!PIRsensor->PIRsensorEnabled()); } + #endif } //... }; diff --git a/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h b/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h index 7124552ba..19fcc2a93 100644 --- a/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h +++ b/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h @@ -39,24 +39,11 @@ public: /** * constructor */ - PIRsensorSwitch() - { - // set static instance pointer - PIRsensorSwitchInstance(this); - } + PIRsensorSwitch() {} /** * desctructor */ - ~PIRsensorSwitch() - { - PIRsensorSwitchInstance(nullptr, true); - ; - } - - /** - * return the instance pointer of the class - */ - static PIRsensorSwitch *GetInstance() { return PIRsensorSwitchInstance(); } + ~PIRsensorSwitch() {} /** * Enable/Disable the PIR sensor @@ -98,11 +85,6 @@ private: */ static void IRAM_ATTR ISR_PIRstateChange(); - /** - * Set/get instance pointer - */ - static PIRsensorSwitch *PIRsensorSwitchInstance(PIRsensorSwitch *pInstance = nullptr, bool bRemoveInstance = false); - /** * switch strip on/off */ @@ -253,17 +235,18 @@ public: */ if (m_PIRenabled) { +/* JsonArray infoArr = user.createNestedArray(F("PIR switch-off timer after")); //name String uiDomString = F("min"); infoArr.add(uiDomString); - +*/ // off timer + String uiDomString = F("PIR "); + JsonArray infoArr = user.createNestedArray(uiDomString); // timer value if (m_offTimerStart > 0) { - uiDomString = F(""); - infoArr = user.createNestedArray(uiDomString); // timer value uiDomString = ""; unsigned int offSeconds = (m_switchOffDelay - (millis() - m_offTimerStart)) / 1000; if (offSeconds >= 3600) @@ -287,6 +270,8 @@ public: } uiDomString += (offSeconds); infoArr.add(uiDomString + F("s")); + } else { + infoArr.add(F("inactive")); } } } @@ -448,17 +433,7 @@ void IRAM_ATTR PIRsensorSwitch::ISR_PIRstateChange() newPIRsensorState(true, true); } -PIRsensorSwitch *PIRsensorSwitch::PIRsensorSwitchInstance(PIRsensorSwitch *pInstance, bool bRemoveInstance) -{ - static PIRsensorSwitch *s_pPIRsensorSwitch = nullptr; - if (pInstance != nullptr || bRemoveInstance) - { - s_pPIRsensorSwitch = pInstance; - } - return s_pPIRsensorSwitch; -}; - // strings to reduce flash memory usage (used more than twice) const char PIRsensorSwitch::_name[] PROGMEM = "PIRsensorSwitch"; -const char PIRsensorSwitch::_switchOffDelay[] PROGMEM = "PIRoffSec"; const char PIRsensorSwitch::_enabled[] PROGMEM = "PIRenabled"; +const char PIRsensorSwitch::_switchOffDelay[] PROGMEM = "PIRoffSec"; diff --git a/usermods/usermod_v2_auto_save/usermod_v2_auto_save.h b/usermods/usermod_v2_auto_save/usermod_v2_auto_save.h index 63b32f7d2..432e6bb73 100644 --- a/usermods/usermod_v2_auto_save/usermod_v2_auto_save.h +++ b/usermods/usermod_v2_auto_save/usermod_v2_auto_save.h @@ -30,6 +30,7 @@ class AutoSaveUsermod : public Usermod { bool firstLoop = true; bool initDone = false; + bool enabled = true; // configurable parameters unsigned long autoSaveAfterSec = 15; // 15s by default @@ -51,6 +52,7 @@ class AutoSaveUsermod : public Usermod { // strings to reduce flash memory usage (used more than twice) static const char _name[]; + static const char _autoSaveEnabled[]; static const char _autoSaveAfterSec[]; static const char _autoSavePreset[]; static const char _autoSaveApplyOnBoot[]; @@ -95,7 +97,7 @@ class AutoSaveUsermod : public Usermod { * Da loop. */ void loop() { - if (!autoSaveAfterSec) return; // setting 0 as autosave seconds disables autosave + if (!autoSaveAfterSec && !enabled) return; // setting 0 as autosave seconds disables autosave unsigned long now = millis(); uint8_t currentMode = strip.getMode(); @@ -181,6 +183,7 @@ class AutoSaveUsermod : public Usermod { void addToConfig(JsonObject& root) { // we add JSON object: {"Autosave": {"autoSaveAfterSec": 10, "autoSavePreset": 99}} JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname + top[FPSTR(_autoSaveEnabled)] = enabled; top[FPSTR(_autoSaveAfterSec)] = autoSaveAfterSec; // usermodparam top[FPSTR(_autoSavePreset)] = autoSavePreset; // usermodparam top[FPSTR(_autoSaveApplyOnBoot)] = applyAutoSaveOnBoot; @@ -198,21 +201,30 @@ class AutoSaveUsermod : public Usermod { void readFromConfig(JsonObject& root) { // we look for JSON object: {"Autosave": {"autoSaveAfterSec": 10, "autoSavePreset": 99}} JsonObject top = root[FPSTR(_name)]; - if (!top.isNull() && top[FPSTR(_autoSaveAfterSec)] != nullptr) { - autoSaveAfterSec = top[FPSTR(_autoSaveAfterSec)].as(); - autoSavePreset = top[FPSTR(_autoSavePreset)].as(); - if (top[FPSTR(_autoSaveApplyOnBoot)].is()) { - // reading from cfg.json - applyAutoSaveOnBoot = top[FPSTR(_autoSaveApplyOnBoot)].as(); - } else { - // reading from POST message - String str = top[FPSTR(_autoSaveApplyOnBoot)]; // checkbox -> off or on - applyAutoSaveOnBoot = (bool)(str!="off"); // off is guaranteed to be present - } - DEBUG_PRINTLN(F("Autosave config (re)loaded.")); - } else { + if (top.isNull()) { DEBUG_PRINTLN(F("No config found. (Using defaults.)")); + return; } + + if (top[FPSTR(_autoSaveEnabled)].is()) { + // reading from cfg.json + enabled = top[FPSTR(_autoSaveEnabled)].as(); + } else { + // reading from POST message + String str = top[FPSTR(_autoSaveEnabled)]; // checkbox -> off or on + enabled = (bool)(str!="off"); // off is guaranteed to be present + } + autoSaveAfterSec = min(3600,max(10,top[FPSTR(_autoSaveAfterSec)].as())); + autoSavePreset = min(250,max(100,top[FPSTR(_autoSavePreset)].as())); + if (top[FPSTR(_autoSaveApplyOnBoot)].is()) { + // reading from cfg.json + applyAutoSaveOnBoot = top[FPSTR(_autoSaveApplyOnBoot)].as(); + } else { + // reading from POST message + String str = top[FPSTR(_autoSaveApplyOnBoot)]; // checkbox -> off or on + applyAutoSaveOnBoot = (bool)(str!="off"); // off is guaranteed to be present + } + DEBUG_PRINTLN(F("Autosave config (re)loaded.")); } /* @@ -226,6 +238,7 @@ class AutoSaveUsermod : public Usermod { // strings to reduce flash memory usage (used more than twice) const char AutoSaveUsermod::_name[] PROGMEM = "Autosave"; +const char AutoSaveUsermod::_autoSaveEnabled[] PROGMEM = "enabled"; const char AutoSaveUsermod::_autoSaveAfterSec[] PROGMEM = "autoSaveAfterSec"; const char AutoSaveUsermod::_autoSavePreset[] PROGMEM = "autoSavePreset"; const char AutoSaveUsermod::_autoSaveApplyOnBoot[] PROGMEM = "autoSaveApplyOnBoot";