Tuya MCU SDK Arduino Library is developed based on the Tuya Wi-Fi general integration solution

Overview

Tuya MCU SDK Arduino Library

English | 中文

Tuya MCU SDK Arduino Library is developed based on the Tuya Wi-Fi general integration solution. The device's MCU is connected to a Wi-Fi module through a serial port to implement a network connection. The development is based on general firmware, which supports the adaptative 9600 and115200 baud rate. Please read this document carefully before development.

Tuya Beta Test Program

Now welcome to join the Tuya Beta Test Program to get your development gifts and make your own arduino projects with Tuya Support. Your feedback is helpful and valuable to the whole community. image

Document introduction

├── config.h // Configuration file. Add and define features in the MCU SDK with macros.
├── examples // The folder to save routines.
├── keywords.txt
├── library.properties
├── README.md
└── src // The folder to save Tuya MCU SDK Arduino Library.
    ├── TuyaWifi.cpp // The APIs for users.
    ├── TuyaDataPoint.cpp // The class of DP operations.
    ├── TuyaDataPoint.h
    ├── TuyaDefs.h // Some constants.
    ├── TuyaWifi.h
    ├── TuyaTools.cpp // Tools used by the MCU SDK.
    ├── TuyaTools.h
    ├── TuyaUart.cpp // Functions for serial communications and data buffer.
    └── TuyaUart.h

Important functions

When you use this library for development with Arduino, you must add the header file TuyaWifi.h in your Arduino project.

1. Initialization

Every product that is created on the Tuya IoT Platform will have a unique product ID (PID). The PID is associated with all information related to this product, including specific DP, app control panel, and delivery information.

In unsigned char TuyaWifi::init(unsigned char *pid, unsigned char *mcu_ver), the PID is obtained after you create a product on the Tuya IoT Platform. The PID of a Wi-Fi product is typically 16 bytes. The mcu_ver parameter is the version number of the software. Pay attention to this parameter if you want to support OTA updates of the MCU.

Note: The current version of the library does not support the OTA feature.

#include <TuyaWifi.h>

TuyaWifi my_device;
...
void setup() 
{   
  Serial.begin(9600);
  ...
  my_device.init("xxxxxxxxxxxxxxxx", "1.0.0");// "xxxxxxxxxxxxxxxx": the PID on the Tuya IoT Platform. "1.0.0" is the default value. You can change "1.0.0" to the actual version number of the current software. 
              
  ...
}

void loop()
{
  ...
  my_device.uart_service();
  ...
}
...

2. Pass in the DP information to the MCU SDK

Create products on the Tuya IoT Platform and get information on product DP points.

A data point (DP) represents a smart device function.

  • Tuya abstracts each function into a data point. DPs are defined in different data types, such as Boolean, enumeration, and integer.
  • DPs have read and write attributes. For example, a 2-gang switch has two Boolean DPs, and each DP has either a True or False value, which is readable and writable.
  • To read means to get the current value of the switch, and to write means to change the current value of the switch.

DPID: specifies the ID of a DP event under a communication protocol.

The MCU SDK needs to know which DPs you have created and what type they are. Pass them to the MCU SDK through the void TuyaWifi::set_dp_cmd_total(unsigned char dp_cmd_array[][2], unsigned char dp_cmd_num) function. The Tuya IoT Platform has six types of DPs:

#define DP_TYPE_RAW     0x00    // Raw type
#define DP_TYPE_BOOL    0x01    // Boolean type
#define DP_TYPE_VALUE   0x02    // Numeric type
#define DP_TYPE_STRING  0x03    // String type
#define DP_TYPE_ENUM    0x04    // Enum type
#define DP_TYPE_BITMAP  0x05    // Fault type

In the void TuyaWifi::set_dp_cmd_total(unsigned char dp_cmd_array[][2], unsigned char dp_cmd_num) function, dp_cmd_array[][2] is the array that stores DP information, and dp_cmd_num is the total number of DPs.

Assume that a light has three functions, corresponding to three DPs as below:

  • Switch (DP ID: 20, DP type: Boolean type).
  • Brightness (DP ID: 21, DP type: numeric type).
  • Light mode (DP ID: 22, DP type: enum type).
