MCU Quick Start

Last Updated on2020-04-25 07:19:01


After the development environment of Tuya Sandwich Evaluate Kit is set up, this article will introduce the connection between Tuya Sandwich development board and each end in development, how to create a development project based on specific products, and how to download the developed program to Tuya Sandwich Evaluate Kit.

Bill of Materials


  1. Tuya Sandwich Wi-Fi MCU Board (E3S) ;

涂鸦三明治 WiFi MCU 通信板(E3S)正面.jpg

2、ST Nucleo Development Board;


3、Micro-USB cable with data transmission function.


Arduino IDE.

Project creation

As developers, we need to create projects to do 2 jobs first:

  • Implement the most basic communication functions between Tuya Sandwich Main control board and Tuya Sandwich Wi-Fi MCU Communication Board (E3S);

  • Writing Tuya Sandwich's specific DP functions according to their own needs.


In order to realize the communication between Tuya Sandwich Evaluate Kit and Tuya E3S Wi-Fi module, we need to use the SDK package generated by Tuya IoT platform according to the product.

Taking the realization of the product socket as an example, the steps to obtain the SDK development kit for the product socket are as follows:

Create product


Choose a development-free solution


Choose the function we need


Download MCU SDK


Downloaded file's directory is as follows: 企业微信截图_15816757319222.png

It includes a protocol file, Tuya serial port helper and its debugfile, and MCU SDK we need here:


When we perform the above operations, we have successfully obtained the SDK development kit. Next, we need to port the SDK development kit to our project.

MCU SDK package porting

Open Arduino IDE and save the new project.


At this time, there are two functions in our project:

setup () is generally used for initialization and executed only once.

loop () is executed in a loop.

All the following .C and .H files in the MCU development kit are copied to the newly created one. The same level directory under the sandwich project path, and change the suffix .C to .CPP . As shown below:


Close the Arduino IDE, click on the extension ending with Yourprojectname.ino and reopen to see the added files:


Click the check mark in the upper left corner to verify.

Fix error

After the first verification, an error message will appear. Follow the prompts to make corrections, and click Verify until there are no errors.

Possible errors are as follows:

Error 1:


Reason: Every #error in SDK is for reminding developer to edit the necessary parts of code.

Correction method: Add // before #error to make it in a comment state temporarily, click Verify, if successful, the next #error will be displayed.

Note: After verify successfully , please back to edit these necessary functions.

Error 2:


Reason: Compilation environment.

Correction method: If the compilation fails, a cast is required. You can find it in system.cpp file, the code reference is as follows:

static void product_info_update(void)
  unsigned char length = 0;
  length = set_wifi_uart_buffer(length,(unsigned char *)"{\"p\":\"", my_strlen((unsigned char *)"{\"p\":\""));
  length = set_wifi_uart_buffer(length,(unsigned char *)PRODUCT_KEY,my_strlen((unsigned char *)PRODUCT_KEY));
  length = set_wifi_uart_buffer(length,(unsigned char *) "\",\"v\":\"", my_strlen((unsigned char *)"\",\"v\":\""));
  length = set_wifi_uart_buffer(length,(unsigned char *)MCU_VER,my_strlen((unsigned char *)MCU_VER));
  length = set_wifi_uart_buffer(length,(unsigned char *)"\",\"m\":", my_strlen((unsigned char *)"\",\"m\":"));
  length = set_wifi_uart_buffer(length,(unsigned char *)CONFIG_MODE, my_strlen((unsigned char *)CONFIG_MODE));
  length = set_wifi_uart_buffer(length,(unsigned char *) "}", my_strlen((unsigned char *)"}"));
  wifi_uart_write_frame(PRODUCT_INFO_CMD, length);

Error 3:


Reason: Compilation environment.

Modification method: Remove followed const at the screenshots in the protocol.cpp and system.cpp files.



Error 4:


Reason: Repeated definition.

Correction method: To avoid this errors, remove the red box in the figure above.

After the error is resolved, compile and pass. We need to implement the serial port sending and receiving functions between the sandwich evaluation kit and the Wi-Fi module communication. For other porting details, please refer to MCU SDK Porting.

Function implementation of serial port

The serial port of the sandwich evaluation kit uses the Arduino serial port API. For instructions on using the Arduino API, developers can check on the Arduino website.

Refer to the following code to implement the serial port receiving function in the main file:

#include "wifi.h"
#include <SoftwareSerial.h>

SoftwareSerial mySerial(0, 1); // RX, TX
#define _SS_MAX_RX_BUFF 300
#define relay 10
int time_cnt = 0, cnt = 0, init_flag = 0;

