print(command["payload"]) # Listen for commands. device.add_event_observer("command", on_command) # Connect to Losant. device.connect(blocking=False) '''**************************************************************************************** While True loop to run for eternity.. or Answer = 42, you decide ****************************************************************************************''' # Send values once every second. while True: device.loop() if device.is_connected(): # Read Lid state to check if its open LidState = GPIO.input(LIDCOVER) # Ultrasonic Sensor Read only if Lid is closed if LidState == 0: GPIO.output(TRIG, False) print "Waiting For Ultrasonic Sensor" time.sleep(2) GPIO.output(TRIG, True) time.sleep(0.00001) GPIO.output(TRIG, False)
def setColor(deviceId, color): print(deviceId) color_to_set = colors[color] for index, pin in enumerate(losantconfig.LED_PINS[deviceId]): print('setting pin', pin, 'to', index) GPIO.output(pin, color_to_set[index]) # set both lights to red to begin setColor(losantconfig.MY_DEVICE_ID, 'red') setColor(losantconfig.OTHER_DEVICE_ID, 'red') try: while True: # keep doing this forever and ever device.loop() # keeps MQTT connection alive ### Key State ### key_state = not GPIO.input( losantconfig.KEY_PIN ) # False is when the key is turned, so we're flipping it for sanity's sake # state changed to turned if key_state == True and is_key_turned == 0: print('Key Turned') is_key_turned = 1 if device.is_connected(): # send the key's state via MQTT device.send_state({"isKeyTurned": is_key_turned}) # state changed to unturned
def setColor(deviceId, color): print(deviceId) color_to_set = colors[color] for index, pin in enumerate(losantconfig.LED_PINS[deviceId]): print('setting pin',pin,'to',index) GPIO.output(pin, color_to_set[index]) # set both lights to red to begin setColor(losantconfig.MY_DEVICE_ID, 'red') setColor(losantconfig.OTHER_DEVICE_ID,'red') try: while True: # keep doing this forever and ever device.loop() # keeps MQTT connection alive ### Key State ### key_state = not GPIO.input(losantconfig.KEY_PIN) # False is when the key is turned, so we're flipping it for sanity's sake # state changed to turned if key_state == True and is_key_turned == 0: print('Key Turned') is_key_turned = 1 if device.is_connected(): # send the key's state via MQTT device.send_state({ "isKeyTurned": is_key_turned}) # state changed to unturned if key_state == False and is_key_turned == 1: print('Key Released')
SOFTWARE. """ import time import random from losantmqtt import Device # Construct device device = Device("my-device-id", "my-app-access-key", "my-app-access-secret") def on_command(my_device, command): print("Command received.") print(command["name"]) print(command["payload"]) # Listen for commands. device.add_event_observer("command", on_command) # Connect to Losant. device.connect(blocking=False) # Send temperature once every second. while True: device.loop() if device.is_connected(): # Call out to your sensor here temp = random.random() * 100 device.send_state({"temperature": temp}) time.sleep(1)