This article describes how to use Link SDK for Java to connect devices with IoT Platform over MQTT.
Prerequisites
IoT Platform is activated.
You are not charged for activating IoT Platform. After you activate IoT Platform, you can enjoy free quotas. For more information, visit the IoT Platform free trial center.
Background information
You can configure the SDK to connect a simulated device with IoT Platform over MQTT. The device can submit data to IoT Platform by using custom topics.
Create a product and a device
Log on to the IoT Platform console.
In the left-side navigation pane, choose . On the page that appears, click Create Product.
On the Create Product page, enter a product name, such as NightLightSwitch. Use the default values for other parameters and click OK.
On the Create Product page, click Add under Add Device.
On the Devices page, click Add Device.
In the Add Device dialog box, enter a device name, such as LightSwitch. Then, click OK.
In the The devices have been added. dialog box, click Copy Device Certificate. Then, save the device certificate information on premises. For more information, see Device certificates.
Prepare the development environment
In this example, the development environment is Java 15 and the development tool is IntelliJ IDEA Community Edition 2020.1.
Install Java.
For more information, visit the Java website.
Install IntelliJ IDEA.
For more information, see IntelliJ IDEA.
Configure the device SDK
Download the sample code file to the development environment and decompress the package.
NoteBy downloading the package, you agree to the software license agreement.
Open IntelliJ IDEA, choose , navigate to the project file, and then click OK.
Open the following files in the project and set the parameters.
Set the following parameters to connect the device with IoT Platform.
File
Parameter
Example
Description
./device_id.jason
productKey
a18wP******
The device certificate information that is saved on premises after you add the device.
You can also view the information on the Device Details page of the IoT Platform console. For more information, see Obtain device authentication information.
deviceName
LightSwitch
deviceSecret
uwMTmVAMnGGHaAkqmeDY6cHxxB******
./src/main/java/com/aliyun/alink/devicesdk/demo/DeviceInfoData.java
region
cn-shanghai
The ID of the region in which the device is connected to IoT Platform. For more information, see Regions and zones.
./src/main/java/com/aliyun/alink/devicesdk/demo/HelloWorld.java
channelHost
"iot-06z00ax1o******.mqtt.iothub.aliyuncs.com:1883"
The value consists of an endpoint and a port number.
use an Enterprise Edition instance, or a public instance that is activated on July 30, 2021 or later, view the endpoint on the Instance Details page. Click View Development Configurations in the upper-right corner. On the Development Configurations panel, the endpoint is displayed. The value format is
"endpoint:1883"
.If you use a public instance that is activated before July 30, 2021, enter
pk ".iot-as-mqtt." + deviceInfoData.region + ".aliyuncs.com:1883"
.
1883
indicates the port number.Specify a topic that the device uses to send messages, such as
/a18wP******/LightSwitch/user/update
. Then, specify the message to be sent. Sample code:public void publish() { MqttPublishRequest request = new MqttPublishRequest(); request.topic = "/" + productKey + "/" + deviceName + "/user/update"; request.qos = 0; request.payloadObj = "{\\\"id\\\":\\\"1\\\",\\\"version\\\":\\\"1.0\\\",\\\"params\\\":{\\\"LightSwitch\\\":0}}"; LinkKit.getInstance().publish(request, new IConnectSendListener() { @Override public void onResponse(ARequest aRequest, AResponse aResponse) { // Publish the result. ALog.d(TAG, "onResponse " + (aResponse == null ? "" : aResponse.data)); } @Override public void onFailure(ARequest aRequest, AError aError) { // Failed to publish the result. ALog.d(TAG, "onFailure " + (aError == null ? "" : (aError.getCode() + aError.getMsg()))); } }); }
File
Parameter
Example
Description
./src/main/java/com/aliyun/alink/devicesdk/demo/MqttSample.java
topic
"/" + productKey + "/" + deviceName + "/user/update"
The topic that has the Publish permission. The device can send messages to IoT Platform by using this topic
You can view custom topics in the IoT Platform console. On the Product Details page, click the Topic Categories tab. On this tab, click Topic Category. For more information, see Topics.
The
user/get
section in the example is the suffix of the topic. You can send messages to a topic by specifying this section.In this example, the destination topic is
/a18wP******/LightSwitch/user/update
.a18wP******
indicates the ProductKey of the device.LightSwitch
indicates the DeviceName of the device.
payloadObj
{\"id\":\"1\",\"version\":\"1.0\",\"params\":{\"LightSwitch\":0}}
The message that is submitted to IoT Platform.
In this example, a custom topic is used. You can customize message formats.
Specify a topic such as
/a18wP******/LightSwitch/user/get
to receive messages from IoT Platform. Sample code:public void subscribe() { MqttSubscribeRequest request = new MqttSubscribeRequest(); request.topic = "/" + productKey + "/" + deviceName + "/user/get"; request.isSubscribe = true; LinkKit.getInstance().subscribe(request, new IConnectSubscribeListener() { @Override public void onSuccess() { // The subscription is successful. ALog.d(TAG, "onSuccess "); } @Override public void onFailure(AError aError) { // The subscription failed. ALog.d(TAG, "onFailure " + getError(aError)); } }); }
File
Parameter
Example
Description
./src/main/java/com/aliyun/alink/devicesdk/demo/MqttSample.java
topic
"/" + productKey + "/" + deviceName + "/user/get"
The topic that has the Subscribe permission The device can receive messages from IoT Platform by using this topic.
You can view custom topics in the IoT Platform console. On the Product Details page, click the Topic Categories tab. On this tab, click Topic Category. For more information, see Topics.
The
user/get
section in the example is the suffix of the topic. You can subscribe to a topic by specifying this section.In this example, the subscribed topic is
/a18wP******/LightSwitch/user/get
.a18wP******
indicates the ProductKey of the device.LightSwitch
indicates the DeviceName of the device.
Running result
After you configure the project file, click Run to run the ./demo/HelloWorld.java executable sample code file. You can view the running result on the device.
The following log data indicates that the
LightSwitch
device is connected to IoT Platform.2021-05-07 06:32:05.360 - 20/? D/ConnectManager:CommonNofity, onConnectStateChange(),state = CONNECTED
The following log data indicates that the
LightSwitch
device subscribes to the/a18wP******/LightSwitch/user/get
topic.2021-05-07 06:32:05.493 - null[MqttSendExecutor.java] - asyncSend(162):MqttSendExecutor:subscribe: topic: [ /a18wP******/LightSwitch/user/get ]
After you subscribe to a topic, you can return to IoT Platform. On the Topics tab of the Device Details page, find the topic and click Publish Notification in the Actions column. In the Publish Notification dialog box, enter a message, such as This is a test message from Alibaba Iot Platform.. Then, send the message from IoT Platform to the device.
The following log data indicates that the
LightSwitch
device uses the/a18wP******/LightSwitch/user/update
topic to send a message to IoT Platform.2021-05-07 06:32:05.488 - null[MqttSendExecutor.java] - asyncSend(129):MqttSendExecutor:publish: topic: [ /a18wP******/LightSwitch/user/update ] 2021-05-07 06:32:05.489 - null[MqttSendExecutor.java] - asyncSend(130):MqttSendExecutor:publish: payload: [ {\"id\":\"1\",\"version\":\"1.0\",\"params\":{\"LightSwitch\":0}} ]
You can go to the IoT Platform console to view the status and operation logs of the device.
In the left-side navigation pane, choose
. Then, find the device and view the online status. The value Online in the State column indicates that the device is connected to IoT Platform.In the left-side navigation pane, choose . Select the LightSwitch product to view the logs of the device that are generated when the device goes online, subscribes to the topic, and submits data to IoT Platform.
NoteYou can ignore the log data about over-the-air (OTA) updates, Thing Specification Language (TSL) models, and sub-devices. The data indicates the advanced features that are provided by the SDK and does not affect your experience.
What to do next
After the device is connected to IoT Platform, you can manage and monitor the device. For more information about the features of IoT Platform, see Features.