#include <TuyaWifi.h>

TuyaWifi my_device;
...
#define DPID_SWITCH 20 // The switch DP of the light.
#define DPID_LIGHT 21 // The brightness DP of the light.
#define DPID_MODE 22 // The working mode DP of the light.
    
// Note: array[][0] is DP ID, and array[][1] is DP type.
unsigned char dp_id_array[][2] = {
    /*  DPID     |  DP type  */
    {DPID_SWITCH, DP_TYPE_BOOL},  
    {DPID_LIGHT, DP_TYPE_VALUE},
    {DPID_MODE, DP_TYPE_ENUM},
};
...
void setup() 
{
    ...
    my_device.set_dp_cmd_total(dp_id_array, 3);	
    ...
}

3. Pairing mode setting

Call void TuyaWifi::mcu_set_wifi_mode(unsigned char mode) to enter the pairing mode.

/**
 * @description: The MCU sets Wi-Fi working mode
 * @param {unsigned char} mode: Enter the specified mode 
 *                              0(SMART_CONFIG): Enter SmartConfig (Wi-Fi Easy Connect) mode
 *                              1(AP_CONFIG): Enter AP mode
 * @return {*}
 */
void TuyaWifi::mcu_set_wifi_mode(unsigned char mode);

4. Send and process DP data

After the cloud sends data, the sent data must be parsed through the unsigned char TuyaWifi::mcu_get_dp_download_data(unsigned char dpid, const unsigned char value[], unsigned short len) function. Currently, this function only supports three types: DP_TYPE_BOOL, DP_TYPE_VALUE, and DP_TYPE_ENUM. DP_TYPE_BITMAP refers to the data of fault type, in which the data is only reported to the cloud. You do not need to handle this type. DP_TYPE_RAW and DP_TYPE_STRING must be implemented yourself.

/**
 * @description: The MCU gets Boolean, numeric, and enum types to send DP value. (The data of the raw and string types shall be handled at the user's discretion. The data of the fault type can only be reported.)
 * @param {unsigned char} dpid: Data point (DP) ID
 * @param {const unsigned char} value: DP data buffer address
 * @param {unsigned short} len: Data length
 * @return {unsigned char} Parsed data
 */
unsigned char TuyaWifi::mcu_get_dp_download_data(unsigned char dpid, const unsigned char value[], unsigned short len);

5. Register a function to process DP sending

The app sends DP control commands to the device through the cloud. After data parsing, the device executes the specified actions accordingly.

A callback function is required to process the sent commands, so a processing function must be registered. We can call void TuyaWifi::dp_process_func_register(tuya_callback_dp_download _func) to register the callback function.

#include <TuyaWifi.h>

TuyaWifi my_device;
...

void setup() 
{
    ...
  // Register DP download processing callback function
  my_device.dp_process_func_register(dp_process);
    ...
}

/**
 * @description: DP download callback function.
 * @param {unsigned char} dpid
 * @param {const unsigned char} value
 * @param {unsigned short} length
 * @return {unsigned char}
 */
unsigned char dp_process(unsigned char dpid,const unsigned char value[], unsigned short length)
{
  switch(dpid) {
    case DPID_SWITCH:
      switch_value = my_device.mcu_get_dp_download_data(dpid, value, length);
      if (switch_value) {
        // Turn on 

      } else {
        // Turn off

      }
      // Status changes must be reported.
      my_device.mcu_dp_update(dpid, value, length);
    break;

    default:break;
  }
  return SUCCESS;
}

6. Report device status

Reporting the device status is to report the values of all DPs. It is also implemented through function registration.

Six data types of DPs are defined as follows:

DP reporting function:

/**
 * @description: DP data upload
 * @param {unsigned char} dpid
 * @param {const unsigned char} value
 * @param {unsigned short} length
 * @return {*}
 */
unsigned char mcu_dp_update(unsigned char dpid, const unsigned char value[], unsigned short len);//update raw, string type
unsigned char mcu_dp_update(unsigned char dpid, unsigned long value, unsigned short len);
unsigned char mcu_dp_update(unsigned char dpid, unsigned int value, unsigned short len);

Example of registering a device status reporting function

#include <TuyaWifi.h>

