Contact Us
Log In

Extension SDK Solution

Last Updated on : 2022-11-24 09:20:02download

This topic describes the overall development process of the Tuya gateway extension SDK solution. For more information about the Tuya gateway solutions, see Smart Gateway.

Step 1: Set up development environment

This topic uses the desktop operating system of Linux distribution Ubuntu18.04 LTS to describe how to configure an embedded development environment and implement a wired gateway device running on an Ubuntu virtual machine.

  1. You can install Ubuntu18.04 LTS on a physical machine or virtual machine. VMware and VirtualBox are the popular virtualization software. For more information about software installation, see their official guide.

  2. The following lists dependency library and tools.

    $ sudo apt-get install lib32ncurses5 lib32z1 $ sudo apt-get install make-guile gcc g++ gawk vim git
  3. When virtualization software is installed, set the Connection mode to Bridge mode.

    Extension SDK Solution

Step 2: Create gateway product

See Create Products and create a gateway product on the Tuya IoT Platform. This section takes a Zigbee gateway as an example to describe product creation.

Note: Currently, Tuya IoT Platform does not distinguish gateways with the different networking methods. You can also create gateway products of wired networking through the above methods.

Extension SDK Solution

When you add functions, the standard functions displayed on the Tuya IoT Platform are based on the Tuya security gateway. If you are developing a common gateway, you can choose functions according to your specific needs.

Extension SDK Solution

Hardware debugging

During the Hardware Development phase, select a connection mode and module, download embedded system SDK, and get licenses.

  • Connection mode: Support Tuya Standard Module + MCU and Gateway SDK. For more information, see Smart Gateway.

    Gateway SDK: Select a toolchain and operating system according to the chipset platform. An SDK download link will be automatically generated. If you cannot find the required chipset platform, you can submit a technical ticket, providing the required SDK type, chip model, and operating system, and upload the toolchain package. Tuya operation personnel will handle the uploaded SDK.

    Extension SDK Solution

  • Module: You can select a network module based on the communication protocol. Multiple specification options are available. See the Module Details to get more information.

    The gateway firmware follows the module type. For example, the serial parameters for the Zigbee module are 115200, 8, N, 1, RTS, CTS.

  • PID: The unique Product ID (PID) is automatically generated on the Tuya IoT Platform after a product is created. PRODUCT_KEY macro is the identifier of a gateway product.

    Extension SDK Solution

  • License: After the gateway is created, you will get 10 device licenses. The license acts as the credential of the gateway’s connection to the Tuya IoT Platform and is used for development and debugging. It is represented by UUID and AUTHKEY macros in the SDK.

SDK demonstration

This section describes how to run the gateway SDK on the Ubuntu system.

Compile application

The extracted SDK directory is as follows:

tuya_zigbee_gw_ext_sdk ├── build_app.sh # Compile script ├── CHANGE_LOG.md # SDK version ├── demos │ ├── demo_gw # The sample program of the Zigbee gateway │ ├── demo_misc # The sample program demonstrates that the Zigbee gateway connects to non-Zigbee sub-devices │ └── demo_z3 # The sample program demonstrates that the Zigbee gateway connects to Zigbee sub-devices ├── Makefile # Compile all makefiles └── sdk ├── include # The header file of the SDK └── lib # The library file of the SDK
  1. Configure the cross-compilation toolchain. Modify the makefile and configure the path of the cross-compilation toolchain to the COMPILE_PREX variable. In the Ubuntu system, COMPILE_PREX defaults to the system’s gcc compiler, so you don’t need to modify the makefile.

  2. Run sh build_app.sh demo_gw. The compiled application is stored in output/demo_gw directory.

    tuya_zigbee_gw_ext_sdk$ sh build_app.sh demo_gw ...... compile done
  3. Set up a dynamic library path

    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/tuya_zigbee_gw_ext_sdk/sdk/lib

Connect to hardware

  1. As described in the overview of extension SDK solution, the Ubuntu system connects to the Tuya Zigbee module through a USB to UART converter. The following figure shows the TYZS4 module connection. For the connection of other modules, see their datasheet.

    Extension SDK Solution

    TYZS4 module silk screen USB to UART converter pin
    3.3V 3.3V
  2. Connect your PC to the Ubuntu virtual machine in the VMware.

  3. Check the serial device ID. As shown in the following commands, /dev/ttyUSB0 can be accessed by normal users. You add the user to the dialout user group.

    $ ls /dev/ttyUSB* -l $ sudo usermod -aG dialout user_name $ cat /etc/group |grep 'dialout'

    Extension SDK Solution

Compile and run dome code

  1. Modify the three macro definitions of UUID/AUTHKEY/PRODUCT_KEY.

    Extension SDK Solution

  2. Get the network interface name and serial device ID and modify the following code.

    Extension SDK Solution

    Extension SDK Solution

  3. Compile and run dome code.

    tuya_zigbee_gw_ext_sdk$ sh build_app.sh demo_gw ...... compile done tuya_zigbee_gw_ext_sdk$ ./output/demo_gw/user_iot [01-01 18:12:15 TUYA Info][kv_storge.c:43] Init Kvs With File:./ [01-01 18:12:15 TUYA Err][online_log_serv.c:280] log stats ufread fail. [01-01 18:12:15 TUYA Debug][gw_internal_api.c:273] BASE SDK INFO: < TUYA IoT SDK V:4.1.5 BS:30.05_PT:2.2_LAN:3.3_CAD:1.0.2_CD:1.0.0 > < BUILD AT:2020_03_06_10_25_02 BY luowl FOR zigbee_gw_ext_sdk_prj AT linux-ubuntu-6.2.0_64Bit > IoT DEFS < WIFI_GW:0 DEBUG:0 KV_FILE:1 SHUTDOWN_MODE:1 LITTLE_END:1 TLS_MODE:4 ENABLE_LOCAL_LINKAGE:1 ENABLE_CLOUD_OPERATION:1 ENABLE_SUBDEVICE:1 ENABLE_ENGINEER_TO_NORMAL:0 OPERATING_SYSTEM:100 ENABLE_SYS_RPC:0 TY_SECURITY_CHIP:0 RELIABLE_TRANSFER:1 ENABLE_LAN_ENCRYPTION:1 ENABLE_SIGMESH:0 > <TUYA GW SDK V:2.0.0 > GW SDK DEFS < WIFI_CONTROL_SUPPORT:0 > , GW SDK VERSION: 1.0.0 ......

Pair and add device

  1. Download the Tuya Smart app to your mobile phone.

  2. Make sure that your mobile phone and the Ubuntu virtual machine connect to the same local area network. Register and log in to the Tuya Smart app.

  3. Open the Tuya Smart app, click + icon in the top right corner to add a device. Switch to the Auto Scan to search for the gateway.

  4. Tap Next to pair the gateway.

Functional debugging

Document reference related to debugging is as follows:

  • Log query: You can query the device background log according to its device ID.

  • Technical ticket: Tuya provides online technical supports.