Proximity Sensor
# Module Import
import LocoIOT
import time
# Class Instances Creation
loco_iot = LocoIOT.LocoIOT()
msg = LocoIOT.IOT_Codes()
# Initiate Connection to the Microcontroller
loco_iot.connect()
# Enable PIR Sensor Hardware Configuration
loco_iot.enable(msg.SUBTYPE_PIR)
loco_iot.enable(msg.SUBTYPE_DO_1)
loco_iot.enable(msg.SUBTYPE_DO_2)
loco_iot.enable(msg.SUBTYPE_DO_3)
# Start Listening for Control/Request Messages
loco_iot.start()
# State Flag Set To Searching = 0
pir_state = 0
# Searching State - Turn ON The Red LED
loco_iot.setData(msg.SUBTYPE_DO_1, [1])
# Loop For a 5 Minute Duration
start_time = time.time()
while (time.time() - start_time) < 300:
# Request PIR Data
pir_dict = loco_iot.getData(msg.SUBTYPE_PIR)
# Print PIR Dictionary
print(pir_dict)
# Check For The Detected State
if ((pir_dict["PIR State"] == 1) and (pir_state == 0)):
loco_iot.setData(msg.SUBTYPE_DO_1, [0])
loco_iot.setData(msg.SUBTYPE_DO_2, [1])
#State Flag Set to Detected = 1
pir_state = 1
elif ((pir_dict["PIR State"] == 0) and (pir_state == 1)):
loco_iot.setData(msg.SUBTYPE_DO_2, [0])
loco_iot.setData(msg.SUBTYPE_DO_3, [1])
time.sleep(3)
#Reset state to Searching
loco_iot.setData(msg.SUBTYPE_DO_3, [0])
loco_iot.setData(msg.SUBTYPE_DO_1, [1])
pir_state = 0
# Print PIR Dictionary Key Value
#print("PIR Detection State:", pir_dict['PIR State'])
# Short Arbitrary Delay
time.sleep(0.1)
# Close Connection to the Microcontroller
loco_iot.close()