From 5309dd7d8c450c8137379c8e54afde971e62ea50 Mon Sep 17 00:00:00 2001 From: Fisch Date: Thu, 1 Oct 2020 22:19:11 +0200 Subject: [PATCH] add dynamic send for pir sensor --- src/main.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 820c10e..fd40b26 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,7 +14,7 @@ float humidity; //[%RH] DHT float temperature; //[deg C] DHT float light; //[Lux] BH1750 - bool movement //true bei pir output hight, false wenn low HC12? + bool movement //true bei pir output hight, false wenn low HC12?501? */ @@ -33,6 +33,8 @@ unsigned long lastsensorreadtime=0; unsigned long sensorupdatedelay=60000; //delay for reading and transmitting unsigned long lastPIRtime=0; unsigned long PIRdelay=100; //polling delay +unsigned long lastPIRSendValueTime=0; +unsigned long PIRMaxDelay=1000*60*10; //maximum time until current value is send bool motion=false; @@ -92,23 +94,36 @@ void loop() { void loopHandler() { - if (millis() >= (lastPIRtime+PIRdelay)){ - + if (millis() >= (lastPIRtime+PIRdelay)){ if (digitalRead(PIRPIN)){ if (!motion) { //changed? Homie.getLogger() << "motion " << ": " << "true" << endl; sensorNode.setProperty("motion").send(String("true")); } motion=true; + lastPIRSendValueTime=millis(); }else{ if (motion) { //changed? Homie.getLogger() << "motion " << ": " << "false" << endl; sensorNode.setProperty("motion").send(String("false")); } motion=false; + lastPIRSendValueTime=millis(); } lastPIRtime=millis(); } + if (millis() >= (lastPIRSendValueTime+PIRMaxDelay)) { //send current value after some long time + if (digitalRead(PIRPIN)){ + Homie.getLogger() << "motion resend " << ": " << "true" << endl; + sensorNode.setProperty("motion").send(String("true")); + motion=true; + }else{ + Homie.getLogger() << "motion resend " << ": " << "false" << endl; + sensorNode.setProperty("motion").send(String("false")); + motion=false; + } + lastPIRSendValueTime=millis(); + } if (millis() >= (lastsensorreadtime+sensorupdatedelay)) {