Fixed Motion to update frequently

This commit is contained in:
2022-12-27 14:10:14 -05:00
parent dfd9635559
commit 3b537d3fac

View File

@@ -49,12 +49,14 @@ def getValues(pir_sensor):
current_state = 0 current_state = 0
try: try:
while True: while True:
time.sleep(0.1) time.sleep(1)
current_state = GPIO.input(pir_sensor) current_state = GPIO.input(pir_sensor)
#Condition if motion is detected #Condition if motion is detected
if current_state == 1: if current_state == 1:
print("GPIO pin %s is %s" % (pir_sensor, current_state)) print("GPIO pin %s is %s" % (pir_sensor, current_state))
return current_state return current_state
else:
return current_state
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
finally: finally:
@@ -75,16 +77,14 @@ def publish(client, current_state):
else: else:
print(f"Failed to send message to topic {motionTopic}") print(f"Failed to send message to topic {motionTopic}")
#Run functions
client = connect_mqtt() # Connect to Broker
client.loop_start() #Start loop
while True: while True:
#Run functions
client = connect_mqtt() # Connect to Broker
client.loop_start() #Start loop
current_state = getValues(pir_sensor) current_state = getValues(pir_sensor)
publish(client, current_state) publish(client, current_state)
client.loop_stop() #Stop loop # print(current_state)
client.disconnect() # Disconnect
GPIO.cleanup() client.loop_stop() #Stop loop
time.sleep(5) client.disconnect() # Disconnect
# GPIO.cleanup()