Connect devices to IoT Platform over CoAP
IoT Platform supports device connections over CoAP, a protocol designed for resource-constrained devices such as NB-IoT devices. This topic covers the connection workflow and device authentication using DTLS or symmetric encryption.
Workflow

| Area | Remarks |
| ① | Integrate the IoT Platform SDK into the NB-IoT module. Apply for the device certificate in the IoT Platform console and burn it to the device. |
| ② | Connect the NB-IoT device to IoT Platform over the carrier mobile network. Confirm NB-IoT network availability in your region with the local carrier. |
| ③ | Use the carrier M2M platform to manage data traffic and fees. |
| ④ | Submit real-time data to IoT Platform over CoAP or UDP. IoT Platform supports secure connections at scale and forwards data to Alibaba Cloud services such as big data, database, and Tablestore (OTS). |
| ⑤ | Forward data to business servers through IoT Platform API operations and message pushing to integrate devices with applications. |
Connect devices by using symmetric encryption
- Connect to the CoAP server.
Endpoints: For information about how to connect a device to the endpoint of a public instance or Enterprise Edition instance over CoAP, see View and configure instance endpoints.
- If you use an Alibaba Cloud device SDK to connect the device to IoT Platform over CoAP, the Pre-Shared Key (PSK) algorithm is used as the default symmetric encryption algorithm.
If you use a third-party device SDK, you must download the root certificate for DTLS secure channels. Then, you can use the DTLS libraries to connect the device to IoT Platform. The following code is used to implement the PSK algorithm-based encryption:
psk_id: "${authType}" + "|" + "${signMethod}" + "|" + "${productKey}" + "&" + "${deviceName}" + "timestamp" psk: signMethod(DeviceSecret, "${productKey}" + "&" + "${deviceName}" + "${timestamp}")Table 1. Parameters Parameter Required Description authType Yes The verification type. Valid value: devicename. signMethod Yes The signature algorithm. Valid values: hmacmd5, hmacsha1, and hmacsha256. productKey Yes The ProductKey of the product to which the device belongs. deviceName Yes The DeviceName of the device. DeviceSecret Yes The DeviceSecret of the device. timestamp Yes The timestamp. - Verify the device.
Sample request:
POST /auth Host: ${YourEndpoint} Port: 5682 Accept: application/json or application/cbor Content-Format: application/json or application/cbor payload: {"productKey":"a1NUjcV****","deviceName":"ff1a11e7c08d4b3db2b1500d8e0e55","clientId":"a1NUjcV****&ff1a11e7c08d4b3db2b1500d8e0e55","sign":"F9FD53EE0CD010FCA40D14A9FE******", "seq":"10"}Table 2. Parameters Property Description Method The request method. Valid value: POST. URL The URL. Valid value: /auth.Host The endpoint. Port The port number. Valid value: 5682.Accept The MIME type of data received by the device. Valid values: application/json and application/cbor. Content-Format The MIME type of data that the device submits to IoT Platform. Valid values: application/json and application/cbor. payload The JSON-formatted device information. Fields are described in the following table. Table 3. Fields in the payload parameter Field Required Description productKey Yes The ProductKey of the product, a GUID issued by IoT Platform. Obtain it from the Device Details page of the instance. deviceName Yes The DeviceName of the device, auto-generated or specified during registration. Obtain it from the Device Details page of the instance. ackMode No The communication mode. Default value: 1. Valid values: - 0: IoT Platform returns response data and an ACK message at the same time.
- 1: IoT Platform returns an ACK message and then returns response data.
Default value: 0.
sign Yes The signature value. You can use the
signmethod(DeviceSecret,content)function to calculate a signature. Then, you can specify the signature for the sign parameter. The hmacmd5 and hmacsha1 signature algorithms are supported.Required parameters:
- signmethod: the signature algorithm. The value must be the same as the value that you specified for the signmethod parameter.
- DeviceSecret: the DeviceSecret of the device. Obtain it from the Devices page of the instance.
- content: all parameters submitted to IoT Platform except version, sign, resources, and signmethod. Values are concatenated in alphabetical order with no separator. Note The parameter values that are used to calculate the signature must be the same as the parameter values that you specify in the request for device verification.
Example:
hmac_md5(mRPVdzSMu2nVBxzK77ERPIMxSYIv****, clientIda1NUjcV****&ff1a11e7c08d4b3db2b1500d8e0e55deviceNameff1a11e7c08d4b3db2b1500d8e0e55productKeya1NUjcV****seq10timestamp1524448722000)signmethod No The signature algorithm. Valid values: hmacmd5 and hmacsha1. Default value: hmacmd5. clientId Yes The client ID, 1 to 64 characters. Recommended value: the MAC address or SN of the device as the clientId. timestamp No The timestamp. IoT Platform does not verify the timestamp. seq Yes The serial number in the verification request, a random number generated by the device. Sample response:
{"random":"ad2b3a5eb51d6****","seqOffset":1,"token":"MZ8m37hp01w1SSqoDFzo001050****.ad2b"}Table 4. Response parameters Parameter Description random The key that is used to encrypt upstream and downstream data. seqOffset The initial offset of the seq parameter. token The token returned if the device is verified. - Submit data.
Sample request:
POST /topic/${topic} Host: ${YourEndpoint} Port: 5682 Accept: application/json or application/cbor Content-Format: application/json or application/cbor payload: ${your_data} CustomOptions: number:2088, 2089Table 5. Parameters Parameter Required Description Method Yes The request method. Valid value: POST. URL Yes The URL of the topic. Format: /topic/${topic}. Replace the ${topic} variable with the topic to which the data is sent.Host Yes The endpoint. Port Yes The port number. Valid value: 5682.Accept Yes The MIME type of data received by the device. Valid values: application/json and application/cbor. Content-Format Yes The MIME type of upstream data. Not verified by IoT Platform. Valid values: application/json and application/cbor. payload Yes The upstream data that is encrypted by using the Advanced Encryption Standard (AES). Note If you use the AES to encrypt data, set the Transform parameter toAES/CBC/PKCS5Paddingand the IV parameter to543yhjy97ae7fyfg. A key is generated by using the SHA-256 algorithm.Example:
If the request isdeviceSecret=zPwChiLh0EaifR809D5Rc6LDIC6A****, the response israndom=8fe3c8d50e10****.- Combine the values of the deviceSecret and random parameters to form a string in the
${deviceSecret},${random}format.zPwChiLh0EaifR809D5Rc6LDIC6A****,8fe3c8d50e10**** - IoT Platform encodes the preceding string in the UTF-8 format, encrypts the encoded string by using the SHA-256 algorithm, and then converts the string into a hexadecimal string.
59ea5ac1cb092e5910c405821119959e5297516d185b71e344735cf3f268**** - IoT Platform uses the subString(16,48) function to extract a sub-string of 32 characters from the preceding string to form a key. The extraction starts from the 17th character of the string.
10c405821119959e5297516d185b71e3
CustomOptions Yes The custom option. Valid values: 2088: the token parameter. Use the value of the token parameter that is returned after the device is verified.Note The token parameter is required when the device submits data. If the token is expired, you must re-verify the device and obtain another token.2089: the seq parameter. The value must be greater than the value of the seqOffset parameter. The value must be a random number that is unique during the validity period of the verification. We recommend that you use a value that is incremented based on the seq parameter in each request and use the AES to encrypt the value.
Sample response:
number:20902090: the ID of the message in IoT Platform.You can configure the tokenand seq parameters in the CustomOptions parameter. You can also configure the token and seq parameters in the URL parameter. Example:
/topic/${topic}?token=xxxx&seq=xxxxx. If you configure the token and seq parameters for the CustomOptions and URL parameters at the same time, the CustomOptions parameter is used.After a message is sent to IoT Platform, a status code that indicates a successful request and a message ID that is generated by IoT Platform are returned.
- Combine the values of the deviceSecret and random parameters to form a string in the
Connect devices to IoT Platform over DTLS
- Connect to the CoAP server.
Endpoints: For information about how to connect a device to the endpoint of a public instance or Enterprise Edition instance over CoAP, see View and configure instance endpoints.
- Download the root certificate for DTLS secure channels. Then, you can use the DTLS libraries to connect the device to IoT Platform. The following code is used to implement the PSK algorithm-based encryption:
psk_id: "${authType}" + "|" + "${signMethod}" + "|" + "${productKey}" + "&" + "${deviceName}" + "timestamp" psk: signMethod(DeviceSecret, "${productKey}" + "&" + "${deviceName}" + "${timestamp}")Table 6. Parameters Parameter Required Description authType Yes The verification type. Valid value: devicename. signMethod Yes The signature algorithm. Valid values: hmacmd5, hmacsha1, and hmacsha256. productKey Yes The ProductKey of the product to which the device belongs. deviceName Yes The DeviceName of the device. DeviceSecret Yes The DeviceSecret of the device. timestamp Yes The timestamp. - Verify the device. You can use the auth operation to verify the device and obtain a token. The token parameter is required when the device submits data.
Sample request:
POST /auth Host: ${YourEndpoint} Port: 5684 Accept: application/json or application/cbor Content-Format: application/json or application/cbor payload: {"productKey":"ZG1EvTE****","deviceName":"NlwaSPXsCpTQuh8FxBGH","clientId":"mylight1000002","sign":"bccb3d2618afe74b3eab12b94042****"}Parameters and payload fields are the same as Connect devices by using symmetric encryption, except for the Port parameter.
Sample response:
response: {"token":"f13102810756432e85dfd351eeb4****"}Table 7. Response codes Code Message Payload Description 2.05 Content A token if the device passes the verification. The request is valid. 4.00 Bad Request no payload The payload in the request is invalid. 4.01 Unauthorized no payload The request is unauthorized. 4.03 Forbidden no payload The request is forbidden. 4.04 Not Found no payload The requested URL does not exist. 4.05 Method Not Allowed no payload The request method is not supported. 4.06 Not Acceptable no payload The Accept parameter is invalid. 4.15 Unsupported Content-Format no payload The requested content is invalid. 5.00 Internal Server Error no payload The request failed due to a timeout or server error. - Submit data.
The device submits data to a custom topic.
Create a custom topic in the IoT Platform console: go to Product Details > Topic Categories. Only topics with Publish permission can submit data.
For example, a topic is in the
/${YourProductKey}/${YourDeviceName}/pubformat. If the DeviceName is device and the ProductKey is a1GFjLP ****, you can use thea1GFjLP****.coap.cn-shanghai.link.aliyuncs.com:5684/topic/a1GFjLP****/device/pubtopic to submit data.Sample request:
POST /topic/${topic} Host: ${YourEndpoint} Port: 5684 Accept: application/json or application/cbor Content-Format: application/json or application/cbor payload: ${your_data} CustomOptions: number:2088Table 8. Parameters Parameter Required Description Method Yes The request method. Valid value: POST. URL Yes /topic/${topic}. Replace the${topic}variable with the topic to which the data is sent.Host Yes The endpoint. Port Yes The port number. Valid value: 5684.Accept Yes The MIME type of data received by the device. Valid values: application/json and application/cbor. Content-Format Yes The MIME type of upstream data. Not verified by IoT Platform. Valid values: application/json and application/cbor. CustomOptions Yes - number: Set the value to
2088. - token: the token that is returned by the verification service.
Note The token parameter is required when the device submits data. If the token is expired, you must re-verify the device and obtain another token. - number: Set the value to