跳到主要内容

Command Response

about

The Command-Response framework in ESP RainMaker is an alternative to the set parameters workflow to send data from clients (like phone apps) to devices. It can be used by admin as well as end users.


Use Case

In an ESP RainMaker setup, Command Response can be used as a fail-safe mechanism for handling device state recovery after an intermittent power loss. For example, imagine a smart light controlled via ESP RainMaker. Normally, it operates based on real-time cloud commands. However, if the device experiences a power outage and restarts, it might lose its last known state. By implementing Command Response, the device can query the cloud for the last received command upon reboot, ensuring it restores the correct light settings (e.g., brightness and color) instead of defaulting to an off state. This ensures seamless operation and prevents unexpected behavior after power interruptions.

Some advantages of using this framework

  • Get status of any request reliably
  • Allow setting a validity for the request so that a request can be triggered even when a device is offline
  • Provide better access control since the node knows the role of the user who triggered the command (admin, primary, secondary)
Before proceeding

If you are already familiar with the Command Response in ESP RainMaker, please proceed to the usage guides.

  • For the usage of command response on firmware, click here.
  • To test out command response on RainMaker cli, click here

Else, please read on.


The diagram below explains how command - response works.

alt text


Client side

Client to cloud communication uses REST APIs with JSON Payload. To invoke a command, a POST API is invoked with this payload:

JSON Payload (Click to view)
  {
"node_ids": [
"6jyMKuFuAq3xxxxxxxxyBQ"
],
"cmd": 1,
"data": {
"brightness": 10
},
"timeout": 30
}
  • node_ids: List of node ids to which the command is to be sent.
  • cmd: The command id.
  • data: The data payload for the command.
  • timeout: The timeout (in seconds) for which the command is valid.

For passing binary data to the nodes, send it as a base64 string.
Add is_based64 as an additional key. E.g.

JSON Payload (Click to view)
: {
"node_ids": [
"6jyMKuFuAq3xxxxxxxxyBQ"
],
"cmd": 1,
"data": "YWJjZAo="
"timeout": 30,
"is_base64": true
}

The cloud backend assigns a request id for this and responds with the following:

JSON Payload (Click to view)
{
"request_id": "6jyMKuFxxxxxxxxStTKyBQ",
"status": "success"
}

The client can then query for the status of this request using the GET API, passing the above request id as a query paramter. Here are the following status:

Status
requested
in_progress
success
timed_out
failure
提示

For success and failure, the corresponding response value will also be returned.


Device side

Device to cloud communication uses TLV8, which is a variant of TLV encoding with 8bit length field. This allows flexibility in communication, as the application data can be JSON, XML, binary or any other convenient format.


Types

TypeMacro in codeDescriptionLength
1ESP_RMAKER_TLV_TYPE_REQ_IDRequest IdVariable length string, max 32 characters
2ESP_RMAKER_TLV_TYPE_USER_ROLEUser Role1 byte
3ESP_RMAKER_TLV_TYPE_STATUSStatus1 byte
4ESP_RMAKER_TLV_TYPE_TIMESTAMPTimestampTBD (Unused)
5ESP_RMAKER_TLV_TYPE_CMDCommand2 bytes (little endian)
6ESP_RMAKER_TLV_TYPE_DATADataVariable length

Roles

These are mapped to bits

ValueMacro in codeDescription
1ESP_RMAKER_USER_ROLE_SUPER_ADMINSuper admin
2ESP_RMAKER_USER_ROLE_PRIMARY_USERPrimary End User
4ESP_RMAKER_USER_ROLE_SECONDARY_USERSecondary End User

Values for status

ValueMacro in CodeDescription
0ESP_RMAKER_CMD_STATUS_SUCCESSSuccess
1ESP_RMAKER_CMD_STATUS_FAILEDGeneric Failure
2ESP_RMAKER_CMD_STATUS_CMD_INVALIDInvalid Command
3ESP_RMAKER_CMD_STATUS_AUTH_FAILAuthentication Failed
4ESP_RMAKER_CMD_STATUS_NOT_FOUNDCommand not found

Usage Guide

Firmware

This usage guide would mainly cover the firmware side to enable and use command response.
On the usage of command response on firmware, click here.


Test Command Response Feature with RainMaker CLI

  • To test out command response on RainMaker cli, click here

More info about API

REST API usage information can be found in the Swagger documentation.

On this page