TuyaWifi my_device;

#define DPID_SWITCH 20
// Record the current status of the LED
unsigned char switch_value = 0;
...
void setup() 
{
    ...
  // Register DP download processing callback function
  my_device.dp_update_all_func_register(dp_update_all);
    ...
}

/**
 * @description: Upload all DP status of the current device.
 * @param {*}
 * @return {*}
 */
void dp_update_all(void)
{
  my_device.mcu_dp_update(DPID_SWITCH, switch_value, 1);
}

Technical Support

You can get support for Tuya by using the following methods:

Issues
  • dp_process callback stops after receiveing some Dreamlight scene mode raw data.

    dp_process callback stops after receiveing some Dreamlight scene mode raw data.

    I am trying to build a Tuya based individual addressable led strip. the data from the module is in raw format and it looks like this.

    Receiving module DP sends information           [2021/08/31 13:36:27.60]
    dpid: 51
    name: Dreamlight scene mode
    type: raw
    value: 01 03 00 00 00 80 00 00 64 00 F0 64 00 3D 64 00 00 64 00 AE 64 01 13 64 00 78 64 
    55 AA 00 06 00 1F 33 00 00 1B 01 03 00 00 00 80 00 00 64 00 F0 64 00 3D 64 00 00 64 00 AE 64 01 13 64 00 78 64 19
    
    

    After selecting some options like "Christmas eve" the dp_process callback stops working need to restart the mcu. But everything works on the Tuya debug assistant. the mcu not crashed all the processes only not receive any updates on dp_process. Not all of the Dreamlight scene causes this issue only few do. Please fix this bug as soon as possible. Thanks in advance.

    opened by REDHUN 7
  • Dev

    Dev

    1. modify part of the documentation description problem
    2. expand mcu serial buffer
    3. add the function of getting Green time and RTC time and demo
    4. add module self control function and demo
    opened by shiliu-yang 0
  • Tuya WBR1 Serial Port

    Tuya WBR1 Serial Port

    I just had the Tuya WBR1 module which connected with atmega2560. And the issue is I am not able to change the serial port available in the MCU to communicate with the module. If I use Serial. begin then the MCU and module will work fine. How can I change that to serial3.begin?

    opened by KPrasadA 2
  • Arduino WB3S Software Serial

    Arduino WB3S Software Serial

    Doing a few experiments with the WB3S module and Uno. How can I change default UART pins to, say, 6 and 7?

    Can I work with WB3S using AT commands or in any other way, than the ones from your lib?

    opened by ITstreet1 1
  • SDK crashes ESP8266 on APP connection process

    SDK crashes ESP8266 on APP connection process

    image

    SDK crashes ESP8266 on APP connection process.

    Steps to reproduce:

    1. system start
    2. check that heartbeats ok
    3. start connection request by button
    4. enter WLAN details in Tuya Smart APP
    5. ESP reboots
    opened by caipifrosch 1
  • Tuya module debugging assistant crashes MCU on Query Status

    Tuya module debugging assistant crashes MCU on Query Status

    ESP8266 crashes and reboots on sending "Query Status" from Tuya module debugging assistant.

    Tuya module debugging assistant is in "module simulation" mode at 9600 baud.

    ESP using latest Tuya arduino library available on GitHub.

    Heartbeats and other DP related commands seem ok.

    opened by caipifrosch 1
Releases(0.0.2)
Owner
Tuya
Open source AIoT projects and samples from Tuya Smart.
Tuya
Freeing the Silvercrest (Lidl/Tuya) Smart Home Gateway from the cloud.

free-your-silvercrest Freeing the Silvercrest (Lidl/Tuya) Smart Home Gateway from the cloud A collection of scripts/programs for freeing your Silvercr

null 104 Jun 21, 2022
This repo contains Direct3D 9, Direct3D 10, a few Direct3D 11, and DirectSound C++ samples from the legacy DirectX SDK updated to build using the Windows 10 SDK and the Microsoft.DXSDK.D3DX NuGet package

DirectX SDK Legacy Samples This repo contains Direct3D 9, Direct3D 10, a few Direct3D 11, and DirectSound samples that originally shipped in the legac

