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.
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.
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.
Select a product category.
Choose TuyaLink for smart mode.
TuyaLink Solution is selected by default.
Complete the basic information about the product.
Under the Function Definition tab, click Add, select a function such as switch 1, and click OK.
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.
You can request free licenses or purchase a license to activate your device.
After you get the license, click Register Device to generate a device for debugging and verification.
After registration, the following information will appear:
Item | Example |
---|---|
RegistrationID | nFUDM2LnPFuL5jTrW*** |
ProductID | gmabzdwevsvlt*** |
DeviceID | 6cc87b39369b6fb754i*** |
DeviceSecret | ***************** |
With the ProductID
, DeviceID
, and DeviceSecret
, a device can report data to the cloud.
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.
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.
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.
When your code is ready to go, open the Tuya IoT Development Platform and choose Online Debugging in the development process.
(Optional) You can click the Online Debugging tab to directly navigate to the destination.
Select a device for debugging. You can search for the device you configured in the code by DeviceID
.
The connection status will appear. You can click Manual Refresh to get the latest status.
Right-click the TuyaMQTT3ClientDemo
file and choose Run to run the code.
The output window shows successful data reporting to the cloud.
Navigate to Device Debugging on the Tuya IoT Development Platform and check the data reported from the device.
This section describes how to implement cloud-to-device messaging.
Navigate to the page of Device Debugging.
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.
If the code runs correctly, the output in the IntelliJ IDEA should look like this.
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.
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback