diff --git a/usermods/PIR_sensor_switch_v2/PIR_Highlight_Standby b/usermods/PIR_sensor_switch_v2/PIR_Highlight_Standby new file mode 100644 index 0000000000..46756f7d57 --- /dev/null +++ b/usermods/PIR_sensor_switch_v2/PIR_Highlight_Standby @@ -0,0 +1,347 @@ +#pragma once + +#include "wled.h" + +/* + * -------------------- + * Rawframe edit: + * - TESTED ON WLED VS.0.10.1 - WHERE ONLY PRESET 16 SAVES SEGMENTS - some macros may not be needed if this changes. + * - Code has been modified as my usage changed, as such it has poor use of functions vs if thens, but feel free to change it for me :) + * + * Edited to SWITCH between two lighting scenes/modes : STANDBY and HIGHLIGHT + * + * Usage: + * - Standby is the default mode and Highlight is activated when the PIR detects activity. + * - PIR delay now set to same value as Nightlight feature on boot but otherwise controlled as normal. + * - Standby and Highlight brightness can be set on the fly (default values set on boot via macros calling presets). + * - Macros are used to set Standby and Highlight states (macros can load saved presets etc). + * + * - Macro short button press = Highlight state default (used on boot only and sets default brightness). + * - Macro double button press = Standby state default (used on boot only and sets default brightness). + * - Macro long button press = Highlight state (after boot). + * - Macro 16 = Standby state (after boot). + * + * ! It is advised not to set 'Apply preset at boot' or a boot macro (that activates a preset) as we will call our own macros on boot. + * + * - When the strip is off before PIR activates the strip will return to off for Standby mode, and vice versa. + * - When the strip is turned off while in Highlight mode, it will return to standby mode. (This behaviour could be changed easily if for some reason you wanted the lights to go out when the pir is activated). + * - Macros can be chained so you could do almost anything, such as have standby mode also turn on the nightlight function with a new time delay. + * + * Segment Notes: + * - It's easier to save the segment selections in preset than apply via macro while we a limited to preset 16. (Ie, instead of selecting sections at the point of activating standby/highlight modes). + * - Because only preset 16 saves segments, for now we are having to use addiotional macros to control segments where they are involved. Macros can be chained so this works but it would be better if macros also accepted json-api commands. (Testing http api segement behaviour of SS with SB left me a little confused). + * + * Future: + * - Maybe a second timer/timetable that turns on/off standby mode also after set inactivity period / date & times. For now this can be achieved others ways so may not be worth eating more processing power. + * + * -------------------- + * + * This usermod handles PIR sensor states. + * The strip will be switched on and the off timer will be resetted when the sensor goes HIGH. + * When the sensor state goes LOW, the off timer is started and when it expires, the strip is switched off. + * + * + * Usermods allow you to add own functionality to WLED more easily + * See: https://github.com/wled-dev/WLED/wiki/Add-own-functionality + * + * v2 usermods are class inheritance based and can (but don't have to) implement more functions, each of them is shown in this example. + * Multiple v2 usermods can be added to one compilation easily. + * + * Creating a usermod: + * This file serves as an example. If you want to create a usermod, it is recommended to use usermod_v2_empty.h from the usermods folder as a template. + * Please remember to rename the class and file to a descriptive name. + * You may also use multiple .h and .cpp files. + * + * Using a usermod: + * 1. Copy the usermod into the sketch folder (same folder as wled00.ino) + * 2. Register the usermod by adding #include "usermod_filename.h" in the top and registerUsermod(new MyUsermodClass()) in the bottom of usermods_list.cpp + */ + +class PIRsensorSwitch : public Usermod { + private: + // PIR sensor pin + const uint8_t PIRsensorPin = 13; // D7 on D1 mini + // notification mode for stateUpdated() + const byte NotifyUpdateMode = CALL_MODE_NO_NOTIFY; // CALL_MODE_DIRECT_CHANGE + // 1 min delay before switch off after the sensor state goes LOW + uint32_t m_switchOffDelay = 60000; + // off timer start time + uint32_t m_offTimerStart = 0; + // current PIR sensor pin state + byte m_PIRsensorPinState = LOW; + // PIR sensor enabled - ISR attached + bool m_PIRenabled = true; + // temp standby brightness store. initial value set as nightlight default target brightness + byte briStandby _INIT(nightlightTargetBri); + // temp hightlight brightness store. initial value set as current brightness + byte briHighlight _INIT(bri); + // highlight active/deactive monitor + bool highlightActive = false; + // wled on/off state in standby mode + bool standbyoff = false; + + /* + * return or change if new PIR sensor state is available + */ + static volatile bool newPIRsensorState(bool changeState = false, bool newState = false) { + static volatile bool s_PIRsensorState = false; + if (changeState) { + s_PIRsensorState = newState; + } + return s_PIRsensorState; + } + + /* + * PIR sensor state has changed + */ + static void IRAM_ATTR ISR_PIRstateChange() { + newPIRsensorState(true, true); + } + + /* + * switch strip on/off + */ + // now allowing adjustable standby and highlight brightness + void switchStrip(bool switchOn) { + //if (switchOn && bri == 0) { + if (switchOn) { // **pir sensor is on and activated** + //bri = briLast; + if (bri != 0) { // is WLED currently on + if (highlightActive) { // and is Highlight already on + briHighlight = bri; // then update highlight brightness with current brightness + } + else { + briStandby = bri; // else update standby brightness with current brightness + } + } + else { // WLED is currently off + if (!highlightActive) { // and Highlight is not already on + briStandby = briLast; // then update standby brightness with last active brightness (before turned off) + standbyoff = true; + } + else { // and Highlight is already on + briHighlight = briLast; // then set hightlight brightness to last active brightness (before turned off) + } + } + applyMacro(16); // apply highlight lighting without brightness + if (bri != briHighlight) { + bri = briHighlight; // set current highlight brightness to last set highlight brightness + } + stateUpdated(NotifyUpdateMode); + highlightActive = true; // flag highlight is on + } + else { // **pir timer has elapsed** + //briLast = bri; + //bri = 0; + if (bri != 0) { // is WLED currently on + briHighlight = bri; // update highlight brightness with current brightness + if (!standbyoff) { // + bri = briStandby; // set standby brightness to last set standby brightness + } + else { // + briLast = briStandby; // set standby off brightness + bri = 0; // set power off in standby + standbyoff = false; // turn off flag + } + applyMacro(macroLongPress); // apply standby lighting without brightness + } + else { // WLED is currently off + briHighlight = briLast; // set last active brightness (before turned off) to highlight lighting brightness + if (!standbyoff) { // + bri = briStandby; // set standby brightness to last set standby brightness + } + else { // + briLast = briStandby; // set standby off brightness + bri = 0; // set power off in standby + standbyoff = false; // turn off flag + } + applyMacro(macroLongPress); // apply standby lighting without brightness + } + stateUpdated(NotifyUpdateMode); + highlightActive = false; // flag highlight is off + } + } + + /* + * Read and update PIR sensor state. + * Initilize/reset switch off timer + */ + bool updatePIRsensorState() { + if (newPIRsensorState()) { + m_PIRsensorPinState = digitalRead(PIRsensorPin); + + if (m_PIRsensorPinState == HIGH) { + m_offTimerStart = 0; + switchStrip(true); + } + else if (bri != 0) { + // start switch off timer + m_offTimerStart = millis(); + } + newPIRsensorState(true, false); + return true; + } + return false; + } + + /* + * switch off the strip if the delay has elapsed + */ + bool handleOffTimer() { + if (m_offTimerStart > 0) { + if ((millis() - m_offTimerStart > m_switchOffDelay) || bri == 0 ) { // now also checking for manual power off during highlight mode + switchStrip(false); + m_offTimerStart = 0; + return true; + } + } + return false; + } + + public: + //Functions called by WLED + + /* + * setup() is called once at boot. WiFi is not yet connected at this point. + * You can use it to initialize variables, sensors or similar. + */ + void setup() { + // PIR Sensor mode INPUT_PULLUP + pinMode(PIRsensorPin, INPUT_PULLUP); + // assign interrupt function and set CHANGE mode + attachInterrupt(digitalPinToInterrupt(PIRsensorPin), ISR_PIRstateChange, CHANGE); + // set delay to nightlight default duration on boot (after which json PIRoffSec overides if needed) + m_switchOffDelay = (nightlightDelayMins*60000); + applyMacro(macroButton); // apply default highlight lighting + briHighlight = bri; + applyMacro(macroDoublePress); // apply default standby lighting with brightness + briStandby = bri; + } + + + /* + * connected() is called every time the WiFi is (re)connected + * Use it to initialize network interfaces + */ + void connected() { + + } + + + /* + * loop() is called continuously. Here you can check for events, read sensors, etc. + */ + void loop() { + if (!updatePIRsensorState()) { + handleOffTimer(); + } + } + + /* + * addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API. + * + * Add PIR sensor state and switch off timer duration to jsoninfo + */ + void addToJsonInfo(JsonObject& root) + { + //this code adds "u":{"⏲ PIR sensor state":uiDomString} to the info object + // the value contains a button to toggle the sensor enabled/disabled + JsonObject user = root["u"]; + if (user.isNull()) user = root.createNestedObject("u"); + + JsonArray infoArr = user.createNestedArray("⏲ PIR sensor state"); //name + String uiDomString = ""; + infoArr.add(uiDomString); //value + + //this code adds "u":{"⏲ switch off timer":uiDomString} to the info object + infoArr = user.createNestedArray("⏲ switch off timer"); //name + + // off timer + if (m_offTimerStart > 0) { + uiDomString = ""; + unsigned int offSeconds = (m_switchOffDelay - (millis() - m_offTimerStart)) / 1000; + if (offSeconds >= 3600) { + uiDomString += (offSeconds / 3600); + uiDomString += " hours "; + offSeconds %= 3600; + } + if (offSeconds >= 60) { + uiDomString += (offSeconds / 60); + offSeconds %= 60; + } else if (uiDomString.length() > 0){ + uiDomString += 0; + } + if (uiDomString.length() > 0){ + uiDomString += " min "; + } + uiDomString += (offSeconds); + infoArr.add(uiDomString + " sec"); + } else { + infoArr.add("inactive"); + } + } + + + /* + * addToJsonState() can be used to add custom entries to the /json/state part of the JSON API (state object). + * Values in the state object may be modified by connected clients + * Add "PIRenabled" to json state. This can be used to disable/enable the sensor. + * Add "PIRoffSec" to json state. This can be used to adjust milliseconds . + */ + void addToJsonState(JsonObject& root) + { + root["PIRenabled"] = m_PIRenabled; + root["PIRoffSec"] = (m_switchOffDelay / 1000); + } + + + /* + * 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 + * Read "PIRenabled" from json state and switch enable/disable the PIR sensor. + * Read "PIRoffSec" from json state and adjust milliseconds . + */ + void readFromJsonState(JsonObject& root) + { + if (root["PIRoffSec"] != nullptr) { + m_switchOffDelay = (1000 * max(60UL, min(43200UL, root["PIRoffSec"].as()))); + } + + if (root["PIRenabled"] != nullptr) { + if (root["PIRenabled"] && !m_PIRenabled) { + attachInterrupt(digitalPinToInterrupt(PIRsensorPin), ISR_PIRstateChange, CHANGE); + newPIRsensorState(true, true); + } + else if(m_PIRenabled) { + detachInterrupt(PIRsensorPin); + } + m_PIRenabled = root["PIRenabled"]; + } + } + + + /* + * getId() allows you to optionally give your V2 usermod an unique ID (please define it in const.h!). + * This could be used in the future for the system to determine whether your usermod is installed. + */ + uint16_t getId() + { + return USERMOD_ID_PIRSWITCH; + } + + //More methods can be added in the future, this example will then be extended. + //Your usermod will remain compatible as it does not need to implement all methods from the Usermod base class! +}; diff --git a/usermods/PIR_sensor_switch_v2/PIR_sensor_switch_v2.cpp b/usermods/PIR_sensor_switch_v2/PIR_sensor_switch_v2.cpp new file mode 100644 index 0000000000..95a3228aa5 --- /dev/null +++ b/usermods/PIR_sensor_switch_v2/PIR_sensor_switch_v2.cpp @@ -0,0 +1,621 @@ +/* + * Rawframe + * Usermod for PIR sensor motion detection. + * + * This usermod handles PIR sensor states and triggers actions (presets) based on motion. + * It supports multiple PIR sensors and multiple actions, with configurable linking. + * + * Features: + * - Multiple PIR sensors + * - Multiple Actions (On/Off presets, delays) + * - Flexible linking between PIRs and Actions + * - Web UI for configuration and status + * - API for external control + */ + +#include "wled.h" + +// ---------- Configuration defaults ---------- +#ifndef PIR_SENSOR_PIN + #ifdef ARDUINO_ARCH_ESP32 + #define PIR_SENSOR_PIN 23 + #else + #define PIR_SENSOR_PIN 13 + #endif +#endif + +#ifndef PIR_SENSOR_MAX + #define PIR_SENSOR_MAX 2 +#endif + +#ifndef ACTION_MAX + #define ACTION_MAX 2 +#endif + +#ifndef PRESET_FIFO_SIZE + #define PRESET_FIFO_SIZE 16 +#endif + +#ifndef PRESET_SPACING_MS + #define PRESET_SPACING_MS 5 +#endif + +static const char _name[] PROGMEM = "MotionDetection"; + +class MotionDetectionUsermod : public Usermod { +private: + bool initDone = false; + unsigned long lastLoop = 0; + + // PIR config/runtime + bool pirEnabled[PIR_SENSOR_MAX] = { true }; + int8_t pirPins[PIR_SENSOR_MAX] = { PIR_SENSOR_PIN }; + bool pirState[PIR_SENSOR_MAX] = { LOW }; + + // Per-PIR links to actions (initialized at runtime to match ACTION_MAX) + // OPTIMIZATION: Use bitmask instead of 2D bool array to save RAM + uint8_t pirActions[PIR_SENSOR_MAX]; + + // Action profile (presets/timers/contributors). Enabled flag removed from struct usage. + struct ActionProfile { + uint8_t onPreset; + uint8_t offPreset; + uint32_t offDelayMs; + bool contrib[PIR_SENSOR_MAX]; + uint8_t activeCount; + unsigned long offStartMs; + ActionProfile() { + onPreset = 0; + offPreset = 0; + offDelayMs = 600 * 1000UL; + activeCount = 0; + offStartMs = 0; + for (int i = 0; i < PIR_SENSOR_MAX; i++) contrib[i] = false; + } + } actions[ACTION_MAX]; + + // Authoritative simple boolean array for enabled state (PIR-like) + bool actionEnabled[ACTION_MAX] = { true }; + + // Preset FIFO + uint8_t presetFifo[PRESET_FIFO_SIZE]; + uint8_t fifoHead = 0; + uint8_t fifoTail = 0; + unsigned long lastPresetApplyMs = 0; + + + + inline bool fifoEmpty() { return fifoHead == fifoTail; } + inline bool fifoFull() { return ((fifoHead + 1) % PRESET_FIFO_SIZE) == fifoTail; } + + inline void enqueuePreset(uint8_t preset) { + if (preset == 0) return; + // OPTIMIZATION: Prevent flooding by ignoring duplicate sequential requests + if (!fifoEmpty() && presetFifo[(fifoHead + PRESET_FIFO_SIZE - 1) % PRESET_FIFO_SIZE] == preset) return; + + if (fifoFull()) fifoTail = (fifoTail + 1) % PRESET_FIFO_SIZE; // drop oldest + presetFifo[fifoHead] = preset; + fifoHead = (fifoHead + 1) % PRESET_FIFO_SIZE; + } + + void processPresetFifo() { + if (fifoEmpty()) return; + unsigned long now = millis(); + if (now - lastPresetApplyMs < PRESET_SPACING_MS) return; + uint8_t preset = presetFifo[fifoTail]; + fifoTail = (fifoTail + 1) % PRESET_FIFO_SIZE; + lastPresetApplyMs = now; + applyPreset(preset, CALL_MODE_BUTTON_PRESET); + } + +public: + MotionDetectionUsermod() {} + ~MotionDetectionUsermod() {} + + /* + * readFromConfig() is called prior to setup() to read configuration from cfg.json + * You can use it to initialize variables, sensors or similar. + */ + bool readFromConfig(JsonObject &root) override { + if (!root.containsKey(FPSTR(_name))) return false; + JsonObject top = root[FPSTR(_name)]; + + // PIRs + char key[16]; // OPTIMIZATION: Buffer for snprintf to avoid String allocation + for (int i = 0; i < PIR_SENSOR_MAX; i++) { + snprintf(key, sizeof(key), "PIR %d", i + 1); + if (!top.containsKey(key)) continue; + JsonObject p = top[key]; + if (!p.isNull()) { + if (p.containsKey("pin")) pirPins[i] = p["pin"] | pirPins[i]; + if (p.containsKey("enabled")) pirEnabled[i] = p["enabled"].as(); // FIX: Explicit cast + for (int a = 0; a < ACTION_MAX; a++) { + snprintf(key, sizeof(key), "Action %d", a + 1); + if (p.containsKey(key)) { + bool linked = p[key].as(); // FIX: Explicit cast for Link Action checkboxes + if (linked) pirActions[i] |= (1 << a); + else pirActions[i] &= ~(1 << a); + } + } + } + } + + // Actions: read into struct fields and read enabled into boolean array + for (int a = 0; a < ACTION_MAX; a++) { + snprintf(key, sizeof(key), "Action %d", a + 1); + if (!top.containsKey(key)) continue; + JsonObject ap = top[key]; + if (!ap.isNull()) { + if (ap.containsKey("onPreset")) actions[a].onPreset = ap["onPreset"] | actions[a].onPreset; + if (ap.containsKey("offPreset")) actions[a].offPreset = ap["offPreset"] | actions[a].offPreset; + if (ap.containsKey("offSec")) { + uint32_t s = ap["offSec"] | (actions[a].offDelayMs / 1000UL); + if (s > 4294967) s = 4294967; // Max ~49.7 days, prevents overflow when multiplied by 1000 + actions[a].offDelayMs = s * 1000UL; + } + if (ap.containsKey("enabled")) { + actionEnabled[a] = ap["enabled"].as(); // FIX: Explicit cast + } + } + } + + return true; + } + + /* + * setup() is called once at boot. WiFi is not yet connected at this point. + * readFromConfig() is called prior to setup() + */ + void setup() override { + // allocate pins and initialize states + for (int i = 0; i < PIR_SENSOR_MAX; i++) { + pirState[i] = LOW; + if (pirPins[i] < 0) { pirEnabled[i] = false; continue; } + if (PinManager::allocatePin(pirPins[i], false, PinOwner::UM_PIR)) { + #ifdef ESP8266 + pinMode(pirPins[i], pirPins[i] == 16 ? INPUT_PULLDOWN_16 : INPUT_PULLUP); + #else + pinMode(pirPins[i], INPUT_PULLDOWN); + #endif + pirState[i] = digitalRead(pirPins[i]); + } else { + DEBUG_PRINT(F("PIR pin allocation failed: ")); + DEBUG_PRINTLN(pirPins[i]); + pirPins[i] = -1; + pirEnabled[i] = false; + } + } + + // initialize action runtime state (pirActions loaded from config, not overwritten here!) + for (int a = 0; a < ACTION_MAX; a++) { + // actionEnabled[a] = true; // REMOVED: Do not overwrite config/state! + actions[a].activeCount = 0; + actions[a].offStartMs = 0; + for (int i = 0; i < PIR_SENSOR_MAX; i++) actions[a].contrib[i] = false; + } + + fifoHead = fifoTail = 0; + lastPresetApplyMs = 0; + + initDone = true; + } + + /* + * loop() is called continuously. Here you can check for events, read sensors, etc. + */ + void loop() override { + if (!initDone) return; + + processPresetFifo(); + + // poll PIRs ~5Hz + if (millis() - lastLoop < 200) return; + lastLoop = millis(); + + for (int i = 0; i < PIR_SENSOR_MAX; i++) { + if (!pirEnabled[i] || pirPins[i] < 0) continue; + + bool pin = digitalRead(pirPins[i]); + if (pin == pirState[i]) continue; + pirState[i] = pin; + + if (pin == HIGH) { + // rising edge: mark contributor and manage action + for (int a = 0; a < ACTION_MAX; a++) { + if (!actionEnabled[a]) continue; // use simple boolean gating (PIR-like) + if (!((pirActions[i] >> a) & 0x01)) continue; // OPTIMIZATION: Bitmask check + + if (!actions[a].contrib[i]) { + actions[a].contrib[i] = true; + actions[a].activeCount++; + } + + // if action just became active, enqueue onPreset; always clear offStartMs + if (actions[a].activeCount == 1) { + // OPTIMIZATION: Removed redundant offStartMs = 0 + if (actions[a].onPreset) enqueuePreset(actions[a].onPreset); + } else { + // OPTIMIZATION: Removed redundant offStartMs = 0 + } + } + } else { + // falling edge: remove contributor, maybe start unified off timer + for (int a = 0; a < ACTION_MAX; a++) { + if (!actionEnabled[a]) continue; + if (!((pirActions[i] >> a) & 0x01)) continue; // OPTIMIZATION: Bitmask check + + if (actions[a].contrib[i]) { + actions[a].contrib[i] = false; + if (actions[a].activeCount > 0) actions[a].activeCount--; + if (actions[a].activeCount == 0) { + actions[a].offStartMs = millis(); + } + } + } + } + } + + // check unified off timers + unsigned long now = millis(); + for (int a = 0; a < ACTION_MAX; a++) { + if (!actionEnabled[a]) continue; + if (actions[a].activeCount == 0) { + if (actions[a].offStartMs > 0 && now - actions[a].offStartMs > actions[a].offDelayMs) { + actions[a].offStartMs = 0; + if (actions[a].offPreset) enqueuePreset(actions[a].offPreset); + } + } else { + // OPTIMIZATION: Removed redundant offStartMs = 0 + } + } + + processPresetFifo(); + } + + /* + * addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API. + * Creating an "u" object allows you to add custom key/value pairs to the Info section of the WLED web UI. + */ + void addToJsonInfo(JsonObject &root) override { + JsonObject user = root["u"]; + if (user.isNull()) user = root.createNestedObject("u"); + + char buf[64]; // OPTIMIZATION: Buffer for snprintf + // PIR blocks + for (int i = 0; i < PIR_SENSOR_MAX; i++) { + snprintf(buf, sizeof(buf), "PIR %d", i+1); + JsonArray infoArr = user.createNestedArray(buf); + + if (pirPins[i] < 0) { + infoArr.add("⛔ PIR not configured"); + continue; + } + + String uiDomString; + uiDomString = F(""); + + infoArr.add(pirEnabled[i] ? (pirState[i] ? "● motion" : "○ idle") : "⛔ disabled"); + infoArr.add(uiDomString); + + String linked = ""; + bool any = false; + for (int a = 0; a < ACTION_MAX; a++) { + if ((pirActions[i] >> a) & 0x01) { // OPTIMIZATION: Bitmask check + if (any) linked += ", "; + linked += String(a+1); + any = true; + } + } + infoArr.add(any ? String("🔗 actions: ") + linked : "🔗 no actions linked"); + } + + // Action blocks: use PIR-like keys and boolean array + unsigned long now = millis(); + for (int a = 0; a < ACTION_MAX; a++) { + snprintf(buf, sizeof(buf), "Action %d", a+1); + JsonArray infoArr = user.createNestedArray(buf); + + bool en = actionEnabled[a]; + + // status first + String status; + if (!en) { + status = "⛔ disabled"; + } else { + bool active = actions[a].activeCount > 0; + uint32_t remain = 0; + // Calculate remaining time, avoiding underflow if timer already elapsed + if (!active && actions[a].offStartMs > 0) { + unsigned long elapsed = now - actions[a].offStartMs; + if (elapsed < actions[a].offDelayMs) { + remain = (actions[a].offDelayMs - elapsed) / 1000; + } + } + if (active) status = "⏱ active"; + else if (remain > 0) status = String("⏱ off in ") + String(remain) + "s"; + else status = "○ inactive"; + } + infoArr.add(status); // MUST be first + + // button second (PIR-style key "actionN") + String uiDomString; + uiDomString = F(""); + infoArr.add(uiDomString); // MUST be second + + // details + String onP = actions[a].onPreset ? String(actions[a].onPreset) : String("none"); + String offP = actions[a].offPreset ? String(actions[a].offPreset) : String("none"); + infoArr.add(String("On: ") + onP + String(" Off: ") + offP); + infoArr.add(String("Delay: ") + String(actions[a].offDelayMs / 1000UL) + "s"); + } + + + } + + /* + * 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) override { + // accept nested under usermod name or flat keys + JsonObject um = root.containsKey(FPSTR(_name)) ? root[FPSTR(_name)] : root; + bool anyChange = false; + + // PIR keys + for (int i = 0; i < PIR_SENSOR_MAX; i++) { + String k = "pir" + String(i); + if (!um.containsKey(k)) continue; + bool en = um[k].is() ? um[k].as() : (um[k].as() != 0); + if (pirEnabled[i] != en) { + pirEnabled[i] = en; + anyChange = true; + if (!en) { + for (int a = 0; a < ACTION_MAX; a++) { + if (actions[a].contrib[i]) { + actions[a].contrib[i] = false; + if (actions[a].activeCount > 0) actions[a].activeCount--; + } + } + } + } + } + + // Action keys: accept actionN, actionN_on, actionN_off, 1-based actionN + // OPTIMIZATION: Use pointer arithmetic to parse keys without creating Strings + for (JsonPair p : um) { + const char* key = p.key().c_str(); + if (strncmp(key, "action", 6) != 0) continue; + + // extract numeric suffix + const char* ptr = key + 6; // after "action" + if (!isdigit(*ptr)) continue; + + int idx = 0; + while (isdigit(*ptr)) { + idx = idx * 10 + (*ptr - '0'); + ptr++; + } + + // tail after digits (may be "_on", "_off", or empty) + const char* tail = ptr; + if (*tail == '_') tail++; // skip underscore + + // map 1-based keys (action1 -> index 0) if needed + int mappedIdx = -1; + if (idx >= 0 && idx < ACTION_MAX && *tail == '\0') { + mappedIdx = idx; // action0 style + } else if (idx >= 1 && idx <= ACTION_MAX && *tail == '\0') { + mappedIdx = idx - 1; // action1 style -> index 0 + } else if (idx >= 0 && idx < ACTION_MAX) { + mappedIdx = idx; // action0_on style + } else { + continue; + } + + // determine desired enabled state + bool en = false; + bool found = false; + + if (*tail != '\0') { + if (strcasecmp(tail, "on") == 0 || strcasecmp(tail, "enable") == 0) { + en = true; + found = true; + } else if (strcasecmp(tail, "off") == 0 || strcasecmp(tail, "disable") == 0) { + en = false; + found = true; + } else { + // fallback: read value if present + en = um[key].is() ? um[key].as() : (um[key].as() != 0); + found = true; + } + } else { + // no tail: use the provided value (true/false or numeric) + en = um[key].is() ? um[key].as() : (um[key].as() != 0); + found = true; + } + + if (!found) continue; + if (mappedIdx < 0 || mappedIdx >= ACTION_MAX) continue; + + // set simple boolean and clear runtime contributor state when toggling + if (actionEnabled[mappedIdx] != en) { + actionEnabled[mappedIdx] = en; + anyChange = true; + // when disabling, clear contributors and active counts + if (!en) { + for (int i = 0; i < PIR_SENSOR_MAX; i++) { + if (actions[mappedIdx].contrib[i]) { + actions[mappedIdx].contrib[i] = false; + } + } + actions[mappedIdx].activeCount = 0; + actions[mappedIdx].offStartMs = 0; + } else { + // enabling: ensure timers cleared + actions[mappedIdx].offStartMs = 0; + } + } + } + + if (anyChange) stateUpdated(CALL_MODE_BUTTON); + } + + /* + * addToConfig() can be used to add custom persistent settings to the cfg.json file in the "um" (usermod) object. + * It will be called by WLED when settings are actually saved (for example, LED settings are saved) + */ + void addToConfig(JsonObject &root) override { + JsonObject top = root.createNestedObject(FPSTR(_name)); + + char key[16]; // OPTIMIZATION: Buffer for snprintf + // PIRs + for (int i = 0; i < PIR_SENSOR_MAX; i++) { + snprintf(key, sizeof(key), "PIR %d", i + 1); + JsonObject p = top.createNestedObject(key); + p["pin"] = pirPins[i]; + p["enabled"] = pirEnabled[i]; + for (int a = 0; a < ACTION_MAX; a++) { + snprintf(key, sizeof(key), "Action %d", a + 1); + p[key] = (bool)((pirActions[i] >> a) & 0x01); // OPTIMIZATION: Bitmask check + } + } + + // Actions + for (int a = 0; a < ACTION_MAX; a++) { + snprintf(key, sizeof(key), "Action %d", a + 1); + JsonObject ap = top.createNestedObject(key); + ap["enabled"] = actionEnabled[a]; + ap["onPreset"] = actions[a].onPreset; + ap["offPreset"] = actions[a].offPreset; + ap["offSec"] = actions[a].offDelayMs / 1000UL; + } + } + + /* + * appendConfigData() is called when user enters usermod settings page + * it may add additional metadata for certain entry fields (adding drop down is possible) + */ + void appendConfigData() override { + // Indicator to show the mod is active (will be updated by JS) + oappend(F("addInfo('MotionDetection:PIR 1:enabled',1,'');")); + + // Inject CSS via JS + // Scoped CSS to #pir-root + // Removed border from .um-g to prevent double lines between blocks + // Added border-top and border-bottom to #pir-root for the only desired lines + // OPTIMIZATION: Combined strings + oappend(F("var s=document.createElement('style');s.innerHTML='#pir-root{border-top:1px solid #444;border-bottom:1px solid #444;padding:10px 0;margin:10px 0} #pir-root .um-g{display:flex;flex-wrap:wrap;justify-content:center;gap:10px;margin:4px 0;padding:10px;border-radius:5px;background:#222} #pir-root .um-h{width:100%;text-align:center;font-weight:bold;margin-bottom:5px;color:#fca} #pir-root .um-i{display:flex;flex-direction:column;align-items:center;background:#333;padding:5px;border-radius:4px;min-width:80px;border:1px solid #555} #pir-root .um-i label{font-size:0.8em;margin-bottom:2px;color:#aaa} #pir-root .um-i input[type=\"checkbox\"]{margin:5px} #pir-root .um-i input[type=\"number\"],#pir-root .um-i select{width:70px;text-align:center} #pir-root hr{display:none !important} #pir-root br{display:none}';document.head.appendChild(s);")); + + // Inject Layout Logic + // OPTIMIZATION: Combined strings + oappend(F("function aUM(){" + "var st=document.getElementById('um-status');" + "try {" + "var count=0;" + // Fix Main Heading + "const headers = document.querySelectorAll('h3');" + "let header = null;" + "headers.forEach(h=>{ if(h.textContent.trim()==='MotionDetection'){ header=h; h.textContent='Motion Detection'; h.style.marginTop='20px'; h.style.color=''; h.style.textDecoration='none'; } });" + + // Create Root Container + "if(header && !document.getElementById('pir-root')){" + "const root = document.createElement('div'); root.id='pir-root';" + "header.parentNode.insertBefore(root, header);" + "root.appendChild(header);" + "let next = root.nextSibling;" + "while(next && next.tagName!=='H3' && next.tagName!=='BUTTON' && !(next.tagName==='DIV' && next.className==='overlay')){" + "let curr = next; next = next.nextSibling; root.appendChild(curr);" + "}" + "}" + + // Selector 'f' + "const f=(k)=>{var e=document.getElementsByName(`MotionDetection:${k}`); if(e.length===0)return null; for(var i=0;i{let c=e; if(c.previousSibling&&c.previousSibling.type==='hidden')c=c.previousSibling; if(c.previousSibling&&c.previousSibling.nodeType===3&&c.previousSibling.textContent.trim().length>0)c=c.previousSibling; return c;};" + + // Helper w: FIX: Robustly find hidden inputs by searching backwards + "const w=(e,l)=>{if(!e)return null;const d=document.createElement('div');d.className='um-i';" + "let curr=e.previousSibling; let lblNode=null;" + "if(curr&&curr.type==='hidden') curr=curr.previousSibling;" + "while(curr&&curr.nodeType===3&&!curr.textContent.trim()) curr=curr.previousSibling;" + "if(curr&&curr.nodeType===3) lblNode=curr;" + "if(lblNode) lblNode.remove();" + "const lbl=document.createElement('label');lbl.textContent=l||(lblNode?lblNode.textContent.trim():'');if(lbl.textContent)d.appendChild(lbl);" + // FIX: Search backwards for the hidden input with the same name + "let sib = e.previousSibling;" + "while(sib) {" + "if(sib.type === 'hidden' && sib.name === e.name) {" + "d.appendChild(sib); break;" + "}" + "if(sib.tagName === 'DIV' || sib.tagName === 'H3' || sib.tagName === 'BUTTON') break;" + "sib = sib.previousSibling;" + "}" + "d.appendChild(e);return d;};" + + // Loop for Actions + "for(let i=1;i<=8;i++){" + "const en=f(`Action ${i}:enabled`); if(!en) continue;" + "const g=document.createElement('div');g.className='um-g';g.innerHTML=`
Action ${i}
`;" + "const s=f(`Action ${i}:offSec`); const on=f(`Action ${i}:onPreset`); const off=f(`Action ${i}:offPreset`);" + "const anchor = en || on;" + "if(anchor) anchor.parentNode.insertBefore(g, getStart(anchor));" + "if(en){count++; g.appendChild(w(en, 'Enabled'));}" + "if(s){count++; g.appendChild(w(s, 'Off Delay (s)'));}" + "if(on){count++; g.appendChild(w(on, 'On Preset'));}" + "if(off){count++; g.appendChild(w(off, 'Off Preset'));}" + "}" + + // Loop for PIRs + "for(let i=1;i<=8;i++){" + "const en=f(`PIR ${i}:enabled`); if(!en) continue;" + "const g=document.createElement('div');g.className='um-g';g.innerHTML=`
PIR Sensor ${i}
`;" + "const p=f(`PIR ${i}:pin`);" + "const anchor = en || p;" + "if(anchor) anchor.parentNode.insertBefore(g, getStart(anchor));" + "if(en){count++; g.appendChild(w(en, 'Enabled'));}" + "if(p){count++; g.appendChild(w(p, 'Pin'));}" + "for(let a=1;a<=8;a++){const l=f(`PIR ${i}:Action ${a}`);if(l){count++; g.appendChild(w(l, `Link Action ${a}`));}}" + "}" + + // AGGRESSIVE CLEANUP + "const root = document.getElementById('pir-root');" + "if(root){" + "const children = Array.from(root.children);" + "children.forEach(c => {" + "if(c.tagName!=='H3' && c.tagName!=='BUTTON' && !c.classList.contains('um-g')){ c.remove(); }" + "});" + "let n = root.firstChild; while(n){ let next=n.nextSibling; if(n.nodeType===3){ n.remove(); } n=next; }" + "let prev = root.previousElementSibling; if(prev && prev.tagName==='HR') prev.remove();" + "}" + + "} catch(e) {" + "if(st){st.innerHTML='Error: '+e.message; st.style.display='block'; st.style.color='red';}" + "}" + "} setTimeout(aUM,1000);")); + } + + /* + * getId() allows you to optionally give your V2 usermod an unique ID (please define it in const.h!). + */ + uint16_t getId() override { return USERMOD_ID_PIRSWITCH; } +}; + +// Register instance +static MotionDetectionUsermod motionDetectionUsermod; +REGISTER_USERMOD(motionDetectionUsermod); + diff --git a/usermods/PIR_sensor_switch_v2/Usermod Info Page (3x PIRs, 3x Actions example).jpg b/usermods/PIR_sensor_switch_v2/Usermod Info Page (3x PIRs, 3x Actions example).jpg new file mode 100644 index 0000000000..0ddd967b0f Binary files /dev/null and b/usermods/PIR_sensor_switch_v2/Usermod Info Page (3x PIRs, 3x Actions example).jpg differ diff --git a/usermods/PIR_sensor_switch_v2/Usermod Settings Page (3x PIRs, 3x Actions example).jpg b/usermods/PIR_sensor_switch_v2/Usermod Settings Page (3x PIRs, 3x Actions example).jpg new file mode 100644 index 0000000000..e06f24885b Binary files /dev/null and b/usermods/PIR_sensor_switch_v2/Usermod Settings Page (3x PIRs, 3x Actions example).jpg differ diff --git a/usermods/PIR_sensor_switch_v2/library.json b/usermods/PIR_sensor_switch_v2/library.json new file mode 100644 index 0000000000..a848bba2f0 --- /dev/null +++ b/usermods/PIR_sensor_switch_v2/library.json @@ -0,0 +1,6 @@ +{ + "name": "PIR_sensor_switch_v2", + "build": { + "libArchive": false + } +} \ No newline at end of file diff --git a/usermods/PIR_sensor_switch_v2/readme.md b/usermods/PIR_sensor_switch_v2/readme.md new file mode 100644 index 0000000000..5bc7b4b21a --- /dev/null +++ b/usermods/PIR_sensor_switch_v2/readme.md @@ -0,0 +1,74 @@ +# PIR Sensor Switch v2 + +## Rawframe + +## Background + +Back in the day, I added PIR functionality to WLED before it became a built-in feature. Since then, a PIR usermod was added (credit to @gegu & @blazoncek! for PIR_sensor_switch). + +However, I needed something more flexible for my setup - multiple PIRs triggering different actions in various combinations. The official version works great for simple cases, but when you need complex linking between sensors and actions, macro chains get messy, and moreover we still cannot easily have seamless fx (i.e avoid restarting an fx animation when called again), So I built this. + +## Screenshots + +![alt text]() +![alt text]() + +## What It Does + +### Multiple PIRs +- Support for multiple PIR sensors (configurable up to 8) +- Each PIR can be independently enabled/disabled +- Configure different GPIO pins for each sensor +- Real-time status monitoring in the Web UI + +### Flexible Actions +- Define multiple "Actions" (also configurable up to 8) +- Each Action has: + - **On Preset** - triggers when motion detected + - **Off Preset** - triggers when motion stops (with delay) + - **Off Delay** - how long to wait before triggering the off preset + - **Enable/Disable toggle** - turn actions on/off without losing settings + +### Smart Linking +- Link any PIR to any Action (many-to-many relationships) +- One PIR can trigger multiple Actions +- One Action can be triggered by multiple PIRs +- Actions stay active as long as ANY linked PIR detects motion +- Off timers only start when ALL linked PIRs go idle + +### Web UI +- Toggle PIRs and Actions on/off directly from the Info page +- See motion status in real-time (● motion / ○ idle) +- View per Action countdown timers for off delays +- Config page for full setup (pins, presets, delays, linking) + +### API Control +- Enable/disable PIRs via JSON API: `{"MotionDetection":{"pir0":true}}` +- Enable/disable Actions via JSON API: `{"MotionDetection":{"action0":true}}` + +## Use Cases + +This helps if you need: +- Multiple PIRs in different rooms/zones +- Different lighting presets for different sensors +- Combined motion detection (e.g., "turn off only when ALL sensors are idle") +- Independent motion zones that don't interfere with each other +- Complex automation without macro chains + +## Technical Bits + +- Uses a preset FIFO queue to prevent flooding (on testing I noticed if presets were called at the same time one would usually be skipped). +- Optimized RAM usage with bitmasks instead of 2D arrays, but you could change this if you need more than 8 PIRs or Actions. +- PIR to Action contributor tracking ensures Actions behave correctly with multiple PIRs + +## Notes + +- To add more PIRs or Actions, just change the defines in the cpp file (defaults) or the platformio.ini / override + - PIR_SENSOR_MAX + - ACTION_MAX + +- If anyone wants to add the other options from the @gegu & @blazoncek usermod then feel free to do so but remember to adjust the custom webpage formatting accordingly. + +Hope it's useful :) + + diff --git a/usermods/PIR_sensor_switch_v2/readme_old.md b/usermods/PIR_sensor_switch_v2/readme_old.md new file mode 100644 index 0000000000..71b05614a9 --- /dev/null +++ b/usermods/PIR_sensor_switch_v2/readme_old.md @@ -0,0 +1,110 @@ +# PIR sensor switch + +This usermod-v2 modification allows the connection of a PIR sensor to switch on the LED strip when motion is detected. The switch-off occurs ten minutes after no more motion is detected. + +_Story:_ + +I use the PIR Sensor to automatically turn on the WLED analog clock in my home office room when I am there. +The LED strip is switched [using a relay](https://kno.wled.ge/features/relay-control/) to keep the power consumption low when it is switched off. + +## Web interface + +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 or HC-SR602 sensor, an 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 + +**NOTE:** Usermod has been included in master branch of WLED so it can be compiled in directly just by defining `-D USERMOD_PIRSWITCH` and optionally `-D PIR_SENSOR_PIN=16` to override default pin. You can also change the default off time by adding `-D PIR_SENSOR_OFF_SEC=30`. + +## 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. + +When the PIR sensor state changes an MQTT message is broadcasted with topic `wled/deviceMAC/motion` and message `on` or `off`. +Usermod can also be configured to send just the MQTT message but not change WLED state using settings page as well as responding to motion only at night +(assuming NTP and latitude/longitude are set to determine sunrise/sunset times). + +### There are two options to get access to the usermod instance + +_1._ Include `usermod_PIR_sensor_switch.h` **before** you include other usermods in `usermods_list.cpp' + +or + +_2._ Use `#include "usermod_PIR_sensor_switch.h"` at the top of the `usermod.h` where you need it. + +**Example usermod.h :** + +```cpp +#include "wled.h" + +#include "usermod_PIR_sensor_switch.h" + +class MyUsermod : public Usermod { + //... + + void togglePIRSensor() { + #ifdef USERMOD_PIR_SENSOR_SWITCH + PIRsensorSwitch *PIRsensor = (PIRsensorSwitch::*) UsermodManager::lookup(USERMOD_ID_PIRSWITCH); + if (PIRsensor != nullptr) { + PIRsensor->EnablePIRsensor(!PIRsensor->PIRsensorEnabled()); + } + #endif + } + //... +}; +``` + +### Configuration options + +Usermod can be configured via the 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` - send only MQTT messages, do not interact with WLED +* `off-only` - only trigger presets or turn WLED on/off if WLED is not already on (displaying effect) +* `notifications` - enable or disable sending notifications to other WLED instances using Sync button +* `HA-discovery` - enable automatic discovery in Home Assistant +* `override` - override PIR input when WLED state is changed using UI +* `domoticz-idx` - Domoticz virtual switch ID (used with MQTT `domoticz/in`) + +Have fun - @gegu & @blazoncek + +## Change log + +2021-04 + +* Adaptation for runtime configuration. + +2021-11 + +* Added information about dynamic configuration options +* Added option to temporary enable/disable usermod from WLED UI (Info dialog) + +2022-11 + +* Added compile time option for off timer. +* Added Home Assistant autodiscovery MQTT broadcast. +* Updated info on compiling. + +2023-?? + +* Override option +* Domoticz virtual switch ID (used with MQTT `domoticz/in`) + +2024-02 + +* Added compile time option to expand number of PIR sensors (they are logically ORed) `-D PIR_SENSOR_MAX_SENSORS=3` +