Chuck Walbourn 33 Jun 21, 2022
Enabling services on your device 66 Jun 23, 2022
Ultra fast and low latency asynchronous socket server & client C++ library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution

CppServer Ultra fast and low latency asynchronous socket server & client C++ library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and

Ivan Shynkarenka 848 Jun 26, 2022
A software C library designed to extract data attributes from network packets, server logs, and from structured events in general, in order to make them available for analysis

MMT-DPI A software C library desinged to extract data attributes from network packets, server logs, and from structured events in general, in odrder t

Montimage 3 Apr 14, 2022
The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.

Welcome! The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design

Microsoft 6.9k Jun 22, 2022
A Rust crate that simplifies the integration of Rust and eBPF programs written in C.

This crate simplifies the compilation of eBPF programs written in C integrating clang with Rust and the cargo build system with functions that can be

Simone Margaritelli 19 Mar 16, 2022
Mars is a cross-platform network component developed by WeChat.

Mars is a cross-platform infrastructure component developed by WeChat Mobile Team

Tencent 16.2k Jun 24, 2022
OceanBase Client for C. A driver to connect applications developed in C language to OceanBase Database.

Oceanbase Client for C OceanBase Client for C is a driver used to connect applications developed in C to OceanBase Database Server. Compatibility Serv

OceanBase 20 Jun 23, 2022
C++ Kite Connect API library / SDK

Kite Connect API C++ client Overview Dependencies Getting dependencies Linux Others & uWS v0.14 Building & installation Examples REST API Ticker Docum

Zerodha Technology 23 Jun 2, 2022
A modding SDK that allows you to interact with a modified version of Cocos 2DX v2.2.3 utilized by Geometry Dash

What is CappuccinoSDK CappucinoSDK is a modding utility that allows you to interact with a modified version of the game engine Cocos-2DX v2.2.3, which

null 16 Mar 27, 2022
Good Game, Peace Out Rollback Network SDK

(日本語ドキュメントはこちら) What's GGPO? Traditional techniques account for network transmission time by adding delay to a players input, resulting in a sluggish,

Tony Cannon 2.5k Jun 24, 2022
grey sdk

grey是龙游天下公司的产品"汉末霸业"的一个附属产品。开始是为了解决产品的扩展性,自由度,热更新,视频流播放,用户DIY,3d扩展,人工智能等功能而开发。后来把这些功能集成整理后,从而形成了一个完整的应用级解决方案。 grey是一套应用开发解决方案,使用类似c++的语言(cplus)进行开发。 让

grey-platform 51 Jun 18, 2022
MCUXpresso SDK

Overview MCUXpresso SDK is a comprehensive software enablement package designed to simplify and accelerate application development with Arm® Cortex®-M

NXP Micro 174 Jun 10, 2022
A modding SDK for Hitman 3

ZHM Mod SDK A modding SDK and mod loader for HITMAN 3. Description This is a community-made modding SDK and mod loader for HITMAN 3. Its purpose is to

Orfeas Zafeiris 35 Jun 6, 2022
Android and iOS SDK to display maps and geodata of swisstopo. Owner: simonroesch, Deputy: gjn

Open Swiss Maps SDK Android and iOS SDK to display maps and geodata of swisstopo Free map layers and geo data by swisstopo in your app Offer your mobi

geo.admin.ch 21 May 20, 2022
Firmware and SDK for the ER-301

ER-301 Sound Computer Table of Contents Introduction Emulator Compiling the emulator Running the emulator Configuring the emulator Installing packages

Orthogonal Devices 100 May 21, 2022
Teamspeak FriendSystem Plugin written in C/C++ (Teamspeak SDK)

Teamspeak-FriendSystem Teamspeak FriendSystem Plugin written in C/C++ (Teamspeak SDK) Install it into AppData / Roaming / Ts3Client / plugins Its only

null 2 Sep 24, 2021
Minecraft Windows 10 Bedrock SDK (1.17.30)

MCBE SDK Minecraft Windows 10 Bedrock SDK (1.17.30) Simple usage: cmake_minimum_required(VERSION 3.20) project(DummieProj VERSION 0.1.0) SET(CMAKE_CX

null 20 Jun 8, 2022