Fix resetting the timer also when the status hasn't changed
This commit is contained in:
13
src/main.cpp
13
src/main.cpp
@@ -111,6 +111,13 @@ void loop() {
|
||||
// This out pin triggering is dependent not only on movement, but also on light levels which can be configured in the app.
|
||||
// I set it to 255 and "below" - this triggered it for me.....
|
||||
//send_mqtt("occupancy_state_debug", currentState);
|
||||
|
||||
// When occupancy is detected, reset the timer
|
||||
if (currentState == HIGH) {
|
||||
previousMotionMillis=millis();
|
||||
}
|
||||
|
||||
// Send messages only on status change, we don't want to flood the network...
|
||||
if (currentState != previousState) {
|
||||
Serial.print("Status changed; currentState: ");
|
||||
Serial.print(currentState);
|
||||
@@ -119,14 +126,14 @@ void loop() {
|
||||
if (currentState == HIGH) {
|
||||
send_mqtt("motion");
|
||||
digitalWrite(LED_BUILTIN,HIGH);
|
||||
previousMotionMillis=millis();
|
||||
} else {
|
||||
send_mqtt("no_motion_ignore");
|
||||
send_mqtt("no_motion_immediate");
|
||||
digitalWrite(LED_BUILTIN,LOW);
|
||||
}
|
||||
previousState = currentState;
|
||||
}
|
||||
|
||||
// Send no_motion messages every minute, indicating how long we didn't see occupancy
|
||||
const int secondsPerMinute=60;
|
||||
long minutesSinceLastMotion=(millis()-previousMotionMillis)/(millisPerSecond*secondsPerMinute);
|
||||
if ((previousNoMoNotifyMinutes != minutesSinceLastMotion) && (0 != minutesSinceLastMotion)) {
|
||||
@@ -134,5 +141,5 @@ void loop() {
|
||||
previousNoMoNotifyMinutes = minutesSinceLastMotion;
|
||||
}
|
||||
|
||||
delay(100);
|
||||
delay(50);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user