iv123 lin
/
connected-lights
sample_pir-lights_rgb
example-python/lights.py@0:7a352727249b, 2017-06-18 (annotated)
- Committer:
- iv123
- Date:
- Sun Jun 18 10:14:56 2017 +0000
- Revision:
- 0:7a352727249b
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
iv123 | 0:7a352727249b | 1 | import mbed_connector_api |
iv123 | 0:7a352727249b | 2 | import time |
iv123 | 0:7a352727249b | 3 | import base64 |
iv123 | 0:7a352727249b | 4 | import os |
iv123 | 0:7a352727249b | 5 | |
iv123 | 0:7a352727249b | 6 | TOKEN = "YOUR_ACCESS_TOKEN" |
iv123 | 0:7a352727249b | 7 | |
iv123 | 0:7a352727249b | 8 | connector = mbed_connector_api.connector(os.environ['TOKEN'] or TOKEN) |
iv123 | 0:7a352727249b | 9 | |
iv123 | 0:7a352727249b | 10 | def notificationHandler(data): |
iv123 | 0:7a352727249b | 11 | for n in data['notifications']: |
iv123 | 0:7a352727249b | 12 | print "Got a notification for %s: %s -> new value %s" % (n['ep'], n['path'], base64.b64decode(n['payload'])) |
iv123 | 0:7a352727249b | 13 | |
iv123 | 0:7a352727249b | 14 | connector.startLongPolling() |
iv123 | 0:7a352727249b | 15 | connector.setHandler('notifications', notificationHandler) |
iv123 | 0:7a352727249b | 16 | |
iv123 | 0:7a352727249b | 17 | e = connector.getEndpoints("light-system") |
iv123 | 0:7a352727249b | 18 | while not e.isDone(): |
iv123 | 0:7a352727249b | 19 | None |
iv123 | 0:7a352727249b | 20 | if e.error: |
iv123 | 0:7a352727249b | 21 | raise(e.error.error) |
iv123 | 0:7a352727249b | 22 | print("Found %d lights: %s" % (len(e.result), str(e.result))) |
iv123 | 0:7a352727249b | 23 | |
iv123 | 0:7a352727249b | 24 | for endpoint in e.result: |
iv123 | 0:7a352727249b | 25 | # Get a notification whenever the PIR count changes |
iv123 | 0:7a352727249b | 26 | connector.putResourceSubscription(endpoint['name'], "/pir/0/count") |
iv123 | 0:7a352727249b | 27 | |
iv123 | 0:7a352727249b | 28 | # And change the color to pink, because that's nice |
iv123 | 0:7a352727249b | 29 | pink = 0xff69b4 |
iv123 | 0:7a352727249b | 30 | x = connector.putResourceValue(endpoint['name'], "/led/0/color", pink) |
iv123 | 0:7a352727249b | 31 | while not x.isDone(): |
iv123 | 0:7a352727249b | 32 | None |
iv123 | 0:7a352727249b | 33 | if (x.error): |
iv123 | 0:7a352727249b | 34 | print("Setting pink color for %s failed: %s" % (endpoint['name'], x.error.error)) |
iv123 | 0:7a352727249b | 35 | else: |
iv123 | 0:7a352727249b | 36 | print("Set color of %s to pink!" % endpoint['name']) |
iv123 | 0:7a352727249b | 37 | |
iv123 | 0:7a352727249b | 38 | while 1: |
iv123 | 0:7a352727249b | 39 | time.sleep(1.0) |