• Stars
    star
    106
  • Rank 325,871 (Top 7 %)
  • Language
    C++
  • Created over 3 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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.

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:

More Repositories

1

tuya-home-assistant

Home Assistant integration for controlling Powered by Tuya (PBT) devices using Tuya Open API, maintained by the Home Assistant Community and Tuya Developer Team.
868
star
2

tuya-homebridge

Homebridge custom plugin for controlling Powered by Tuya (PBT) devices in HomeKit. This plugin is officially maintained by the Tuya Developer Team.
JavaScript
342
star
3

tuya-smart-life

Tuya Smart Life Home Assistant Integration
Python
338
star
4

tuya-iot-python-sdk

Tuya IoT Python SDK for Tuya Open API.
Python
127
star
5

medusa

Micro Frontends All-in-one Integrated Solution.
JavaScript
107
star
6

tuya-panel-kit

Comprehensive components for development with Tuya Panel.
JavaScript
83
star
7

tuya-iotos-embeded-sdk-multimedia

Tuya IotOS embeded SDK for developing IPC/NVR/doorbell etc.
73
star
8

tuya-connect-kit-for-mqtt-embedded-c

Provide core capabilities like device connection, uplink and downlink communication and OTA across platforms and operating systems.
C
64
star
9

tuya-panel-demo

Tuya IoT Panel demo provides a series of common template codes.
Shell
64
star
10

tuya-connector

tuya-connector helps you efficiently create cloud development projects regarding the OpenAPI or message subscription capabilities. You can put all the focus on business logic without taking care of server-side programming nor relational databases.
Java
59
star
11

tuya-iotos-embeded-sdk-wifi-ble-bk7231n

Tuya IoTOS Embeded SDK WiFi & BLE for BK7231N
C
47
star
12

tuya-home-android-sdk-sample-java

This sample is designed to help developers learn more about how to use the features of Tuya Smart Life App SDK for Android.
HTML
47
star
13

tuya-iotos-embeded-sdk-wifi-ble-bk7231t

TuyaOS Embedded SDK is designed to promote the development of smart products with BK7231T that enables communication over Wi-Fi and Bluetooth Low Energy (LE).
C
44
star
14

tuya-panel-sdk

Tuya Panel SDK is designed to promote the development of smart device panels.
TypeScript
38
star
15

tuya-connector-nodejs

nodejs sdk
TypeScript
38
star
16

tuya-home-android-sdk

Tuya Smart Life App SDK is designed to promote the development of apps with multiple smart device features, such as device pairing, device control, firmware updates, scheduled tasks, and smart scenes.
Java
37
star
17

tuya-connector-python

The tuya-connector-python SDK is designed to support open APIs and Pulsar messages provided by Tuya.
Python
36
star
18

tuya-saas-development-framework

A submodule repository for distributing Tuya SaaS Development framework source code. Use 'git clone --recurse-submodules [email protected]:tuya/tuya-saas-framework.git' to also clone all submodules.
35
star
19

tuya-home-android-sdk-sample-kotlin

This sample is designed to help developers learn more about how to use the features of Tuya Smart Life App SDK for Android.
Kotlin
32
star
20

tuya-home-ios-sdk-sample-swift

This sample demonstrates how to use Tuya Smart Home iOS SDK features in Swift.
Swift
31
star
21

tuya-ui-bizbundle-android-demo

Demo project of Tuya UI BizBundle that is a vertical service bundle encompassing logic modules and UI pages.
Java
30
star
22

tuya-iot-core-sdk

Tuya IoT Core SDK
C
23
star
23

tuya-home-ios-sdk

Tuya Smart Life App SDK is designed to promote the development of apps with multiple smart device features, such as device pairing, device control, firmware updates, scheduled tasks, and smart scenes.
Objective-C
22
star
24

tuya-open-sdk-for-device

Tuya open source IoT development framework for device
C
21
star
25

iot-server

Cloud-based all-in-one SaaS console application.
Java
20
star
26

tuya-smart-life-app-sdk

A submodule repository for distributing Tuya Smart Life App SDK Sample source code. Use 'git clone --recurse-submodules [email protected]:tuya/tuya-smart-life-app-sdk.git' to also clone all submodules.
18
star
27

tuya-iotos-embeded-multimedia-demo

demo codes for Tuya multimedia SDK applications
C
17
star
28

tuyaos-link-sdk-python

TuyaOS Link SDK for Python provides core IoT capabilities like device connection, upstream and downstream communication, and firmware OTA upgrade across chip platforms and operating systems.
Python
17
star
29

tuya-device-sharing-sdk

Tuya Device Sharing SDK
Python
16
star
30

connector

The connector framework maps cloud APIs to local APIs based on simple configurations and flexible extension mechanisms. You can subscribe to the distribution of cloud messages as local events. You can put all the focus on business logic without taking care of server-side programming nor relational databases. The OpenAPI or message subscription process is simplified, so you can focus on service logic and promote development efficiency.
Java
16
star
31

tuya-cloud-sdk-go

