From 3b537d3fac7972651581d4c2ce38041c7c241115 Mon Sep 17 00:00:00 2001 From: jerick Date: Tue, 27 Dec 2022 14:10:14 -0500 Subject: [PATCH] Fixed Motion to update frequently --- mqtt/motionSensor.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/mqtt/motionSensor.py b/mqtt/motionSensor.py index 555d115..ba6e894 100644 --- a/mqtt/motionSensor.py +++ b/mqtt/motionSensor.py @@ -49,12 +49,14 @@ def getValues(pir_sensor): current_state = 0 try: while True: - time.sleep(0.1) + time.sleep(1) current_state = GPIO.input(pir_sensor) #Condition if motion is detected if current_state == 1: print("GPIO pin %s is %s" % (pir_sensor, current_state)) return current_state + else: + return current_state except KeyboardInterrupt: pass finally: @@ -75,16 +77,14 @@ def publish(client, current_state): else: print(f"Failed to send message to topic {motionTopic}") - - - + #Run functions +client = connect_mqtt() # Connect to Broker +client.loop_start() #Start loop while True: - #Run functions - client = connect_mqtt() # Connect to Broker - client.loop_start() #Start loop current_state = getValues(pir_sensor) publish(client, current_state) - client.loop_stop() #Stop loop - client.disconnect() # Disconnect - GPIO.cleanup() - time.sleep(5) +# print(current_state) + +client.loop_stop() #Stop loop +client.disconnect() # Disconnect +# GPIO.cleanup()