Browse Source

add dynamic send for pir sensor

master
interfisch 2 years ago
parent
commit
5309dd7d8c
  1. 21
      src/main.cpp

21
src/main.cpp

@ -14,7 +14,7 @@ @@ -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; @@ -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() { @@ -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))
{

Loading…
Cancel
Save