Removed PIR instance methods, added AutoSave enable/disable.

This commit is contained in:
Blaz Kristan 2021-05-03 21:22:03 +02:00
parent 24932d6ba3
commit de7da8b26e
3 changed files with 43 additions and 67 deletions

View File

@ -10,27 +10,14 @@ The LED strip is switched [using a relay](https://github.com/Aircoookie/WLED/wik
## Webinterface ## Webinterface
The info page in the web interface shows the items below 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. - the remaining time of the off timer.
**I recommend to deactivate the sensor before an OTA update and activate it again afterwards**.
## 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.
## Sensor connection ## Sensor connection
My setup uses an HC-SR501 sensor, a HC-SR505 should also work. 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. [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. 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. ## 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. 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: ### There are two options to get access to the usermod instance:
@ -98,9 +83,12 @@ class MyUsermod : public Usermod {
//... //...
void togglePIRSensor() { void togglePIRSensor() {
if (PIRsensorSwitch::GetInstance() != nullptr) { #ifdef USERMOD_PIR_SENSOR_SWITCH
PIRsensorSwitch::GetInstance()->EnablePIRsensor(!PIRsensorSwitch::GetInstance()->PIRsensorEnabled()); PIRsensorSwitch *PIRsensor = (PIRsensorSwitch::*) usermods.lookup(USERMOD_ID_PIRSWITCH);
if (PIRsensor != nullptr) {
PIRsensor->EnablePIRsensor(!PIRsensor->PIRsensorEnabled());
} }
#endif
} }
//... //...
}; };

View File

@ -39,24 +39,11 @@ public:
/** /**
* constructor * constructor
*/ */
PIRsensorSwitch() PIRsensorSwitch() {}
{
// set static instance pointer
PIRsensorSwitchInstance(this);
}
/** /**
* desctructor * desctructor
*/ */
~PIRsensorSwitch() ~PIRsensorSwitch() {}
{
PIRsensorSwitchInstance(nullptr, true);
;
}
/**
* return the instance pointer of the class
*/
static PIRsensorSwitch *GetInstance() { return PIRsensorSwitchInstance(); }
/** /**
* Enable/Disable the PIR sensor * Enable/Disable the PIR sensor
@ -98,11 +85,6 @@ private:
*/ */
static void IRAM_ATTR ISR_PIRstateChange(); static void IRAM_ATTR ISR_PIRstateChange();
/**
* Set/get instance pointer
*/
static PIRsensorSwitch *PIRsensorSwitchInstance(PIRsensorSwitch *pInstance = nullptr, bool bRemoveInstance = false);
/** /**
* switch strip on/off * switch strip on/off
*/ */
@ -253,17 +235,18 @@ public:
*/ */
if (m_PIRenabled) if (m_PIRenabled)
{ {
/*
JsonArray infoArr = user.createNestedArray(F("PIR switch-off timer after")); //name JsonArray infoArr = user.createNestedArray(F("PIR switch-off timer after")); //name
String uiDomString = F("<input type=\"number\" min=\"1\" max=\"720\" value=\""); String uiDomString = F("<input type=\"number\" min=\"1\" max=\"720\" value=\"");
uiDomString += (m_switchOffDelay / 60000); uiDomString += (m_switchOffDelay / 60000);
uiDomString += F("\" onchange=\"requestJson({PIRoffSec:parseInt(this.value)*60});\">min"); uiDomString += F("\" onchange=\"requestJson({PIRoffSec:parseInt(this.value)*60});\">min");
infoArr.add(uiDomString); infoArr.add(uiDomString);
*/
// off timer // off timer
String uiDomString = F("PIR <i class=\"icons\">&#xe325;</i>");
JsonArray infoArr = user.createNestedArray(uiDomString); // timer value
if (m_offTimerStart > 0) if (m_offTimerStart > 0)
{ {
uiDomString = F("<i class=\"icons\">&#xe325;</i>");
infoArr = user.createNestedArray(uiDomString); // timer value
uiDomString = ""; uiDomString = "";
unsigned int offSeconds = (m_switchOffDelay - (millis() - m_offTimerStart)) / 1000; unsigned int offSeconds = (m_switchOffDelay - (millis() - m_offTimerStart)) / 1000;
if (offSeconds >= 3600) if (offSeconds >= 3600)
@ -287,6 +270,8 @@ public:
} }
uiDomString += (offSeconds); uiDomString += (offSeconds);
infoArr.add(uiDomString + F("s")); infoArr.add(uiDomString + F("s"));
} else {
infoArr.add(F("inactive"));
} }
} }
} }
@ -448,17 +433,7 @@ void IRAM_ATTR PIRsensorSwitch::ISR_PIRstateChange()
newPIRsensorState(true, true); 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) // strings to reduce flash memory usage (used more than twice)
const char PIRsensorSwitch::_name[] PROGMEM = "PIRsensorSwitch"; const char PIRsensorSwitch::_name[] PROGMEM = "PIRsensorSwitch";
const char PIRsensorSwitch::_switchOffDelay[] PROGMEM = "PIRoffSec";
const char PIRsensorSwitch::_enabled[] PROGMEM = "PIRenabled"; const char PIRsensorSwitch::_enabled[] PROGMEM = "PIRenabled";
const char PIRsensorSwitch::_switchOffDelay[] PROGMEM = "PIRoffSec";

