add dynamic send for pir sensor

This commit is contained in:
interfisch 2020-10-01 22:19:11 +02:00
parent 5f2e49f7a2
commit 5309dd7d8c
1 changed files with 18 additions and 3 deletions

View File

@ -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))
{