Fixed Motion to update frequently
This commit is contained in:
@@ -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}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
while True:
|
|
||||||
#Run functions
|
#Run functions
|
||||||
client = connect_mqtt() # Connect to Broker
|
client = connect_mqtt() # Connect to Broker
|
||||||
client.loop_start() #Start loop
|
client.loop_start() #Start loop
|
||||||
|
while True:
|
||||||
current_state = getValues(pir_sensor)
|
current_state = getValues(pir_sensor)
|
||||||
publish(client, current_state)
|
publish(client, current_state)
|
||||||
|
# print(current_state)
|
||||||
|
|
||||||
client.loop_stop() #Stop loop
|
client.loop_stop() #Stop loop
|
||||||
client.disconnect() # Disconnect
|
client.disconnect() # Disconnect
|
||||||
GPIO.cleanup()
|
# GPIO.cleanup()
|
||||||
time.sleep(5)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user