Protocol of serial port access

Connection of general serial port of Tuya Cloud

1. Introduction

Protocols are classified into basic and functional protocols. Basic protocols are independent of products. They are common protocols of modules and include module initialization commands and some extended functional commands. Functional protocols are DP data transmitting and receiving commands that the platform automatically generates based on the definition of each product DP.

2. Protocol Framework

The MCU interworks with the Wi-Fi module through a serial port and common firmware. Settings of the communication parameters are as follows:

  • Bits per second: 9600
  • Data bits: 8
  • Parity: None
  • Stop bits: 1
  • Flow control: None The following table describes the data frame format.

The following table describes command words:

Command Word Description
0x00 Heartbeat detection.
0x01 Query product information.
0x02 Query the working mode of the Wi-Fi module.
0x03 Report the network connection status of the device.
0x04 Reset the Wi-Fi module and switch the network configuration mode.
0x05 Reset the Wi-Fi module and select a network configuration mode.
0x06 Deliver DP commands.
0x07 Report DP status.
0x08 Query the device initialization status.
0x0a (Optional) Start OTA upgrade.
0x0b (Optional) Transmit the OTA upgrade package.
0x1c (Optional) Obtain the local time.
0x0e Test the Wi-Fi function (product testing command).

2.1 Basic Protocol

Basic protocols are the same for each product and mandatory for the Wi-Fi module. Basic protocols include heartbeat detection and query for product information, working mode of the Wi-Fi module, and Wi-Fi status. Command words 0x00 to 0x08 are basic commands of the Wi-Fi module. Command words 0x0a to 0x0e are basic functions of the Wi-Fi module, including the MCU OTA upgrade, local time acquisition, and product testing. To enable the Wi-Fi module to work properly, you need to initialize the module and configure the network connection. The following figure shows command words involved in the module initialization protocol and the initialization process:

After being powered on, the Wi-Fi module sends heartbeat packets continuously. After the MCU responds, the preceding initialization process starts.

1. Heartbeat detection:

After the MCU is powered on, it returns 0x00 for the first heartbeat packet and 0x01 for the second and later heartbeat packets. After receiving 0x00, the Wi-Fi module is automatically initialized for data synchronization. Later heartbeat packets are used to determine whether the device is online and automatically connects to the network upon disconnection. For example, the Wi-Fi module sends 55 aa 00 00 00 00 ff, and the MCU returns 55 aa 03 00 00 01 00 03 for the first packet and 55 aa 03 00 00 01 01 04 for other packets.

2. Query product information:

After receiving a heartbeat response, the Wi-Fi module sends a command to query the product information. The MCU reports the product information, including the PID, version, and mode. Note that characters, such as curly brackets ({}), colons (:), and double quotation marks ("") also need to be included. For details about the format, see the following table.

For example, the Wi-Fi module sends 55 aa 00 01 00 00 00, and the MCU returns PID:RN2FVAgXG6WfAktU, as shown in the following figure. You need to convert your product ID to an ASCII code and use the ASCII code to replace RN2FVAgXG6WfAktU.

3. Query the working mode of the set Wi-Fi module:

After receiving the product information, the Wi-Fi module sends command word 0x02 to query the working mode of the set Wi-Fi module. The working mode of the Wi-Fi module instructs how to show the Wi-Fi status and how to reset the Wi-Fi module. a. Cooperative processing by the MCU and Wi-Fi module The Wi-Fi module notifies the MCU of the current Wi-Fi status over a serial port. The MCU controls status of the Wi-Fi indicator.

b. Processing by the Wi-Fi module The GPIO pins of the Wi-Fi module change status of the Wi-Fi indicator (LED indicator). The Wi-Fi module is reset based on the GPIO inputs. In processing by the Wi-Fi module mode, the Wi-Fi module triggers a reset when it detects that the GPIO input is at a low level for more than 5s. GPIO pins used by the Wi-Fi indicator and Wi-Fi reset button are configured by using command word 0x02.

If the MCU selects cooperative processing by the MCU and Wi-Fi module, it reports 0. If the MCU selects processing by the Wi-Fi module, it reports the I/O interfaces of the Wi-Fi indicator and Wi-Fi reset button. If the MCU selects processing by the Wi-Fi module, the following steps 4 to 6 can be ignored. For example, the Wi-Fi module sends 55 aa 00 02 00 00 01. The MCU returns 55 aa 03 02 00 00 04 (cooperative processing by the MCU and Wi-Fi module) or 55 aa 03 02 00 02 05 00 0b (processing by the Wi-Fi module). “05” and “00” indicate the I/O interfaces 5 and 0 that are connected to the Wi-Fi indicator and Wi-Fi reset button, respectively.

4. Report the Wi-Fi status:

