IoT Core Device Management

Last Updated on : 2023-08-08 06:21:35download

This topic takes a virtual smart campus project as an example and describes how to perform device control and device management by calling the new IoT Core OpenAPI. By calling the essential APIs of Tuya IoT Core, you can implement a wide array of operations. For example, classify devices by space, manage bulk devices in groups, set device properties and scheduled tasks, and trigger scene linkages. This practice applies to the development environments IntelliJ IDEA and API Explorer.

Scenario analytics

This topic simulates the remote management of various smart devices in a virtual smart campus, and describes how to manage and operate devices. In the following example, five types of open APIs provided by IoT Core are supported: space, device, group, linkage, and timer.

Sample scenario

The figure below shows the spatial structure of a smart campus building. The entire building has three floors, and each floor has three classrooms and an activity room or a meeting room.

IoT Core Device Management

Device list

The following table lists the devices managed in this example. You can add devices as you want.

SpaceDeviceQuantitySpaceDeviceQuantitySpaceDeviceQuantity
Meeting roomSmart light4ClassroomSmart switch1Activity roomSmart switch1
Smart lock1Smart light4Large light2
Central air conditioner1Smart lock1Smart lock2
Smart projector1Central air conditioner1Ambient light10
Smart curtain4Smart projector1Smart speaker1
Human presence sensor1Smart camera1Central air conditioner1
Sound sensor1Smart curtain4Smart LCD1
Temperature & humidity sensor1Human presence sensor4Smart camera1
Common deviceZigbee gateway2Sound sensor1---
Wireless router2Temperature & humidity sensor4---
---Smart LCD4---

Procedure

The following section takes a virtual case as an example and walks you through the whole procedure.

IoT Core Device Management

The procedure is divided into three steps. If you have created a cloud project and added devices to the project, you can proceed to Step 3: Control devices.

Step 1: Create a cloud project

  1. Log in to the Tuya IoT Development Platform.
  2. In the left-side navigation bar, choose Cloud > Development.
  3. On the page of My Cloud Projects, click Create Cloud Project.
  4. On the Create Cloud Project window, configure Project Name, Description, Industry, Development Method, and Data Center. From the Development Method drop-down list, select Custom. This sample project is named Smart Campus Building.
  5. On the Authorize API Services page, subscribe to the preferred API services and click Authorize.

For more information about cloud projects, see Manage Projects.

Step 2: Add devices

The cloud project supports multiple ways to add devices. You can choose the desired way to add devices in different scenarios.

This practice describes how to add a virtual device to show the features. For a real project, you can choose the development method that best suits your requirements.

  1. Link your project with a Tuya-enabled app account.

    1. Choose Devices > Link Tuya App Account > Add App Account.

      IoT Core Device Management
    2. Scan the QR code with the Smart Life app that you have logged in. After authorization, you will see all devices under the authorized account in the All Devices tab.

  2. Add a virtual device to your Tuya-enabled app account.

    1. Choose Devices > All Devices > Add Device > Add Virtual Device.

      IoT Core Device Management
    2. On the page of Add Virtual Devices, select desired devices and click Add Virtual Devices.

      IoT Core Device Management
    3. On the page that appears, click the tab Add to Tuya App Account, scan the QR code with the Tuya-enabled app that is authorized in the previous steps, and then add devices as prompted. You can find the added devices in the list of all devices of the current cloud development project.

      IoT Core Device Management

Step 3: Control devices

Prerequisites

  • Development environment: IntelliJ IDEA

  • Development tool: Tuya-connector Java SDK.

    Tuya-connector Java SDK helps you efficiently create cloud development projects regarding the OpenAPI or message subscription capabilities (cloud-to-cloud integration). You can put all the focus on business logic. For more information, see Develop with Java SDK.

  • Demo project: tuya-campus-demo. You can import this demo to try out the whole process.

  • Debugging tool: Tuya’s API Explorer.

Create a space

