Application Development (TuyaLink)

Last Updated on : 2024-06-20 04:16:03download

This topic describes how to perform integrated application development by using TuyaLink-based devices that are connected to the Tuya Developer Platform.

Concepts

Cloud development

Cloud development is a one-stop development platform built by Tuya for smart solutions, providing fundamental products including smart device management, data analytics, rule engine, and more. It covers nine major industries such as real estate, industry, and energy. Furthermore, you can also develop with industry PaaS products including Smart Community, Smart Consumer Security, Smart Commercial Lighting, Smart Hotel, and Smart Apartment.

You can easily develop fundamental products with only a few lines of code, develop PaaS products on top of components, and select your desired industry products. Based on Tuya’s diversified functionality services, you can add industry products to various projects, perform resource management, and thus quickly implement a variety of industry SaaS. For more information, see Cloud Development.

Application Development (TuyaLink)

TuyaLink-based product

A TuyaLink-based product refers to a product created by using the TuyaLink smart mode. For more information, see Device Connectivity.

Application Development (TuyaLink)

Device binding code

The device binding code is a credential designed for TuyaLink-based devices that can be used to control device permissions. The bound object is granted permissions to control and get the device information. Two types of binding codes are provided to suit different scenarios.

  • QR code binding: Bind a device by scanning the QR code with the Device Management app. It is usually used for on-site construction.
  • Bind code: Bind a device with an API asset of a cloud development project. It is usually used in business system integration.

Each device can be bound with only one asset object. After the binding is successful, the device will be in the bound state. For the specific method, see the following sections.

Application Development (TuyaLink)

Permissions scope settings

By default, a device only supports binding with a cloud project under the current account. If you need to support binding projects under other IoT accounts, go to the My Products page, click Manage Product in the Operation column of the target product, and then click Set Permissions for Device Binding.

Application Development (TuyaLink)

Application Development (TuyaLink)

Device capabilities

Device capabilities refer to the northbound open API services provided by cloud development. Based on the device capabilities, application developers can implement data communication with devices, including device control and device message listener.

Capability type Source Definition
Standard capabilities Tuya’s official definition Standard capabilities indicate the capabilities to help achieve unified control across products and categories through Tuya’s standardized mapping and packaging. For example, on/off can be used as a standard capability. All devices with this capability can use the same set of parameters for unified control, bridging the hardware differences between products.
Original capabilities Auto-generated The unconverted hardware product definitions on the device model. For example, the R value of a colored light (data point or property), camera initialization (action), and device power-down alarm (event). Please note that: To control a device based on original capabilities, you need to first understand the definitions of hardware product models such as data point (DP) model or things data model, before you can correctly send device commands or parse messages.
Custom capabilities (coming soon) Custom The design and orchestration capabilities based on the device models, such as DP models or things data models. For example, a product has three properties: R, G, and B. For the convenience of operation, you can customize a capability control color and orchestrate the three properties into a method. You can request the method to control the color. Another example, a product can be used to measure weight and height. You can customize a capability get BMI. After you request the capability, the weight, height, and BMI are automatically returned.

Procedure

Core process

Application Development (TuyaLink)

Step 1: Create a project

A project is a collection of resources on the Tuya Developer Platform. Resources assigned to each cloud project are isolated from those for other cloud projects. Custom and Smart Home projects are available. You can follow the prompts to create the desired project. For more information, see Create a project.

Application Development (TuyaLink)

Step 2: Create an asset

Cloud development allows you to manage the devices and assign the device permissions by asset IDs. This topic takes Community A-North Gate as an example. For more information, see Create an asset.

Before creation, subscribe to the IoT Core service first.

Application Development (TuyaLink)

Step 3: Link (bind) a device

You can bind a TuyaLink-based device with an asset project by using the APIs or Device Management app. Before binding, make sure the product development has been completed and ready for release. The steps are as follows:

Method 1: Bind a device with an app

  1. On the Devices tab of a cloud development project, place the pointer over Add Device, and select Add Device with Device Management App. Download and install the app.

    Application Development (TuyaLink)

  2. On the Users tab of the project, add a user. This user account can be used to log in to the Device Management app.

    Application Development (TuyaLink)

  3. On the Assets tab, click Authorized Users > Add Authorization to authorize the user.

    Application Development (TuyaLink)

  4. Log in to the Device Management app, switch the asset to North Gate in the top left corner, tap Add, and then scan the binding QR code to add the device.

    Application Development (TuyaLink)

    After the binding is successful, you can view the device in Assets > Devices of the project.

    Application Development (TuyaLink)

Method 2: Bind a device on the platform

On the Assets tab of a project, click Add Device > Other to add a device.

Application Development (TuyaLink)

Application Development (TuyaLink)

Step 4: Subscribe to Pulsar messages

You can enable the message service to timely receive messages about the device, such as device registration, data reporting, and status change.

Only the Message Queue service is available currently. The platform provides push notifications of message accumulation via SMS or email. For more information, see Quick Start of Message Queue.

Enable the message service

Choose Cloud > Message Service, select a project, and then enable the message service.

Application Development (TuyaLink)

Application Development (TuyaLink)

Listener for device messages

For more information, see Get Push Messages by Pulsar (Java SDK).

Step 5: Northbound device control

Subscribe to the Device Northbound Service

On Tuya’s Value-Added Services (VAS) page, you can subscribe to the Device Northbound Service.

Application Development (TuyaLink)

Authorize the project

On the Service API tab of the project, click Go to Authorize, find the Device Northbound Service, and then authorize the project to use the service.

Application Development (TuyaLink)

Debug on the API Explorer

Click Debug in the Operation column of the device northbound service. The API Explorer will be opened for you to debug.

Application Development (TuyaLink)

Query device capability list

GET: /v1.0/iot-03/devices/{device_id}/capabilities-definition

The tag can be standard and original. For more information, see the API documents.

Application Development (TuyaLink)

Analyze the device capability list

{
  "result": {
    "capabilities": [
      {
        "capability_code": "switch_led",
        "methods": [
          "get",
          "post",
          "event"
        ],
        "name":"Light Switch",
        "request": {
          "description": "",
          "name":"Switch",
          "type": "boolean"
        },
        "response": {
          "description": "",
          "name":"Switch",
          "type": "boolean"
        },
        "tags": [
          "standard"
        ]
      },
      {
        "capability_code": "bright_value",
        "methods": [
          "get",
          "post",
          "event"
        ],
        "name":"Brightness value",
        "request": {
          "description": "",
          "max": 1000,
          "min": 10,
          "name":"Brightness value",
          "scale": 0,
          "step": 1,
          "type": "integer"
        },
        "response": {
          "description": "",
          "max": 1000,
          "min": 10,
          "name":"Brightness value",
          "scale": 0,
          "step": 1,
          "type": "integer"
        },
        "tags": [
          "standard"
        ]
      },
    ],
    "device_id": "vdevo159661074251***"
  },
  "success": true,
  "t": 1644561248369
}

By analytics of the returned device list definition, the following important fields are extracted:

  • capability_code: The capability code, which will be used when accessing the capability. For example, switch_led in the above example.
  • methods: The methods that are used to access the capability. - get: You can make API requests using the GET method. - post: You can make API requests using the POST method. - event: You can access the capability by subscribing to Pulsar messages.
  • request: The parameter description that is required when accessing the capability. As shown in the above example, the request parameter of switch_led is of Boolean type. You should enter true or false when accessing the capability.
  • response: A description of the return result.

Execute device capability

The analytics of the capability list indicates that this device has a capability whose capability_code is switch_led, and its request parameter is of Boolean type.

You can set parameters in the API Explorer to implement this capability.

POST: /v1.0/iot-03/devices/{device_id}/capabilities-definition

Application Development (TuyaLink)

Query device capability

It is similar to Execute device capability, except that the access method is changed from POST to GET.

Next step

For more information, see Device Connectivity.