iv123 lin
/
connected-lights
sample_pir-lights_rgb
docs/5_controlling-from-cloud.md@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 | # Controlling the device from mbed Device Connector |
iv123 | 0:7a352727249b | 2 | |
iv123 | 0:7a352727249b | 3 | At this moment the device is connected through mbed Device Connector. In the code sample that we gave in the previous section, we defined a number of resources using calls to `client.define_resource()`. These resources are automatically exposed to mbed Device Connector, from where you can read and write resources. Then, changes are automatically synced back to the device. That means that we already have a remote management interface for this device. |
iv123 | 0:7a352727249b | 4 | |
iv123 | 0:7a352727249b | 5 | ## Seeing the status of a device |
iv123 | 0:7a352727249b | 6 | |
iv123 | 0:7a352727249b | 7 | Each device that you connect to mbed Device Connector has an endpoint name. This is a GUID (in the form of 8874e0eb-96ef-4799-b948-e91d05147bfe), which is the unique identifier of your device. If you don't know the endpoint name of your device, check the ``security.h`` file in the online compiler. |
iv123 | 0:7a352727249b | 8 | |
iv123 | 0:7a352727249b | 9 | We need the know the endpoint's name to check the device's status in the mbed Device Connector's online interface. The [endpoint](https://connector.mbed.com/#endpoints) page lists all devices associated with your account, with their current status. |
iv123 | 0:7a352727249b | 10 | |
iv123 | 0:7a352727249b | 11 | <span class="tips">**Tip:** The mbed Device Connector interface lists your devices by type. You can categorize devices by setting the device type in the application running on the device. See `options.DeviceType = "light-system";` in ``main.cpp``.</span> |
iv123 | 0:7a352727249b | 12 | |
iv123 | 0:7a352727249b | 13 | <span class="images">![Two connected devices](assets/lights11.png)<span>The mbed Device Connector Connected Devices page, showing two connected devices: our light-system and another device.</span></span> |
iv123 | 0:7a352727249b | 14 | |
iv123 | 0:7a352727249b | 15 | |
iv123 | 0:7a352727249b | 16 | ## Controlling the device |
iv123 | 0:7a352727249b | 17 | |
iv123 | 0:7a352727249b | 18 | We created four resources before (see ``main.cpp``): |
iv123 | 0:7a352727249b | 19 | |
iv123 | 0:7a352727249b | 20 | * `led/0/color` - the color of the LED, encoded as three bytes. |
iv123 | 0:7a352727249b | 21 | * `led/0/timeout` - the timeout (in seconds) after detection; lights are disabled when this period ends. |
iv123 | 0:7a352727249b | 22 | * `led/0/permanent_status` - whether we should have the lights permanently on (status 1) or off (status 2), or just let the PIR sensor figure it out (status 0). |
iv123 | 0:7a352727249b | 23 | * `pir/0/count` - the number of times the PIR sensor was triggered. Read only, and should show notifications. |
iv123 | 0:7a352727249b | 24 | |
iv123 | 0:7a352727249b | 25 | These resources can be controlled through the mbed Device Connector web interface. For instance, when we write the value `1` to `led/0/permanent_status` the lights will stay on indefinitely. |
iv123 | 0:7a352727249b | 26 | |
iv123 | 0:7a352727249b | 27 | ### Turning the lights on |
iv123 | 0:7a352727249b | 28 | |
iv123 | 0:7a352727249b | 29 | To test this out, in mbed Device Connector go to *Device Connector* > *API Console*, and select *Endpoint directory lookups*. This gives you access to a management console where you can quickly test out interactions with resources. |
iv123 | 0:7a352727249b | 30 | |
iv123 | 0:7a352727249b | 31 | To enable the lights: |
iv123 | 0:7a352727249b | 32 | |
iv123 | 0:7a352727249b | 33 | 1. Choose *PUT*. |
iv123 | 0:7a352727249b | 34 | 1. Select your endpoint and the resource path `/led/0/permanent_status`. |
iv123 | 0:7a352727249b | 35 | |
iv123 | 0:7a352727249b | 36 | <span class="images">![Preparing a PUT request to a resource in mbed Device Connector](assets/lights12.png)</span> |
iv123 | 0:7a352727249b | 37 | |
iv123 | 0:7a352727249b | 38 | 1. Switch to the *PUT data* tab and enter `1`. |
iv123 | 0:7a352727249b | 39 | 1. Click the *TEST API* button. |
iv123 | 0:7a352727249b | 40 | |
iv123 | 0:7a352727249b | 41 | |
iv123 | 0:7a352727249b | 42 | <span class="images">![Setting PUT data for a request in mbed Device Connector](assets/lights13.png)</span> |
iv123 | 0:7a352727249b | 43 | |
iv123 | 0:7a352727249b | 44 | Now your lights will stay on until you change the status of `permanent_status` to 0 (listen to PIR sensor) or 2 (always off). |
iv123 | 0:7a352727249b | 45 | |
iv123 | 0:7a352727249b | 46 | ### Setting the color |
iv123 | 0:7a352727249b | 47 | |
iv123 | 0:7a352727249b | 48 | We can control the color of the lights the same way. The color is encoded in an integer where we store three channels: red, green and blue. Each of the channels can have a value between 0 (off) and 255 (completely on). |
iv123 | 0:7a352727249b | 49 | |
iv123 | 0:7a352727249b | 50 | To encode the value of a color: |
iv123 | 0:7a352727249b | 51 | |
iv123 | 0:7a352727249b | 52 | ```js |
iv123 | 0:7a352727249b | 53 | red = 0; |
iv123 | 0:7a352727249b | 54 | green = 255; |
iv123 | 0:7a352727249b | 55 | blue = 255; |
iv123 | 0:7a352727249b | 56 | |
iv123 | 0:7a352727249b | 57 | // alternatively: encode the color as a hex value, via encoded = 0x00ffff |
iv123 | 0:7a352727249b | 58 | |
iv123 | 0:7a352727249b | 59 | encoded = (red << 16) + (green << 8) + blue; |
iv123 | 0:7a352727249b | 60 | // 65380 |
iv123 | 0:7a352727249b | 61 | ``` |
iv123 | 0:7a352727249b | 62 | |
iv123 | 0:7a352727249b | 63 | Use the API Console to write this value to resource `/led/0/color` and change the color of the LED to turquoise. |
iv123 | 0:7a352727249b | 64 | |
iv123 | 0:7a352727249b | 65 | ### Other variables |
iv123 | 0:7a352727249b | 66 | |
iv123 | 0:7a352727249b | 67 | We can also change the value of the timeout (in a real light system you probably want at least 30 seconds) and read the number of times the PIR sensor triggered (in the GET tab). |