Skip to content

Commit

Permalink
Enhanced usermods. (Aircoookie#2522)
Browse files Browse the repository at this point in the history
  • Loading branch information
blazoncek authored Feb 1, 2022
1 parent 6e0e5c1 commit 0a5a0be
Show file tree
Hide file tree
Showing 11 changed files with 1,883 additions and 860 deletions.
30 changes: 26 additions & 4 deletions usermods/PIR_sensor_switch/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ The LED strip is switched [using a relay](https://github.com/Aircoookie/WLED/wik

## Webinterface

The info page in the web interface shows the remaining time of the off timer.
The info page in the web interface shows the remaining time of the off timer. Usermod can also be temporarily disbled/enabled from the info page by clicking PIR button.

## Sensor connection

My setup uses an HC-SR501 sensor, a HC-SR505 should also work.
My setup uses an HC-SR501 or HC-SR602 sensor, a HC-SR505 should also work.

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.
You can also use usermod's off timer instead of sensor's. In such case rotate the potentiometer to its shortest time possible (or use SR602 which lacks such potentiometer).

## Usermod installation

Expand Down Expand Up @@ -59,6 +60,8 @@ void registerUsermods()
}
```

**NOTE:** Usermod has been included in master branch of WLED so it can be compiled in directly just by defining `-D USERMOD_PIRSWITCH` and optionaly `-D PIR_SENSOR_PIN=16` to override default pin.

## API to enable/disable the PIR sensor from outside. For example from another usermod.

To query or change the PIR sensor state the methods `bool PIRsensorEnabled()` and `void EnablePIRsensor(bool enable)` are available.
Expand Down Expand Up @@ -95,8 +98,27 @@ class MyUsermod : public Usermod {
};
```
Have fun - @gegu
### Configuration options
Usermod can be configured in Usermods settings page.
* `PIRenabled` - enable/disable usermod
* `pin` - dynamically change GPIO pin where PIR sensor is attached to ESP
* `PIRoffSec` - number of seconds after PIR sensor deactivates when usermod triggers Off preset (or turns WLED off)
* `on-preset` - preset triggered when PIR activates (if this is 0 it will just turn WLED on)
* `off-preset` - preset triggered when PIR deactivates (if this is 0 it will just turn WLED off)
* `nighttime-only` - enable triggering only between sunset and sunrise (you will need to set up _NTP_, _Lat_ & _Lon_ in Time & Macro settings)
* `mqtt-only` - only send MQTT messages, do not interact with WLED
* `off-only` - only trigger presets or turn WLED on/off in WLED is not already on (displaying effect)
* `notifications` - enable or disable sending notifications to other WLED instances using Sync button
Have fun - @gegu & @blazoncek
## Change log
2021-04
* Adaptation for runtime configuration.
* Adaptation for runtime configuration.
2021-11
* Added information about dynamic configuration options
* Added option to temporary enable/disble usermod from WLED UI (Info dialog)
175 changes: 109 additions & 66 deletions usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,29 @@ class PIRsensorSwitch : public Usermod
bool PIRsensorEnabled() { return enabled; }

private:
// PIR sensor pin
int8_t PIRsensorPin = PIR_SENSOR_PIN;
// notification mode for colorUpdated()
const byte NotifyUpdateMode = CALL_MODE_NO_NOTIFY; // CALL_MODE_DIRECT_CHANGE
// delay before switch off after the sensor state goes LOW
uint32_t m_switchOffDelay = 600000; // 10min
// off timer start time
uint32_t m_offTimerStart = 0;
// current PIR sensor pin state
byte sensorPinState = LOW;
// PIR sensor enabled
bool enabled = true;
// status of initialisation
bool initDone = false;
// on and off presets
uint8_t m_onPreset = 0;
uint8_t m_offPreset = 0;
// flag to indicate that PIR sensor should activate WLED during nighttime only
bool m_nightTimeOnly = false;
// flag to send MQTT message only (assuming it is enabled)
bool m_mqttOnly = false;
// flag to enable triggering only if WLED is initially off (LEDs are not on, preventing running effect being overwritten by PIR)
bool m_offOnly = false;
bool PIRtriggered = false;

byte prevPreset = 0;
byte prevPlaylist = 0;
bool savedState = false;

uint32_t offTimerStart = 0; // off timer start time
byte NotifyUpdateMode = CALL_MODE_NO_NOTIFY; // notification mode for colorUpdated(): CALL_MODE_NO_NOTIFY or CALL_MODE_DIRECT_CHANGE
byte sensorPinState = LOW; // current PIR sensor pin state
bool initDone = false; // status of initialization
bool PIRtriggered = false;
unsigned long lastLoop = 0;

// configurable parameters
bool enabled = true; // PIR sensor enabled
int8_t PIRsensorPin = PIR_SENSOR_PIN; // PIR sensor pin
uint32_t m_switchOffDelay = 600000; // delay before switch off after the sensor state goes LOW (10min)
uint8_t m_onPreset = 0; // on preset
uint8_t m_offPreset = 0; // off preset
bool m_nightTimeOnly = false; // flag to indicate that PIR sensor should activate WLED during nighttime only
bool m_mqttOnly = false; // flag to send MQTT message only (assuming it is enabled)
// flag to enable triggering only if WLED is initially off (LEDs are not on, preventing running effect being overwritten by PIR)
bool m_offOnly = false;

// strings to reduce flash memory usage (used more than twice)
static const char _name[];
static const char _switchOffDelay[];
Expand All @@ -91,30 +87,30 @@ class PIRsensorSwitch : public Usermod
static const char _nightTime[];
static const char _mqttOnly[];
static const char _offOnly[];
static const char _notify[];

/**
* check if it is daytime
* if sunrise/sunset is not defined (no NTP or lat/lon) default to nighttime
*/
bool isDayTime() {
bool isDayTime = false;
updateLocalTime();
uint8_t hr = hour(localTime);
uint8_t mi = minute(localTime);

if (sunrise && sunset) {
if (hour(sunrise)<hr && hour(sunset)>hr) {
isDayTime = true;
return true;
} else {
if (hour(sunrise)==hr && minute(sunrise)<mi) {
isDayTime = true;
return true;
}
if (hour(sunset)==hr && minute(sunset)>mi) {
isDayTime = true;
return true;
}
}
}
return isDayTime;
return false;
}

/**
Expand All @@ -124,17 +120,47 @@ class PIRsensorSwitch : public Usermod
{
if (m_offOnly && bri && (switchOn || (!PIRtriggered && !switchOn))) return;
PIRtriggered = switchOn;
if (switchOn && m_onPreset) {
applyPreset(m_onPreset);
} else if (!switchOn && m_offPreset) {
applyPreset(m_offPreset);
} else if (switchOn && bri == 0) {
bri = briLast;
colorUpdated(NotifyUpdateMode);
} else if (!switchOn && bri != 0) {
briLast = bri;
bri = 0;
colorUpdated(NotifyUpdateMode);
if (switchOn) {
if (m_onPreset) {
if (currentPlaylist>0) prevPlaylist = currentPlaylist;
else if (currentPreset>0) prevPreset = currentPreset;
else {
saveTemporaryPreset();
savedState = true;
prevPlaylist = 0;
prevPreset = 0;
}
applyPreset(m_onPreset, NotifyUpdateMode);
return;
}
// preset not assigned
if (bri == 0) {
bri = briLast;
colorUpdated(NotifyUpdateMode);
}
} else {
if (m_offPreset) {
applyPreset(m_offPreset, NotifyUpdateMode);
return;
} else if (prevPlaylist) {
applyPreset(prevPlaylist, NotifyUpdateMode);
prevPlaylist = 0;
return;
} else if (prevPreset) {
applyPreset(prevPreset, NotifyUpdateMode);
prevPreset = 0;
return;
} else if (savedState) {
applyTemporaryPreset();
savedState = false;
return;
}
// preset not assigned
if (bri != 0) {
briLast = bri;
bri = 0;
colorUpdated(NotifyUpdateMode);
}
}
}

Expand All @@ -160,12 +186,12 @@ class PIRsensorSwitch : public Usermod
sensorPinState = pinState; // change previous state

if (sensorPinState == HIGH) {
m_offTimerStart = 0;
offTimerStart = 0;
if (!m_mqttOnly && (!m_nightTimeOnly || (m_nightTimeOnly && !isDayTime()))) switchStrip(true);
publishMqtt("on");
} else /*if (bri != 0)*/ {
// start switch off timer
m_offTimerStart = millis();
offTimerStart = millis();
}
return true;
}
Expand All @@ -177,14 +203,14 @@ class PIRsensorSwitch : public Usermod
*/
bool handleOffTimer()
{
if (m_offTimerStart > 0 && millis() - m_offTimerStart > m_switchOffDelay)
if (offTimerStart > 0 && millis() - offTimerStart > m_switchOffDelay)
{
if (enabled == true)
{
if (!m_mqttOnly && (!m_nightTimeOnly || (m_nightTimeOnly && !isDayTime()))) switchStrip(false);
publishMqtt("off");
}
m_offTimerStart = 0;
offTimerStart = 0;
return true;
}
return false;
Expand Down Expand Up @@ -248,15 +274,25 @@ class PIRsensorSwitch : public Usermod
JsonObject user = root["u"];
if (user.isNull()) user = root.createNestedObject("u");

if (enabled)
{
// off timer
String uiDomString = F("PIR <i class=\"icons\">&#xe325;</i>");
JsonArray infoArr = user.createNestedArray(uiDomString); // timer value
if (m_offTimerStart > 0)
String uiDomString = F("<button class=\"btn\" onclick=\"requestJson({");
uiDomString += FPSTR(_name);
uiDomString += F(":{");
uiDomString += FPSTR(_enabled);
if (enabled) {
uiDomString += F(":false}});\">");
uiDomString += F("PIR <i class=\"icons\">&#xe325;</i>");
} else {
uiDomString += F(":true}});\">");
uiDomString += F("PIR <i class=\"icons\">&#xe08f;</i>");
}
uiDomString += F("</button>");
JsonArray infoArr = user.createNestedArray(uiDomString); // timer value

if (enabled) {
if (offTimerStart > 0)
{
uiDomString = "";
unsigned int offSeconds = (m_switchOffDelay - (millis() - m_offTimerStart)) / 1000;
unsigned int offSeconds = (m_switchOffDelay - (millis() - offTimerStart)) / 1000;
if (offSeconds >= 3600)
{
uiDomString += (offSeconds / 3600);
Expand All @@ -282,8 +318,6 @@ class PIRsensorSwitch : public Usermod
infoArr.add(sensorPinState ? F("sensor on") : F("inactive"));
}
} else {
String uiDomString = F("PIR sensor");
JsonArray infoArr = user.createNestedArray(uiDomString);
infoArr.add(F("disabled"));
}
}
Expand All @@ -302,26 +336,34 @@ class PIRsensorSwitch : public Usermod
* 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
*/
/*

void readFromJsonState(JsonObject &root)
{
if (!initDone) return; // prevent crash on boot applyPreset()
JsonObject usermod = root[FPSTR(_name)];
if (!usermod.isNull()) {
if (usermod[FPSTR(_enabled)].is<bool>()) {
enabled = usermod[FPSTR(_enabled)].as<bool>();
}
}
}
*/


/**
* provide the changeable values
*/
void addToConfig(JsonObject &root)
{
JsonObject top = root.createNestedObject(FPSTR(_name));
top[FPSTR(_enabled)] = enabled;
top[FPSTR(_enabled)] = enabled;
top[FPSTR(_switchOffDelay)] = m_switchOffDelay / 1000;
top["pin"] = PIRsensorPin;
top[FPSTR(_onPreset)] = m_onPreset;
top[FPSTR(_offPreset)] = m_offPreset;
top[FPSTR(_nightTime)] = m_nightTimeOnly;
top[FPSTR(_mqttOnly)] = m_mqttOnly;
top[FPSTR(_offOnly)] = m_offOnly;
top["pin"] = PIRsensorPin;
top[FPSTR(_onPreset)] = m_onPreset;
top[FPSTR(_offPreset)] = m_offPreset;
top[FPSTR(_nightTime)] = m_nightTimeOnly;
top[FPSTR(_mqttOnly)] = m_mqttOnly;
top[FPSTR(_offOnly)] = m_offOnly;
top[FPSTR(_notify)] = (NotifyUpdateMode != CALL_MODE_NO_NOTIFY);
DEBUG_PRINTLN(F("PIR config saved."));
}

Expand All @@ -336,9 +378,9 @@ class PIRsensorSwitch : public Usermod
bool oldEnabled = enabled;
int8_t oldPin = PIRsensorPin;

DEBUG_PRINT(FPSTR(_name));
JsonObject top = root[FPSTR(_name)];
if (top.isNull()) {
DEBUG_PRINT(FPSTR(_name));
DEBUG_PRINTLN(F(": No config found. (Using defaults.)"));
return false;
}
Expand All @@ -351,15 +393,15 @@ class PIRsensorSwitch : public Usermod

m_onPreset = top[FPSTR(_onPreset)] | m_onPreset;
m_onPreset = max(0,min(250,(int)m_onPreset));

m_offPreset = top[FPSTR(_offPreset)] | m_offPreset;
m_offPreset = max(0,min(250,(int)m_offPreset));

m_nightTimeOnly = top[FPSTR(_nightTime)] | m_nightTimeOnly;
m_mqttOnly = top[FPSTR(_mqttOnly)] | m_mqttOnly;
m_offOnly = top[FPSTR(_offOnly)] | m_offOnly;

DEBUG_PRINT(FPSTR(_name));
NotifyUpdateMode = top[FPSTR(_notify)] ? CALL_MODE_DIRECT_CHANGE : CALL_MODE_NO_NOTIFY;

if (!initDone) {
// reading config prior to setup()
DEBUG_PRINTLN(F(" config loaded."));
Expand All @@ -385,7 +427,7 @@ class PIRsensorSwitch : public Usermod
DEBUG_PRINTLN(F(" config (re)loaded."));
}
// use "return !top["newestParameter"].isNull();" when updating Usermod with new features
return !top[FPSTR(_offOnly)].isNull();
return !top[FPSTR(_notify)].isNull();
}

/**
Expand All @@ -407,3 +449,4 @@ const char PIRsensorSwitch::_offPreset[] PROGMEM = "off-preset";
const char PIRsensorSwitch::_nightTime[] PROGMEM = "nighttime-only";
const char PIRsensorSwitch::_mqttOnly[] PROGMEM = "mqtt-only";
const char PIRsensorSwitch::_offOnly[] PROGMEM = "off-only";
const char PIRsensorSwitch::_notify[] PROGMEM = "notifications";
Loading

0 comments on commit 0a5a0be

Please sign in to comment.