English
English
简体中文
Contact Us
Register
Log In
Language
English
English
简体中文
Contact Us
Log In
Register
Go to main website
App Develop

App Development Platform

App development platform provides multiple development methods such as no-code or IoT App SDK development to maximize the monetization of IoT apps.

Connect Modbus Devices to Tuya IoT Cloud Based on Tuya Edge Gateway

Last Updated on2021-04-27 09:00:02

Overview

This tutorial describes how to activate a gateway, install a driver, and activate a device based on the Tuya Edge Gateway (TEdge). You can also use a simulated device to migrate data to the cloud and send commands from the cloud to control the device.

Device ecosystem

Materials

Hardware (1)Software (1)
  • Simulated or real Modbus device

    Count:1

    The Modbus protocol is a standard communication protocol in the industry sector. It provides the most popular connection channels between industrial electrical devices.more

Steps

  • Step 1: Prepare for the connection

    Install Docker

    You can follow the instructions in Get Docker to install Docker for your operating system. The following procedure describes how to install Docker on two operating systems:

    Create a directory and get Tuya’s Modbus driver

    1. Create a directory.

      sudo mkdir /var/tedge
      
    2. View the current user.

      ps aux | grep docker
      
    3. Grant the current user the directory permissions.

       sudo chown Username /var/tedge
      
    4. Create the required directories.

       sudo -u Username mkdir /var/tedge/res
       sudo -u Username mkdir /var/tedge/edgex-db-data
       sudo -u Username mkdir -p /var/tedge/edgex-driver-data/upload/library
       sudo -u Username mkdir /var/tedge/edgex-driver-data/run_config
      

    Start the Modbus simulated device

    1. Pull and install the modbus-simulator image.

       docker run --name "modbus-simulator" --restart=always \
       -p 1502:1502 \
       -d tuya/modbus-simulator:latest
      
    2. Get the IP address of the simulated device Docker.

       docker inspect --format '{{ .NetworkSettings.IPAddress }}' <Image ID>
      
  • Step 2: Deploy local services

    1. Deploy core services.

       docker run  --name "tuya-edgex" --restart=always \
       -p 58880:58880 \
       -v /var/tedge/edgex-db-data:/dbdata \
       -v /var/tedge/res:/res \
       -v /var/tedge/edgex-driver-data/run_config:/run_config \
       -v /var/tedge/edgex-driver-data/upload:/upload \
       -v /var/run/docker.sock:/var/run/docker.sock \
       -d tuya/tedge-server:v1.0
      
    2. Deploy frontend services.

       docker run --name "web-client" --restart=always \
       -p 3000:3000 \
       -d tuya/tedge-web:v1.0
      
    3. Download the official driver and package the files for installation (x86/ARMv7).

      Note:

      • The earlier files are overwritten after you run the Docker save command.
      • The files must be in the .tar format when they are saved with the Docker save command.
       docker pull tuya/tedge-modbus-driver:v1.0
      
       docker save tuya/tedge-modbus-driver:v1.0 > \
       /var/tedge/edgex-driver-data/upload/library/tedge-modbus-driver.tar		 
      
  • Step 3: Activate the edge gateway

    Create a gateway product on the Tuya IoT Platform

    You can create a smart product on the Tuya IoT Platform. This section describes how to create a gateway based on the low-code development solution. For more information, see Create Products.

    1. Log in to the Tuya IoT Platform.

      • On the Standard Category tab, choose Gateway Control > IoT Edge Gateway.

      • Enter the required information.

        Create a product

    2. Get a list of licenses.

      • Go to the Hardware Development tab.
      • Set Selected Cloud Connection Mode to Self-Developed Module SDK.
      • Enter information about the chipset platform, click OK, and then click Get 10 Free Licenses.
      • On the Purchase page, choose License List from the Delivery form drop-down list.
        Hardware development
      • In the left-side navigation pane, choose Purchase > Order > Purchase > Sample order.
      • Find the newly placed order and click Download License List to download a list of licenses.
        Download the license list

    Configure the project, assets, and user information for cloud development

    You can create a cloud development project and configure assets and user information for the project on the Cloud Development Platform. When you create a project, set Project Type to Industry Solutions. For more information, see:

    Activate the edge gateway

    1. Connect to the address IP address of the local host:3000 and log in to the locally deployed edge gateway.

    2. Activate the gateway.

      Activate the edge gateway

      • Gateway Product ID: the PID of the gateway product. It is generated when you create the product on the Tuya IoT Platform in Step 1.
      • UUID and AuthKey: You can check both field values from the list of licenses that are downloaded in Step 1.
  • Step 4: Manage the driver

    Add a Modbus driver

    1. Select the required driver file.
    2. Download the Modbus configuration template and upload the configuration file. You do not need to modify the template in this example.
      Add a driver

    Add a driver instance

    1. Search the driver library by name. Fuzzy matching is supported. Create a driver instance.

      Add a driver instance

    2. On the list of driver instances, click Stopped next to the newly created driver instance to run the driver.

      Run the driver instance

  • Step 5: Activate the device

    Create a sub-device

    1. Create a sub-device on the Tuya IoT Platform.

    2. On the Standard Category tab, choose Electrical > Socket > Custom Solution.

    3. Add the data points (DPs) Switch 1 and Countdown 1.

      Create a sub-device

    Add products to the edge gateway

    1. In the Add Product dialog box, enter the PID, product name, and product description that are used on the Tuya IoT Platform.

    2. Search the driver library by name, such as Modbus. Fuzzy matching is supported. Create a product.

      Create a product

    Add functions to the edge gateway

    1. Add both DPs that are specified on the Tuya IoT Platform to the edge gateway.
    2. Configure the Modbus information.
      • Switch 1
        Switch 1
      • Countdown 1
        Countdown 1

    Add a sub-device to the edge gateway

    1. Search the newly created socket by name in the Product Type field. Fuzzy matching is supported.

    2. Select Modbus from the Driver Instance drop-down list that is automatically loaded after you select a product type.

    3. Select the protocol type modbus-tcp.

    4. In the IP Address field, enter the Docker IP address of the simulated device. This IP address is generated in the Prepare for the connection section.

    5. Set Port to 1502.

    6. Set UnitID to the address of the simulated Modbus device.

    7. After the sub-device is added, click Activate Online to activate the device on the Tuya IoT Platform.

      Add a sub-device.png

  • Step 6: Debug APIs with API Explorer

    Go to the Cloud page of the Tuya IoT Platform, subscribe to the device control service, and then debug APIs.

    1. Choose API Products > Industry Project and subscribe to Industrial General Device Control. For more information, see Manage API Products.
    2. Choose Subscribed Products > Industrial General Device Control > Authorized Projects (Authorize) > New Authorization, and select your project in the dialog box that appears.
    3. Choose Cloud > API Explorer and send control commands. For more information, see API Explorer.

    Send cloud development commands

    1. Choose API Explorer > Control Device and set the device_id field. You can get the value of device_id on the Sub-devices page of Tuya Edge Gateway.
    2. Enter the commands. The following code block shows the sample code:
      [
      	{"code":"switch_1","value":true},
      	{"code":"countdown_1","value":318}
      ]
      
      API Expolorer

    View device logs

    Go to the Log query page of the Tuya IoT Platform and enter the device ID to view device logs.

    Device logs

    View sub-device status

    At the edge gateway, you can view the latest status of the sub-device for the edge gateway.

    View sub-device status

Related Tutorials