View File

@ -30,6 +30,7 @@ class AutoSaveUsermod : public Usermod {
bool firstLoop = true; bool firstLoop = true;
bool initDone = false; bool initDone = false;
bool enabled = true;
// configurable parameters // configurable parameters
unsigned long autoSaveAfterSec = 15; // 15s by default 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) // strings to reduce flash memory usage (used more than twice)
static const char _name[]; static const char _name[];
static const char _autoSaveEnabled[];
static const char _autoSaveAfterSec[]; static const char _autoSaveAfterSec[];
static const char _autoSavePreset[]; static const char _autoSavePreset[];
static const char _autoSaveApplyOnBoot[]; static const char _autoSaveApplyOnBoot[];
@ -95,7 +97,7 @@ class AutoSaveUsermod : public Usermod {
* Da loop. * Da loop.
*/ */
void 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(); unsigned long now = millis();
uint8_t currentMode = strip.getMode(); uint8_t currentMode = strip.getMode();
@ -181,6 +183,7 @@ class AutoSaveUsermod : public Usermod {
void addToConfig(JsonObject& root) { void addToConfig(JsonObject& root) {
// we add JSON object: {"Autosave": {"autoSaveAfterSec": 10, "autoSavePreset": 99}} // we add JSON object: {"Autosave": {"autoSaveAfterSec": 10, "autoSavePreset": 99}}
JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname JsonObject top = root.createNestedObject(FPSTR(_name)); // usermodname
top[FPSTR(_autoSaveEnabled)] = enabled;
top[FPSTR(_autoSaveAfterSec)] = autoSaveAfterSec; // usermodparam top[FPSTR(_autoSaveAfterSec)] = autoSaveAfterSec; // usermodparam
top[FPSTR(_autoSavePreset)] = autoSavePreset; // usermodparam top[FPSTR(_autoSavePreset)] = autoSavePreset; // usermodparam
top[FPSTR(_autoSaveApplyOnBoot)] = applyAutoSaveOnBoot; top[FPSTR(_autoSaveApplyOnBoot)] = applyAutoSaveOnBoot;
@ -198,21 +201,30 @@ class AutoSaveUsermod : public Usermod {
void readFromConfig(JsonObject& root) { void readFromConfig(JsonObject& root) {
// we look for JSON object: {"Autosave": {"autoSaveAfterSec": 10, "autoSavePreset": 99}} // we look for JSON object: {"Autosave": {"autoSaveAfterSec": 10, "autoSavePreset": 99}}
JsonObject top = root[FPSTR(_name)]; JsonObject top = root[FPSTR(_name)];
if (!top.isNull() && top[FPSTR(_autoSaveAfterSec)] != nullptr) { if (top.isNull()) {
autoSaveAfterSec = top[FPSTR(_autoSaveAfterSec)].as<int>();
autoSavePreset = top[FPSTR(_autoSavePreset)].as<int>();
if (top[FPSTR(_autoSaveApplyOnBoot)].is<bool>()) {
// reading from cfg.json
applyAutoSaveOnBoot = top[FPSTR(_autoSaveApplyOnBoot)].as<bool>();
} 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 {
DEBUG_PRINTLN(F("No config found. (Using defaults.)")); DEBUG_PRINTLN(F("No config found. (Using defaults.)"));
return;
} }
if (top[FPSTR(_autoSaveEnabled)].is<bool>()) {
// reading from cfg.json
enabled = top[FPSTR(_autoSaveEnabled)].as<bool>();
} 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<int>()));
autoSavePreset = min(250,max(100,top[FPSTR(_autoSavePreset)].as<int>()));
if (top[FPSTR(_autoSaveApplyOnBoot)].is<bool>()) {
// reading from cfg.json
applyAutoSaveOnBoot = top[FPSTR(_autoSaveApplyOnBoot)].as<bool>();
} 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) // strings to reduce flash memory usage (used more than twice)
const char AutoSaveUsermod::_name[] PROGMEM = "Autosave"; const char AutoSaveUsermod::_name[] PROGMEM = "Autosave";
const char AutoSaveUsermod::_autoSaveEnabled[] PROGMEM = "enabled";
const char AutoSaveUsermod::_autoSaveAfterSec[] PROGMEM = "autoSaveAfterSec"; const char AutoSaveUsermod::_autoSaveAfterSec[] PROGMEM = "autoSaveAfterSec";
const char AutoSaveUsermod::_autoSavePreset[] PROGMEM = "autoSavePreset"; const char AutoSaveUsermod::_autoSavePreset[] PROGMEM = "autoSavePreset";
const char AutoSaveUsermod::_autoSaveApplyOnBoot[] PROGMEM = "autoSaveApplyOnBoot"; const char AutoSaveUsermod::_autoSaveApplyOnBoot[] PROGMEM = "autoSaveApplyOnBoot";