Tuya Cloud API SDK for go
Go
15
star
32

tuya-home-ios-sdk-sample-objc

This sample demonstrates how to use Tuya Smart Home iOS SDK features in Objective-C.
Objective-C
15
star
33

webrtc-demo-go

Tuya WebRTC Web Sample
JavaScript
12
star
34

tuya-connector-go

The tuya-connector-go helps you quickly set up projects regarding the open API and message subscription capabilities of Tuya Cloud Development. You can put all the focus on application development without taking care of server-side programming nor relational databases.
Go
12
star
35

tuya-iotos-beacon-sdk-ak80x

The Bluetooth protocol of Tuya IOT platform includes: Tuya BLE Protocol, SIG Mesh Protocol, Tuya Beacon Protocol. Compared with Tuya BLE Protocol and SIG Mesh Protocol, Tuya Beacon Protocol is very compact, It is suitable for porting to various ble beacon chips to realize SOC level low-cost applications.
C
11
star
36

tuya-bizbundle-ios-sdk-sample-objc

This sample demonstrates the use of Tuya iOS Biz Bundle SDK to build an IoT App from scratch.
Objective-C
10
star
37

tuya-panel-kit-docs

Docs site for tuya-panel-kit
TypeScript
10
star
38

tuya-cloud-sdk-nodejs

Tuya Cloud API SDK for node.js
JavaScript
10
star
39

tuya-connector-fe

Tuya SaaS Connector for Javascript
TypeScript
10
star
40

tuya-android-industry-app-sdk-sample

This sample provides examples of Tuya Industry App SDK basic functions such as device network configuration, login and registration, and asset management of Tuya Open API.
Java
9
star
41

TuyaPublicSpecs

Ruby
8
star
42

tuya-weapp-demo

Tuya weapp Software Development Demo
JavaScript
8
star
43

iot-portal

The frontend project of Tuya IoT Suite.
TypeScript
8
star
44

tuya-iotos-embedded-zigbee-gw-extend-sdk

Tuya Embedded Zigbee Gateway Extend Software Development Kit
7
star
45

tuya-pulsar-sdk-java

Tuya Pulsar SDK for java
Java
7
star
46

tuyasmart_android_saas_edge_ipc_demo

A demo for IPC Android application on edge.
Java
7
star
47

tuya-zigbee-mcu-sdk-arduino-library

Tuya Zigbee MCU SDK Arduino Library enables interfacing your Arduino with Tuya's network module, helping you build an IoT-enabled project.
C++
6
star
48

tuya-home-sdk-react-native

JavaScript
6
star
49

tuyaos-development-board-t2

tuyaos-development-board for t2
C
6
star
50

tuya-iotos-embeded-demo-wifi-ble-smart-planter

smart-planter
C
6
star
51

tuya-ble-sdk

Tuya BLE SDK implements the protocol to communicate with Tuya Smart App and provides event scheduling capabilities.
C
6
star
52

tuya-ios-industry-app-sdk-sample

This sample provides examples of Tuya Industry App SDK basic functions such as device network configuration, login and registration, and asset management of Tuya Open API.
Swift
6
star
53

tuya-panel-cli

CLI for Tuya Panel Developer
PowerShell
5
star
54

tuyalink-java-demo

Tuyalink Java Demo
Java
5
star
55

tuya-pulsar-sdk-go

Tuya Pulsar sdk for go
Go
5
star
56

tuya-ui-bizbundle-android-config-values

5
star
57

tuya-lighting-ios-sdk

The Tuya SmartLighting iOS SDK is an interface package that extends the TuyaSmartDevice with access to lighting device related features to speed up the development process.
Objective-C
5
star
58

tuya-iotos-embeded-demo-wifi-ble-environment-monitor

smart environment monitor
C
5
star
59

tuya-pts-sdk

C#
5
star
60

tuya-bsp-gpl-public-components

Tuya BSP GPL Public Components
C
5
star
61

tuya-iotos-embeded-mcu-demo-ble-smart-salty-spoon

This Demo uses the Tuya smart cloud platform, Tuya smart APP and IoTOS Embedded BLE MCU SDK to realize a salty spoon.MCU used by STC8G1K08A
C
5
star
62

tuya-smart-planter-sample-kotlin

This sample demonstrates how to use Tuya Smart Planter Sample in kotlin.
Kotlin
4
star
63

tuya-iotos-embedded-gw-tuyaos-integrated-sdk

TuyaOS all-in-one gateway SDK is designed for developing smart gateways with the support for connectivity protocols including Zigbee, Bluetooth mesh, RS-433, Z-Wave, and so on.
C
4
star
64

tuya-lighting-android-sdk

The Tuya Smart Lighting Android SDK is an interface package that extends the TuyaSmartDevice with access to lighting device related features to speed up the development process.
Kotlin
4
star
65

tuya_os_compile_script_on_windows

Python
4
star
66

tuya-central-control-android-sdk

3
star
67

tuya-edge-driver-sdk-go

