iv123 lin / Mbed OS connected-lights

Dependencies:   ChainableLED

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lights.py Source File

lights.py

00001 import mbed_connector_api
00002 import time
00003 import base64
00004 import os
00005 
00006 TOKEN = "YOUR_ACCESS_TOKEN"
00007 
00008 connector = mbed_connector_api.connector(os.environ['TOKEN'] or TOKEN)
00009 
00010 def notificationHandler(data):
00011     for n in data['notifications']:
00012         print "Got a notification for %s: %s -> new value %s" % (n['ep'], n['path'], base64.b64decode(n['payload']))
00013 
00014 connector.startLongPolling()
00015 connector.setHandler('notifications', notificationHandler)
00016 
00017 e = connector.getEndpoints("light-system")
00018 while not e.isDone():
00019     None
00020 if e.error:
00021     raise(e.error.error)
00022 print("Found %d lights: %s" % (len(e.result), str(e.result)))
00023 
00024 for endpoint in e.result:
00025     # Get a notification whenever the PIR count changes
00026     connector.putResourceSubscription(endpoint['name'], "/pir/0/count")
00027 
00028     # And change the color to pink, because that's nice
00029     pink = 0xff69b4
00030     x = connector.putResourceValue(endpoint['name'], "/led/0/color", pink)
00031     while not x.isDone():
00032         None
00033     if (x.error):
00034         print("Setting pink color for %s failed: %s" % (endpoint['name'], x.error.error))
00035     else:
00036         print("Set color of %s to pink!" % endpoint['name'])
00037 
00038 while 1:
00039     time.sleep(1.0)