When the Wi-Fi module detects that the MCU restarts or the Wi-Fi status is changed, the Wi-Fi module proactively reports the Wi-Fi status to the MCU. Based on the Wi-Fi status indicated by the command word 0x03, the MCU controls blinking of the Wi-Fi indicator. The following table describes six states in the protocol V03.

Device Network Connection Status Description Status Value LED Indicator Status
State 1 Smart network configuration 0x00 The indicator blinks at 250 ms intervals.
State 2 AP network configuration 0x01 The indicator blinks at 1500 ms intervals.
State 3 The Wi-Fi is configured. However, the device fails to connect to the router. 0x02 The indicator is off.
State 4 The Wi-Fi is configured, and the device successfully connects to the router. 0x03 The indicator is steady on.
State 5 The device connects to the router and cloud. 0x04 The indicator is steady on.
State 6 The Wi-Fi device is in low power consumption mode. 0x05 The indicator is off.

For example, the Wi-Fi module sends the checksum of 55 aa 00 03 00 01 01 (“01” indicates AP network configuration), and the MCU returns 55 aa 03 03 00 00 05.

5. Reset the Wi-Fi module.

Network configuration command: You can reset the Wi-Fi module to enable the device to enter network configuration state. The network configuration modes include: a. Smart network configuration, in which mode, the Wi-Fi indicator blinks quickly. This mode is simple and convenient. b. AP network configuration, in which mode, the Wi-Fi indicator blinks slowly. This mode is stable and reliable. We recommend that you use both modes. You can customize the triggering mechanism to control quick or slow blinking of the Wi-Fi indicator. When receiving command word 0x04 sent from the MCU, the Wi-Fi module changes the network configuration mode. The default mode is smart network configuration, and the Wi-Fi module switches between the smart and AP network configuration modes.

For example, the MCU sends 55 aa 03 04 00 00 06, and the Wi-Fi module returns 55 aa 00 04 00 00 03.

6. Reset the Wi-Fi module and select a network configuration mode:

Based on the parameters sent by the MCU, the Wi-Fi module selects the smart or AP network configuration mode. Similar to command word 0x04, the command can be used for network configuration. It also enables the Wi-Fi module to select a network configuration mode.

For example, the MCU sends 55 aa 03 05 00 01 00 08 (indicating the smart network configuration mode) or 55 aa 03 05 00 01 01 09 (indicating the AP network configuration mode), and the Wi-Fi module returns 55 aa 00 05 00 00 04.

7. Query the MCU working status:

The Wi-Fi module uses command word 0x08 to query the status of all MCU DPs as the initial values that are displayed on the app. After receiving the command word, the MCU reports data of all DPs one by one. The Wi-Fi module queries the DP status in the following scenarios: a. The Wi-Fi module is powered on for the first time and connects to the MCU through heartbeat packets. b. The Wi-Fi module detects that the MCU has restarted or gone offline and then online. For example, the Wi-Fi module sends 55 aa 00 08 00 00 07, and the MCU returns the checksum of 55 aa 03 07 N **** (DP 1), the checksum of 55 aa 03 07 N **** (DP 2), or the checksum of … (DP N).

8. Test product functions:

The product testing command is used to test the RF performance of the Wi-Fi module during mass production of the product. We recommend that you invoke the product testing command 5s after the Wi-Fi module is powered on and initialized. After receiving the product testing command, the Wi-Fi module automatically searches for the “tuya_mdev_test” WLAN network and returns the search result with the signal strength (0 to 100 with a step of 20). For example, the MCU sends the checksum of 55 aa 03 0e 00 00, and the Wi-Fi module returns 55 aa 00 0e 00 02 01 28 38, indicating that the product testing is successful and that the signal strength is 40.

2.2 Functional Protocol

Functional protocols are used for delivering and reporting DP data. The command word for the Wi-Fi module to deliver DP data is 0x06, and that for the MCU to report DP data is 0x07. After receiving a data delivery command, the MCU performs corresponding logical control. When the DP status is changed, the MCU reports the DP data and changes the DP status displayed on the app. The Wi-Fi module filters out duplicated DP data that the MCU reports. Example:

Note: a. Value data has four bytes. If a value contains less than four bytes, 0 is supplemented before the value. For example, if the MCU sends the checksum of 55 aa 03 07 00 08 02 02 00 04 00 00 00 1e, the target temperature is 30°C. b. Alarm data can contain multiple alarms reported simultaneously. Each bit represents an alarm. The value 1 indicates that the fault occurs, and the value 0 indicates that the fault does not occur. For example, if the MCU sends the checksum of 55 aa 03 07 00 06 0d 05 00 02 00 09, the faults represented by bit 0 and bit 3 occur. c. The meaning and display of string data must be the same as that on the panel. Customized string data needs to be negotiated with the panel developer. d. Raw data is transparent and typically used for implementing complex functions. We do not recommend that you use raw data yourself.

400 Call

Consult

400-881-8611