English
English
简体中文
Contact Us
Register
Log In

Quick Start

Last Updated on : 2022-12-25 09:22:49download

This topic walks you through the process of connecting your device to the Tuya IoT Development Platform and implementing bidirectional messaging between device and cloud.

Prerequisite

Register account

Create an account of the Tuya IoT Development Platform to manage your connected devices and access advanced features. Click here to register or log in to your account.

Create product

A product describes an abstract representation of a collection of physical devices, which is used to manage devices with the same capabilities. For example, if you want to have your Bluetooth treadmill connected to the Tuya IoT Development Platform for device monitoring, you can define it as a smart treadmill.

  1. Select a product category.

  2. Choose TuyaLink for smart mode.

    Quick Start
  3. TuyaLink Solution is selected by default.

    Quick Start
  4. Complete the basic information about the product.

    Quick Start

Define functions

Under the Function Definition tab, click Add, select a function such as switch 1, and click OK.

Quick Start

Develop device

Under the Device Development tab, choose Open Protocol and click Next.

Check out the development guide and determine the resources you want to develop with, either the Tuya MQTT Standard Protocol or the Tuya IoT Core SDK. This topic describes the Tuya MQTT Standard Protocol.

Quick Start

Activate and verify

  1. You can request free licenses or purchase a license to activate your device.

    Quick Start
  2. After you get the license, click Register Device to generate a device for debugging and verification.

    Quick Start
  3. After registration, the following information will appear:

    Item Example
    RegistrationID nFUDM2LnPFuL5jTrW***
    ProductID gmabzdwevsvlt***
    DeviceID 6cc87b39369b6fb754i***
    DeviceSecret *****************
    • The values in the above code snippet are for demonstration only. Replace them with your own.
    • This information is the unique credential to authenticate a device’s access to the Tuya IoT Development Platform over MQTT. Keep it safe.
    Quick Start

Device-to-cloud messaging

With the ProductID, DeviceID, and DeviceSecret, a device can report data to the cloud.

Device-to-cloud messaging over MQTT

Message Queuing Telemetry Transport (MQTT) is a lightweight, publish/subscribe messaging protocol based on TCP/IP developed in 1999 by IBM.

It allows two remote devices to communicate via messages asynchronously with a small code footprint and minimal network bandwidth. MQTT today is used in a wide variety of scenarios, such as IoT and mobile applications.

Tuya IoT Development Platform can connect to devices deployed globally and scale to meet the needs of your business.

This section describes how to use the Java code to connect a device to the Tuya IoT Development Platform and implement device-to-cloud messaging. The Java code is run on a physical host, which can represent a real device.

Download code: TuyaLink Demo

Unzip the downloaded file and import it to IntelliJ IDEA.

Sample code: TuyaMQTT3ClientDemo
Sign package: Includes the signature logic for MQTT connection.
Listener: Includes the MQTT listener.
Resources: Includes the certificate.

Quick Start

Device configuration

Go to the Tuya IoT Development Platform and get the ProductID, DeviceID, and DeviceSecret information. Make sure to replace the values of these three parameters with your own, and do not modify other code. The sample code has implemented the subscription to the topic used to report a property change.

 // TuyaLink device configuration is as follows, you must change it
 String productId = "dsadus***";
 String deviceId = "6cc87b393436fb754i***";
 String deviceSecret = "ffad8e******8c717";

The values in the above code snippet are for demonstration only. Replace them with your own.

Report device properties

Topic: tylink/${deviceId}/thing/property/report

{
	"msgId":"45lkj3551234***",
  	"time":1626197189638,
	"data":{
    	"switch_led_1":{
        	"value":true,
          	"time": 1626197189638  
        }
	}
}

Description

Parameters Type Description Required Notes
${deviceId} string The device ID. Yes The device that requests the device model.
version string Protocol version No The protocol version defaults to 1.0, which is the only valid value currently.
msgId string Message ID Yes A string up to 32 bits in length. The request and response are associated with a message ID.
time number Message timestamp Yes The Unix timestamp when a message is sent, in seconds (10-digit value) or milliseconds (13-digit value).
data object A collection of property values reported Yes key represents the property code.
value represents the property value and the timestamp when the property value is changed.
data.${key} object The property object Yes key represents the property code.
data.${key}.time number The timestamp when the property value is changed. Yes The Unix timestamp, in seconds (10-digit value) or milliseconds (13-digit value).
data.${key}.value object The property value reported Yes The specific property value.

Example:

 //****************************************device property report********************************************

 // Property report topic
 String topic = "tylink/" + deviceId + "/thing/property/report";
 // Current timestamp
 long timestamp = System.currentTimeMillis();
 // Property report content
 String content = "{\n" +
         "\t\"msgId\":\"45lkj3551234002\",\n" +
         "  \t\"time\":" + timestamp + ",\n" +
         "\t\"data\":{\n" +
         "    \t\"switch_led_1\":{\n" +
         "        \t\"value\":true,\n" +
         "        \t\"time\": " + timestamp + "  \n" +
         "        }\n" +
         "\t}\n" +
         "}";

 MqttMessage message = new MqttMessage(content.getBytes());
 message.setQos(1);
 sampleClient.publish(topic, message);
 System.out.println("publish topic: " + topic);
 System.out.println("publish content: " + content);

The value of the property field in the code must be identical to that defined on the Tuya IoT Development Platform.

Quick Start

Online debugging

  1. When your code is ready to go, open the Tuya IoT Development Platform and choose Online Debugging in the development process.

    Quick Start

    (Optional) You can click the Online Debugging tab to directly navigate to the destination.

    Quick Start
  2. Select a device for debugging. You can search for the device you configured in the code by DeviceID.

  3. The connection status will appear. You can click Manual Refresh to get the latest status.

    Quick Start
  4. Right-click the TuyaMQTT3ClientDemo file and choose Run to run the code.

    Quick Start

  5. The output window shows successful data reporting to the cloud.

    Quick Start

  6. Navigate to Device Debugging on the Tuya IoT Development Platform and check the data reported from the device.

Cloud-to-device messaging

This section describes how to implement cloud-to-device messaging.

Online debugging

Navigate to the page of Device Debugging.

Quick Start

Send property values to device

In Property Debugging, enter or change the value of a property and click Set. The cloud will send the desired property value to the device. The content sent to the device is printed in the log.

Verify on the device side

If the code runs correctly, the output in the IntelliJ IDEA should look like this.

Quick Start

Learn more

The sample Java code simulates the bidirectional commutation between the device and the cloud, which can help you quickly verify use cases. For more information, see Product Creation.