void setup() {

  pinMode(relay, OUTPUT);   //init the output IO
  digitalWrite(relay, LOW);

  pinMode(PC13, INPUT);     //reset the button of wifi configration
  pinMode(8, OUTPUT);       //Wi-Fi configration light

  mySerial.begin(9600);     //init the serial port
  mySerial.println("myserial init successful!");
  Serial.begin(115200);     //PA3 RX   PA2 TX
  Serial.println("serial init successful!");


void loop() {
  if (init_flag == 0) {
    if (time_cnt % 6000 == 0) {
      time_cnt = 0;
      cnt ++;
    wifi_stat_led(&cnt);   //Wi-Fi statues process
  myserialEvent();      //receiving data
  key_scan();           //Wi-Fi reset button scan 


void myserialEvent() {
  if (mySerial.available()) {
    unsigned char ch = (unsigned char);

void key_scan(void)
  static char ap_ez_change = 0;
  unsigned char buttonState  = HIGH;
  buttonState = digitalRead(PC13);
  if (buttonState == LOW) {
    buttonState = digitalRead(PC13);
    if (buttonState == LOW) {
      init_flag = 0;
      switch (ap_ez_change) {
        case 0 :
        case 1 :
      ap_ez_change = !ap_ez_change;


void wifi_stat_led(int *cnt)
#define wifi_stat_led 8
  switch (mcu_get_wifi_work_state())
    case SMART_CONFIG_STATE:  //0x00
      init_flag = 0;
      if (*cnt == 2) {
        *cnt = 0;
      if (*cnt % 2 == 0)  //LED fast flashing
        digitalWrite(wifi_stat_led, LOW);
        digitalWrite(wifi_stat_led, HIGH);
    case AP_STATE:  //0x01
      init_flag = 0;
      if (*cnt >= 30) {
        *cnt = 0;
      if (*cnt  == 0)      //LED slowly flashing
        digitalWrite(wifi_stat_led, LOW);
      else if (*cnt == 15)
        digitalWrite(wifi_stat_led, HIGH);

    case WIFI_NOT_CONNECTED:  //0x02
      digitalWrite(wifi_stat_led, HIGH); //LED always lit
    case WIFI_CONNECTED:  //0x03
    case WIFI_CONN_CLOUD:  //0x04
      if ( 0 == init_flag )
        digitalWrite(wifi_stat_led, LOW);//LED off
        init_flag = 1;                  
        *cnt = 0;


      digitalWrite(wifi_stat_led, HIGH);

Transmit function rewrite:


Change to:

void uart_transmit_output(unsigned char value)
//  #error "Please fill in the MCU serial port send function and delete the line."
	extern SoftwareSerial mySerial;

Import Arduino corresponding library function header files:

  • Change the screenshot part from protocol.cpp file

企业微信截图_15816721221421.png To:

#include "wifi.h"
#include <SoftwareSerial.h>
#include "Arduino.h"App
  • The screenshot part from the wifi.h file


Add a line below:

#include "Arduino.h"

Product function implementation

After the serial port sending and receiving functions are implemented, developers need to implement the specific functions of the product. We generated the corresponding DP function functions from the SDK package downloaded by Tuya IoT platform. When creating the product, we selected the switch function, and the specific function was implemented in the protocol. c function.


For serial port related transplantation, please refer to 《MCU SDK Migration》 For detailed implementation details of the communication protocol, please refer to Tuya Cloud Universal Serial Port Access Protocol.

Download and debug


After the developer has written the application for the product, the following steps can be used to download the code to Tuya Sandwich Evaluate Kit .

1、Connect the sandwich evaluation kit to the computer and select the corresponding port;App

2、Click Upload.


Results display

After downloading the program to our evaluation kit through the above steps, the following page appears after downloading successfully:



The Tuya sandwich evaluation kit debugging can use the printf function to format and output the information we need to debug

1、Initialize the hardware serial port Serial.begin (115200); ;

2、Since the printf () function has been redirected to our serial port in Tuya Sandwich Evaluate Kit library, you can use the printf () function directly.


Download the program to the evaluation kit and power it on again. At this time, the Wi-Fi module is in EZ network configuration mode by default. Use Tuya Smart App for network configuration.

Network indicator:

Status of lightNetwork status
Fast flashingEZ network configuration status, waiting for network configuration App
Slow flashingAP network configuration status, waiting for network configuration
OffWIFI is configured and connected to the router
Always litConnected to the router and connected to the cloud

Networking steps:

1、 Open Tuya Smart, click icon;


2、 Add device;


Screenshot_20200214185728_TuyaSmart1.jpg Screenshot_20200214185752_TuyaSmart.jpg

3、 Wait for the network to complete;


4、 After the equipment network is successfully configured, we can control it through APP.


Tuya technical glossary See Explanation of Terminology.

Tuya serial port access protocol See Tuya Cloud Universal Serial Port Access Protocol.

MCU SDK migration on STM32 board SeeMCU SDK Migration.