MCU Quick Start

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

Introduction

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

Hardware

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

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

2、ST Nucleo Development Board;

NUCLEOG071RB正面.jpg

3、Micro-USB cable with data transmission function.

Software

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.

Get SDK

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

微信截图_20200212181245.png

Choose a development-free solution

微信截图_20200212182741.png

Choose the function we need

微信截图_20200212182808.png

Download MCU SDK

微信截图_20200212182924.png

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:

4f327b19dbcff9c05ae6f2727b16600.png

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.

微信截图_20200212184556.png

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:

cf39370893ce6ae3dcaf6fb6a8d8439.png

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

企业微信截图_15816734077680.png

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:

企业微信截图_15816693633170.png

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:

企业微信截图_15816700212641.png

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:

企业微信截图_15816715329106.png

Reason: Compilation environment.

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

企业微信截图_15816717844034.png

企业微信截图_15816718107043.png

Error 4:

企业微信截图_1581672504653.png

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!");

  wifi_protocol_init();
}

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


}


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

void key_scan(void)
{
  static char ap_ez_change = 0;
  unsigned char buttonState  = HIGH;
  buttonState = digitalRead(PC13);
  if (buttonState == LOW) {
    delay(3000);
    buttonState = digitalRead(PC13);
      printf("------%d",buttonState);
    if (buttonState == LOW) {
      printf("123\r\n");
      init_flag = 0;
      switch (ap_ez_change) {
        case 0 :
          mcu_set_wifi_mode(SMART_CONFIG);
          break;
        case 1 :
          mcu_set_wifi_mode(AP_CONFIG);
          break;
        default:
          break;
      }
      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);
      }
      else
      {
        digitalWrite(wifi_stat_led, HIGH);
      }
      break;
    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);
      }
      break;

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

      break;

    default:
      digitalWrite(wifi_stat_led, HIGH);
      break;
  }
}

Transmit function rewrite:

企业微信截图_1581672886471.png

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;
	mySerial.write(value);                                
}

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
#ifdef WEATHER_ENABLE
  • The screenshot part from the wifi.h file

企业微信截图_15816721492245.png

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.

企业微信截图_15816730201595.png

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

Download

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.

企业微信截图_15816730933277.png

Results display

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

企业微信截图_15816749444711.png

Debug

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.

Distribution

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;

企业微信截图_15816779813590.png

2、 Add device;

企业微信截图_15816780064780.png

Screenshot_20200214185728_TuyaSmart1.jpg Screenshot_20200214185752_TuyaSmart.jpg

3、 Wait for the network to complete;

image.png

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

References

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.