Updated on 2024-12-02 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. If the command delivery takes more than 20 seconds, use the API Delivering a Message to a Device.

Notes:

  • The API can only be used to deliver commands to MQTT devices but not NB-IoT devices.

  • This API supports synchronous command delivery to a single device. For details about how to deliver commands to multiple devices synchronously, see Creating a Batch Task.

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. 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.

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.

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

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.

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

OK

{
  "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.