Updated on 2023-02-16 GMT+08:00

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. The API can only be used to deliver commands to MQTT devices but not NB-IoT devices.

Debugging

You can debug this API through automatic authentication in API Explorer or use the SDK sample code generated by API Explorer.

URI

POST /v5/iot/{project_id}/devices/{device_id}/commands

Table 1 Path Parameters

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

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

No

String

Parameter description: user token. You can obtain the token by calling the IAM API Obtaining a User Token Through Password Authentication. In the returned response header, X-Subject-Token is the desired user token. For details about how to obtain the token, see Token Authentication.

Instance-Id

No

String

Parameter description: instance ID. This parameter is required only when the API is called from the management plane in the physical multi-tenant scenario.

Table 3 Request body parameters

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.

Maximum: 64

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.

Maximum: 128

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.

Maximum: 2048

Response Parameters

Status code: 200

Table 4 Response body parameters

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.

Example Requests

POST https://{endpoint}/v5/iot/{project_id}/devices/{device_id}/commands

{
  "service_id" : "b1224afb-e9f0-4916-8220-b6bab568e888",
  "command_name" : "ON_OFF",
  "paras" : {
    "value" : "ON"
  }
}

Example Responses

Status code: 200

OK

{
  "command_id" : "b1224afb-e9f0-4916-8220-b6bab568e888",
  "response" : {
    "result_code" : 0,
    "response_name" : "COMMAND_RESPONSE",
    "paras" : {
      "result" : "success"
    }
  }
}

Status Codes

Status Code

Description

200

OK

400

Bad Request

403

Forbidden

404

Not Found

500

Internal Server Error

Error Codes

See Error Codes.