The SDK is used to develop southbound device services connected to tuya edge gateway.
Go
3
star
68

tuya-iotos-android-ipc-demo

Tuya IPC Android demo is an example for using Tuya IPC Android SDK
Java
3
star
69

iot-suite-portal

Iot suite portal, master application, the whole project entry
TypeScript
3
star
70

tuya-iotos-embeded-demo-wifi-ble-smart-planter-lite

smart-planter-lite
C
3
star
71

tuya-pod-specs

A podspec repository for Tuya iOS Mobile SDKs.
Ruby
3
star
72

tuya-lock-ios-demo

TuyaSmartLockKit provides APIs for smart door locks.
Objective-C
2
star
73

iot-suite-portal-device

Iot suite portal, sub application, device management
TypeScript
2
star
74

tuya-iotos-android-iot-demo

Tuya IoT Android demo is an example for using Tuya IoT Android SDK
Java
2
star
75

tuya-community-android-sdk

This sample demonstrates how to use Tuya Community Android SDK features in Java.
Java
2
star
76

tuya-lock-android-demo

Tuya Lock Android SDK
Kotlin
2
star
77

iot-suite-portal-asset

Iot suite portal, sub application, asset management
TypeScript
2
star
78

tuyaos-iot-wifi-ble-bk7231n

TuyaOS networked product for bk7231n
Assembly
2
star
79

tuya-camera-ios-sdk

TuyaSmartCameraKit provides APIs for communicating with remote cameras.
2
star
80

tuya-iotos-embedded-gw-link-sdk

Tuya Embedded Gateway Link Software Development Kit
2
star
81

tuya-android-activator-sdk

TuyaSmartActivator supports four network configuration modes, quick connection mode (TLink, it is referred to as the EZ mode) and hotspot mode (AP mode), wired network configuration of zigbee gateway, BLE + Wi-Fi Dual-mode.
2
star
82

tuya-android-open-sdk

Tuya android open source sdk repo.
Java
2
star
83

tuya-ray-cra-template

This is the official TypeScript templates for Create Ray App
TypeScript
2
star
84

tuya-iotos-embeded-demo-ble-smart-kettle

A smart kettle demo based on Tuya Smart Cloud Platform, Tuya Smart APP, IoTOS Embeded Ble SDK, using Tuya BLE series modules.
C
2
star
85

tuya-iotos-embeded-demo-ble-sdk-development-course

Tuya IoTOS Embeded Demo BLE SDK Development Course
C
2
star
86

tuya-industry-app-sdk

Tuya IoT App SDK is an important part of the Tuya SaaS Development Framework product series. It provides basic functions such as device network configuration, device control, and asset management of Tuya Open API.
2
star
87

tuya-ble-mcu-sdk-arduino-library

Tuya Bluetooth Low Energy (LE) MCU SDK Arduino Library enables interfacing your Arduino with Tuya's network module, helping you build an IoT-enabled project.
C++
2
star
88

tuya-iotos-embeded-mcu-demo-wifi-ble-samrt-lawn-mower

A smart lawn mower demo based on Tuya Smart Cloud Platform, Tuya Smart App, IoTOS Embeded MCU SDK, using Tuya WiFi/WiFi+BLE series modules and GD32F4 series MCUs.
C
2
star
89

tuya-commercial-lighting-ui-bizbundle-android-sample

This sample repo demonstrates how to use UI Bizbundle of Tuya Commercial Lighting App SDK for Android in Java.
Java
2
star
90

tuya-residence-ios-sdk-sample

This sample demonstrates how to use Tuya Residence iOS SDK features in Objective C.
Objective-C
1
star
91

tuya-iotos-embeded-demo-wifi-wbru-air-purifier

This demo shows you how to retrofit an ordinary air purifier and make it IoT-enabled.
C
1
star
92

arduino-tuyaopen

Arduino core for the T2/T3/T5
C++
1
star
93

tuya-iotos-embeded-demo-ble-gesture-controller

This demo shows you how to make a Bluetooth gesture-based remote control.
C
1
star
94

.github

Tuya organization github readme
1
star
95

tuya-iotos-android-iot-p2p-demo

Tuya IoT&P2P Android demo is an example for using Tuya P2P Android SDK under IoT Android SDK
Java
1
star
96

tuya-bizbundle-ios-demo

Objective-C
1
star
97

tuya-rtc-camera-sdk-android

TuyaRTCCamera SDK is a comprehensive solution for audio and video based on WebRTC technology. The solution focuses on providing developers with a set of SDKs that can interact and control with IPC devices (Powered by Tuya)
Java
1
star
98

tuya-community-ios-sdk

This sample demonstrates how to use Smart Community App SDK features in Objective-C.
Objective-C
1
star
99

tuya-iotos-embedded-gw-router-sdk

Tuya Embedded Gateway Router Software Development Kit
1
star
100

tuya-commercial-lighting-android-config-values

This repo provides Android configuration files of display languages and UI theme for the Tuya Commercial Lighting UI Bizbundle.
1
star