Tuya Sandwich Evaluation Kit -Wi-Fi Arduino-Socket Implementation

Last Updated on2020-02-13 07:39:04103


This document mainly introduces the application code part of the socket demo, about environment building, SDK porting, downloading and debugging. Please refer to the following article.

《Tuya Sandwich Development Board Development Environment》

《Tuya Sandwich Development Board Quick Start》

《MCU-SDK Migration》


  • Step 1: Download verification

    1. Graffiti sandwich development board, long press button 1 for distribution network, the black button next to the indicator is the development board reset port, USB is the program download port.


    2. Tuya socket development board

      Corporate WeChat screenshot_1576322458763.png

    3. Tuya Wi-Fi Development Board

    4. The connection method is as follows: (from top to bottom: Tuya Relay Board, Tuya Sandwich Development Board, Tuya Wi-Fi Development Board)

  • Step 2: Program implementation

    Developers need to focus on three functions: setup (), loop () and dp_download_handle ()

    1. setup () function is used to perform some initialization content, only executed once
    void setup () {
      pinMode (relay, OUTPUT); // Socket IO port initialization
      digitalWrite (relay, LOW);
      pinMode (PC13, INPUT); // Keyboard initialization
      pinMode (wifi_led, OUTPUT); // Initialize the distribution network status indicator
      mySerial.begin (9600); // Software serial port initialization
      mySerial.println ("myserial init successful!");
      Serial.begin (115200); // PA3 RX PA2 TX
      Serial.println ("serial init successful!");
      wifi_protocol_init (); // Protocol serial port initialization
    1. The loop () function will loop through the contents of the function body
    void loop () {
      if (init_flag == 0) {
        time_cnt ++;
        if (time_cnt% 6000 == 0) {
          time_cnt = 0;
          cnt ++;
        wifi_stat_led (& cnt); // WI-FI status processing
      wifi_uart_service (); // WI-FI serial port processing service
      myserialEvent (); // Serial port receiving processing
      key_scan (); // Reset network button detection

    3.dp_download_handle () is mainly used to process DP data issued by the cloud, developers need to implement the corresponding functions according to the corresponding DP release

    / ************************************************* ****************************
    Function Name: dp_download_handle
    Function description: dp release processing function
    Input parameters: dpid: DP
               value: dp data buffer address
               length: dp data length
    Return parameter: Success return: SUCCESS / Failure return: ERRO
    Instructions for use: This function cannot be modified by the user
    ********************************************** *************************** /
    unsigned char dp_download_handle (unsigned char dpid, const unsigned char value [], unsigned short length)
      / *********************************
      Current function processing can be issued / reportable data call
      The specific function needs to implement the issued data processing
      To complete the application, the processing result needs to be fed back to the APP.Otherwise, the APP will consider that the delivery fails.
      *********************************** /
      unsigned char ret;
      switch (dpid)
        case DPID_SWITCH_1:
          // Switch 1 processing function
          ret = dp_download_switch_1_handle (value, length);
      return ret;

    The socket switch function is implemented in dp_download_switch_1_handle ()

    / ************************************************* ****************************
    Function Name: dp_download_switch_1_handle
    Function description: Processing function for DPID_SWITCH_1
    Input parameter: value: data source data
            : length: data length
    Return parameter: Success return: SUCCESS / Failure return: ERROR
    Instruction for use: Can be issued and reportable, need to report the processing result to the app after processing the data
    ********************************************** *************************** /
    static unsigned char dp_download_switch_1_handle (const unsigned char value [], unsigned short length)
      #define relay 10
      // Example: The current DP type is BOOL
      unsigned char ret;
      // 0: off / 1: on
      unsigned char switch_1;
      switch_1 = mcu_get_dp_download_bool (value, length);
      if (switch_1 == 0)
        // Switch off
          digitalWrite (relay, LOW);
        // Switch on
          digitalWrite (relay, HIGH);
      // There should be feedback after processing the DP data
      ret = mcu_dp_bool_update (DPID_SWITCH_1, switch_1);
      if (ret == SUCCESS)
        return SUCCESS;
        return ERROR;

    Through the implementation of the above functions, a simple socket demo can be realized.

  • Step 3: Arduino-API instructions

    As the Tuya Sandwich development board is programmed using Arduino standard API, Arduino-API developers can refer to the following URL:

    《Arduino-API Instructions》

    ArduinoIDE also provides Demo to demonstrate the use of related APIs