# 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 Servo Hardware Configuration
loco_iot.enable(msg.SUBTYPE_SERVO)
loco_iot.enable(msg.SUBTYPE_SW_1)
loco_iot.enable(msg.SUBTYPE_SW_2)
# Start Listening for Control/Request Messages
loco_iot.start()
servo_position = 0
# Infinite Loop: Press cancel to end the loop.
while True:
# Request Button State Data
btn_dict1 = loco_iot.getData(msg.SUBTYPE_SW_1)
btn_dict2 = loco_iot.getData(msg.SUBTYPE_SW_2)
if (btn_dict1 == {'Button 1':0}):
servo_position = servo_position + 10
time.sleep(1)
print('button 1')
elif (btn_dict2 == {'Button 2':0}):
servo_position = servo_position - 10
time.sleep(1)
print('button 2')
elif servo_position < 0:
loco_iot.setData(msg.SUBTYPE_SERVO, [0])
time.sleep(1)
elif servo_position > 180:
loco_iot.setData(msg.SUBTYPE_SERVO, [180])
time.sleep(1)
else:
loco_iot.setData(msg.SUBTYPE_SERVO, [servo_position])
print("restart")
time.sleep(1)
# Arbitrary Delay
time.sleep(1)
# Close Connection to the Microcontroller
loco_iot.close()