Fixed Motion to update frequently
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user