From e0c0f29fc6c1f9c8762f32f642835628f701d569 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Mon, 10 May 2021 22:41:27 +0200 Subject: [PATCH] Added nigttime only and MQTT only options to PIR sensor switch usermod. Clarified empty UM settings a bit. --- .../usermod_PIR_sensor_switch.h | 65 +++++++++++++++++-- wled00/data/settings_um.htm | 4 +- wled00/html_settings.h | 2 +- wled00/wled.h | 2 +- 4 files changed, 64 insertions(+), 9 deletions(-) diff --git a/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h b/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h index d76226efc..6a37f475b 100644 --- a/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h +++ b/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h @@ -72,6 +72,10 @@ private: // 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; unsigned long lastLoop = 0; @@ -81,6 +85,8 @@ private: static const char _enabled[]; static const char _onPreset[]; static const char _offPreset[]; + static const char _nightTime[]; + static const char _mqttOnly[]; /** * return or change if new PIR sensor state is available @@ -92,6 +98,31 @@ private: */ static void IRAM_ATTR ISR_PIRstateChange(); + /** + * 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==0 || sunset==0) return false; + + if (hour(sunrise)>hr && hour(sunset)mi) { + isDayTime = true; + } + } + return isDayTime; + } + /** * switch strip on/off */ @@ -133,7 +164,7 @@ private: if (m_PIRsensorPinState == HIGH) { m_offTimerStart = 0; - switchStrip(true); + if (!m_mqttOnly && (!m_nightTimeOnly || (m_nightTimeOnly && !isDayTime()))) switchStrip(true); publishMqtt("on"); } else /*if (bri != 0)*/ { // start switch off timer @@ -154,7 +185,7 @@ private: { if (m_PIRenabled == true) { - switchStrip(false); + if (!m_mqttOnly && (!m_nightTimeOnly || (m_nightTimeOnly && !isDayTime()))) switchStrip(false); publishMqtt("off"); } m_offTimerStart = 0; @@ -288,11 +319,13 @@ public: void addToConfig(JsonObject &root) { JsonObject top = root.createNestedObject(FPSTR(_name)); - top[FPSTR(_enabled)] = m_PIRenabled; + top[FPSTR(_enabled)] = m_PIRenabled; top[FPSTR(_switchOffDelay)] = m_switchOffDelay / 1000; - top["pin"] = PIRsensorPin; - top[FPSTR(_onPreset)] = m_onPreset; + top["pin"] = PIRsensorPin; + top[FPSTR(_onPreset)] = m_onPreset; top[FPSTR(_offPreset)] = m_offPreset; + top[FPSTR(_nightTime)] = m_nightTimeOnly; + top[FPSTR(_mqttOnly)] = m_mqttOnly; DEBUG_PRINTLN(F("PIR config saved.")); } @@ -334,6 +367,26 @@ public: m_offPreset = max(0,min(250,top[FPSTR(_offPreset)].as())); } + if (top[FPSTR(_nightTime)] != nullptr) { + if (top[FPSTR(_nightTime)].is()) { + m_nightTimeOnly = top[FPSTR(_nightTime)].as(); // reading from cfg.json + } else { + // change from settings page + String str = top[FPSTR(_nightTime)]; // checkbox -> off or on + m_nightTimeOnly = (bool)(str!="off"); // off is guaranteed to be present + } + } + + if (top[FPSTR(_mqttOnly)] != nullptr) { + if (top[FPSTR(_mqttOnly)].is()) { + m_mqttOnly = top[FPSTR(_mqttOnly)].as(); // reading from cfg.json + } else { + // change from settings page + String str = top[FPSTR(_mqttOnly)]; // checkbox -> off or on + m_mqttOnly = (bool)(str!="off"); // off is guaranteed to be present + } + } + if (!initDone) { // reading config prior to setup() DEBUG_PRINTLN(F("PIR config loaded.")); @@ -396,3 +449,5 @@ const char PIRsensorSwitch::_enabled[] PROGMEM = "PIRenabled"; const char PIRsensorSwitch::_switchOffDelay[] PROGMEM = "PIRoffSec"; const char PIRsensorSwitch::_onPreset[] PROGMEM = "on-preset"; const char PIRsensorSwitch::_offPreset[] PROGMEM = "off-preset"; +const char PIRsensorSwitch::_nightTime[] PROGMEM = "nighttime-only"; +const char PIRsensorSwitch::_mqttOnly[] PROGMEM = "mqtt-only"; diff --git a/wled00/data/settings_um.htm b/wled00/data/settings_um.htm index 3c8ec7d63..d9b02c90f 100644 --- a/wled00/data/settings_um.htm +++ b/wled00/data/settings_um.htm @@ -100,9 +100,9 @@ addField(k,'unknown',o); } if (urows==="") - urows = "No Usermods configuration found.
Press Save to initialize defaults."; + urows = "No Usermods configuration found.
Press Save to initialize defaults if usermods were compiled in."; } else { - urows = "Usermods configuration not found.
Most likely no Usermods exist.
Press Save to initialize defaults."; + urows = "Usermods configuration not found.
Most likely no Usermods exist.
Press Save to try to initialize defaults."; } gId("um").innerHTML = urows; }) diff --git a/wled00/html_settings.h b/wled00/html_settings.h index 113ef7583..41c4f9c60 100644 --- a/wled00/html_settings.h +++ b/wled00/html_settings.h @@ -395,7 +395,7 @@ type="submit">Save & Reboot)====="; // Autogenerated from wled00/data/settings_um.htm, do not edit!! const char PAGE_settings_um[] PROGMEM = R"=====(UI Settings%CSS%%SCSS%