Deliver a Command to a Device
Function
A product model defines commands that the IoT platform can deliver to devices. An application can call this API to deliver commands to a specific device to control the device synchronously. The platform synchronously sends commands to the device and returns the command execution result to the application. If the device does not respond, the platform returns a timeout message to the application. The timeout interval is 20 seconds. If the command delivery takes more than 20 seconds, you are advised to use the API for Message Delivery.
Notes:
-
The API can only be used to deliver commands to MQTT devices but not NB-IoT devices.
-
This API can be used to deliver commands synchronously to only one device at a time. To deliver commands synchronously to multiple devices, see Create a Batch Task.
Calling Method
For details, see Calling APIs.
Authorization Information
Each account has all the permissions required to call all APIs, but IAM users must be assigned the required permissions.
- If you are using role/policy-based authorization, see Permissions Policies and Supported Actions for details on the required permissions.
- If you are using identity policy-based authorization, the following identity policy-based permissions are required.
Action
Access Level
Resource Type (*: required)
Condition Key
Alias
Dependencies
iotda:commands:send
Write
app *
-
-
-
-
g:EnterpriseProjectId
URI
POST /v5/iot/{project_id}/devices/{device_id}/commands
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| project_id | Yes | String | Parameter description : project ID. For details about how to obtain the project ID, see Obtaining a Project ID. |
| device_id | Yes | String | Parameter description: ID of the device to which the message is delivered. The ID is unique and is allocated by the platform during device registration. Value: The value can contain a maximum of 128 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed. |
Request Parameters
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| Instance-Id | No | String | Parameter description : instance ID. Unique identifier of each instance in the physical multi-tenant scenario. Mandatory for professional editions and recommended in other cases. Log in to the IoTDA console and choose Overview in the navigation pane to view the instance ID. For details, see Viewing Instance Details. |
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| service_id | No | String | Parameter description: ID of the device service that the device command belongs to, which is defined in the product model associated with the device. Value: The value can contain a maximum of 64 characters. |
| command_name | No | String | Parameter description: command name, which is defined in the product model associated with the device. Value: The value can contain a maximum of 128 characters. |
| paras | Yes | Object | Parameter description: command that will be executed. The command is in JSON format (key-value pairs). If service_id is specified, each key is a paraName in commands in the product model. If service_id is left empty, custom command format is used. {"value":"1"} is an example device command. The specific format depends on the application and device. This parameter supports only the JSON format and does not support character strings. |
Response Parameters
Status code: 200
| Parameter | Type | Description |
|---|---|---|
| command_id | String | Device command ID, which is unique and is allocated by the platform during command delivery. |
| response | Object | Command execution result reported by the device. The value is in JSON format. The specific format depends on the application and device. |
| error_code | String | Error code of a command delivery exception. |
| error_msg | String | Error message of a command delivery exception. |
Example Requests
Creates a command. The command name is ON_OFF, and the command is ON.
POST https://{endpoint}/v5/iot/{project_id}/devices/{device_id}/commands
{
"service_id" : "reboot",
"command_name" : "ON_OFF",
"paras" : {
"value" : "ON"
}
} Example Responses
Status code: 200
{
"command_id" : "b1224afb-e9f0-4916-8220-b6bab568e888",
"response" : {
"result_code" : 0,
"response_name" : "COMMAND_RESPONSE",
"paras" : {
"result" : "success"
}
},
"error_code" : "string",
"error_msg" : "string"
} Status Codes
| Status Code | Description |
|---|---|
| 200 | OK |
| 400 | Bad Request |
| 401 | Unauthorized |
| 403 | Forbidden |
| 404 | Not Found |
| 500 | Internal Server Error |
Error Codes
See Error Codes.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot