This is an interactive example page for device communication between Pelion Device Management and its client device.
Through this example, you get to understand how to use Pelion Device Management Service API to communicate with your deployed devices(s).
You can find source code of this example page here.
Prerequisites
To go through this example, you need these.
- Pelion Device Management API key (Get API key)
- Device running mbed-cloud-client-example or mbed-os-example-pelion
API key
Please input your API key here (API key starts from ak_...). About API key and how to get it, please refer online document.
Open event notification channel and receive event from device
To receive messages from your devices, you need to open an event notification channel. We use WebSocket notification channel in this example.
To use the WebSocket channel, follow these steps. Detailed description on this flow is here. Note that you can open only 1 notification channel per API key.
Create WebSocket notification channel
First we will need to open a Event notification channel. Click this button to create a WebSocket notification channel with PUT /v2/notification/websocket.
Connect WebSocket notification channel
After creating your WebSocket notification channel, connect to it with GET /v2/websocket-connect.
Now you have a event notification channel open. You will receive the device lifitime events (Register, Register-Update and Expire) by default.
Connect your device
Turn on or reset your device and connect to Pelion. You should see reqistration or reg-update event in the Notification Channel below.
Choose your device
First, click Get Registered Devices button below to get the devices which state is registered. GET /v3/devices is used to get device list.
When you click the button above, you'll see the registered device(s) in the box below. Choose your target device.
Write resource to device
Send a resource update request to a device with POST /v2/device-requests. For detail flow, see the Write operation.
With Write request you can configure the device. The example application blinks a LED, let's configure new blink pattern by writing it in "pattern_resource" /3201/0/5853. The default value of this resource is "500:500:500:500". We change this to "500:1000:500:1000". This value must be encoded into Base64, which is done in our script.
You'll get an async-response on your notification channel when the device accepted the request. In addition, the user LED on the device will blink with the specified pattern, 500 ms, 1000 ms, 500 ms, 1000 ms.
Read resource from device
Send a resource read request to a device with POST /v2/device-requests. For detail flow, see the Read operation.
For example, let's read resource Digital Input Counter /3200/0/5501.
Note for cache
If max-age value is set by the device's client, reported values from the device is cached for max-age:
https://www.pelion.com/docs/device-management/current/connecting/device-guidelines.html#resource-cache
If the cached value is valid, it will be returned as the response. If your webapp needs to get the current value from the device, not cached value, max-age must be set 0 (default is 0) at the device client.
Subscribe resource
Subscribe
Subscribe to a resource on a device with PUT /v2/subscription/{device-id}/{resourcePath}.
For example, let's subscribe Digital Input Counter /3200/0/5501 resource.
You will receive an async response and followed by notifications for resource change.
Unsubscribe
To stop subscribing to the resource, use DELETE /v2/subscriptions/{device-id}/{resourcePath}.
Presubscribe resource
Presubscribe to a resource with PUT /v2/subscriptions.
A presubscription automatically subscribes to Resources based on given rules. When a device with Resources match the set rules, the Device management Connect service sends a subscription (observe) request to the device on behalf of your application. See this online document for details of presubscription.
Subscribe
Let's presubscribe to Digital Input Counter /3200/0/5501. To presubscribe, use PUT /v2/subscriptions. This case, any device which has Digital Input Counter /3200/0/5501 resource will be subscribed automatically when device registers or does register update.
Try to reboot your device. The resource /3200/0/5501 will be subscribed automatically and you'll see notification on your notification channel.
Unsubscribe
To unsubscribe presubscription, use DELETE /v2/subscriptions.
Set notification rules for a resource
The web application can set a rule-based subscription to an Object, an Object Instance or a Resource.
Subscribe
Note that you need to subscribe /3200/0/5501 besides setting a notification rule.
Set notification rule
The device updates Digital Input Counter /3200/0/5501 every 5 seconds and is reported to Pelion Device Management. However, your webapp may need data only every 1 minutes and not want to receive redundant data. In this case, you can use the notification rule pmin, which sets minimum report period. Details for notification rules, see this page.
To set pmin 1 minute to your subscription to Digital Input Counter /3200/0/5501, set tMin with POST /v2/device-request. Specify the resource path in uri field with pmin=60 parameter. This is done in the script.
Unsubscribe
To stop subscribing to the resource, use DELETE /v2/subscriptions/{device-id}/{resourcePath}.
Delete WebSocket notification channel
In the end, delete the WebSocket notification channel bound to the API key. Use DELETE /v2/notification/websocket. This is required to change the channel from websocket to another type.
End-of-Contents