MCU Quick Start

Last Updated on : 2020-04-25 07:19:01download

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

MCU Quick Start

2、ST Nucleo Development Board;

MCU Quick Start

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

MCU Quick Start

Choose a development-free solution

MCU Quick Start

Choose the function we need

MCU Quick Start

Download MCU SDK

MCU Quick Start

Downloaded file's directory is as follows: MCU Quick Start

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

MCU Quick Start

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.

MCU Quick Start

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:

MCU Quick Start

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

MCU Quick Start

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:

MCU Quick Start

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:

MCU Quick Start

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:

MCU Quick Start

Reason: Compilation environment.

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

MCU Quick Start

MCU Quick Start

Error 4:

MCU Quick Start

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:

MCU Quick Start

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

MCU Quick Start To:

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

MCU Quick Start

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.

MCU Quick Start

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.

MCU Quick Start

Results display

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

MCU Quick Start

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;

MCU Quick Start

2、 Add device;

MCU Quick Start

MCU Quick Start MCU Quick Start

3、 Wait for the network to complete;

MCU Quick Start

MCU Quick Start

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.

Your advice
Feedback effective questions, more chances to winsandwich development board and other small gifts
0/200
Your advice
Feedback effective questions, more chances to winsandwich development board and other small gifts
0/200