Based on the Space structure diagram of smart campus building, you can:

  1. Create a structure including the first to third floors of the teaching building.

  2. Initialize three classrooms and an activity room on the first floor. During creation, you need to use APIs related to space.

    • The initialization APIs are included in the tuya-campus-demo sample code. Thus, you can make API requests for initialization directly with your developer account.

      GET http://127.0.0.1:8080/campus/init?removeAllSpace=false
      
    • APIs

      Create a space.

      POST:/v2.0/cloud/space/creation
      
    • Demo snippet

      IoT Core Device Management
    • Debugging example

      IoT Core Device Management
  3. Note down the generated space numbers, which will be used in the space-based device management process. This way, you do not need to query the space APIs again.

    Building: 157536431

    • Floor 1: 157535642
      • Classroom 101: 157535643
      • Classroom 102: 157536432
      • Classroom 103: 157536433
      • Activity room: 157536434
      • Public utilities: 157536435
    • Floor 2: 157536436
    • Floor 3: 157535644

Transfer devices

According to the device list of each space, you can allocate the added devices to specific spaces as planned. For example, in Classroom 101, you need to allocate one smart switch, one door lock, four lights, two air conditioners, smart curtains, and other devices. You can allocate devices to spaces as follows:

  • APIs

    Transfer devices.

    POST:/v2.0/cloud/thing/{device_id}/transfer
    
  • Demo snippet

    IoT Core Device Management IoT Core Device Management
  • Debugging example

    IoT Core Device Management

Query devices in a space

  • APIs

    Query the list of resources in the specified space.

    GET:/v2.0/cloud/space/{space_id}/resource
    
  • Debugging example

    You can request the listSpaceResources API to view all devices in the specified space.

    IoT Core Device Management

Create a group

  • APIs:

    Create a group.

    POST: /v2.0/cloud/thing/group
    
  • Demo snippet

    IoT Core Device Management IoT Core Device Management
  • Debugging example

    IoT Core Device Management

Create linkage rules

  • APIs

    Create linkage rules.

    POST: /v2.0/cloud/scene/rule
    
  • Demo snippet

    Based on the previously created group, create linkage rules. They will be automatically triggered when certain conditions are met.
    IoT Core Device Management

  • Debugging example

    IoT Core Device Management

    As shown in the above process, the lighting group 13069655 is created in the example, and the rule FF8TxADHXdoMmCjd is configured. The lights in this group are automatically turned on every afternoon from Monday to Friday. You can view the real-time execution of the devices in the group by data reporting logs or device properties.

Query device logs

  • APIs

    Get operation logs of the specified device.

    GET: /v2.0/cloud/thing/{device_id}/logs
    
  • Debugging example

    IoT Core Device Management

Query device properties

  • APIs

    Get the property values of the specified device.

    GET: /v2.0/cloud/thing/{device_id}/shadow/properties
    
  • Debugging example

    IoT Core Device Management

Trigger a group rule

When creating a linkage rule, you can enable the manual trigger type. Then, you can make API requests to trigger a group rule.

  • APIs

    Trigger the specified rule for a scene.

    POST: /v2.0/cloud/scene/rule/{rule_id}/actions/trigger
    
  • Demo snippet

    IoT Core Device Management IoT Core Device Management
  • Debugging example:

    IoT Core Device Management

Create a scheduled task

  • APIs

    Create a scheduled task for the specified device.

    POST:/v2.0/cloud/timer/device/{device_id}
    
  • Demo snippet

    IoT Core Device Management IoT Core Device Management
  • Debugging example

    IoT Core Device Management

Modify a scheduled task

  • APIs

    Modify the state of the specified scheduled task.

    PUT:/v2.0/cloud/timer/{timer_id}/state
    
  • Debugging example

    IoT Core Device Management

Delete a scheduled task

  • APIs

    Delete a certain scheduled task for the specified device.

    DELETE: /v2.0/cloud/timer/device/{device_id}
    
  • Debugging example

    IoT Core Device Management

Except for the APIs demonstrated above, other IoT Core v2.0 APIs have been implemented in tuya-campus-demo. You can download and import it into a local project for testing.

Reference

For more information, see IoT Core.