Device Integration Development
Overview
This section describes how to transmit messages with devices through LINK. Two parts are involved: configuring device connection information for a demo, and sending and receiving messages.
When MQTT devices are used, only QoS0 and QoS1 in MQTT are supported.
Preparations
- Downloading SDKs
LINK supports the standard Message Queue Telemetry Transport (MQTT) protocol. You can use the open-source Eclipse Paho MQTT Client to connect to LINK.
Download link: Eclipse Paho MQTT Client
- Download the LINK demo.
In this example, the demo uses a Java SDK. A demo contains two files. The DeviceConnectDemo.java file is used to connect to devices, and the DeviceControlDemo.java file is used to call APIs of control devices.
- Creating a product
For details, see Creating a Product.
- Creating a device
For details, see Registering a Device.
Configuring Device Connection Information of a Demo
- Log in to the ROMA Connect console and click View Console.
- In the navigation pane on the left, choose LINK > Device Management.
- On the Device Management page displayed, click the name of a created device to access the device details page and obtain device connection information.
You can edit the following device information: device connection address, device client ID, username, password, topics with the PUB permission, and topics with the SUB permission.
- Decompress the demo package and find the DeviceConnectDemo.java file under romalink_demo > src > com > demo > romalink.
- Use the Java editing tool to open the file and edit the device connection information. After the running is successful, you can view the status of the online device on the Device Management page.
The format of the device connection address is tcp://ip:port. Enter the device connection address in the correct format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//Device connection address final String host = ""; //Device client ID final String clientId = ""; //Username for device authentication final String userName = ""; //Password for device authentication final String password = ""; //Topic with the PUB permission final String pubTopic = ""; //Topic with the SUB permission final String subTopic = ""; //Content of the message sent by the device final String payload = "hello world.";
Sending and Receiving Messages
1 2 3 4 |
client.subscribe(subTopic, (s, mqttMessage) -> {
String recieveMsg = "Receive message from topic:" + s + "\n";
System.out.println(recieveMsg + new String(mqttMessage.getPayload(), StandardCharsets.UTF_8));
});
|
- Call APIs for sending control messages.
- Use the Java editor to open the DeviceControlDemo.java file and change the parameters of the API for sending control messages to the created device information.
Enter the following information: appKey, appSecret, device client ID, topic with the SUB permission, access address of the API for sending control messages, access port, and message content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
public static void main(String[] args) { //appKey used for API authentication String appKey = ""; //appSecret used for API authentication String appSecret = ""; //ID of the device client that needs to send control messages String clientId = ""; //Topic with the SUB permission of the device that needs to send control messages String subTopic = ""; //Access address of the API for sending control messages String host = ""; //Access port of the API for sending control messages String port = ""; //Content of the message to be sent to the device String payload = "hello world."; String url = "https://" + host + ":" + port + "/v1/devices/" + clientId; controlDevice(url, appKey, appSecret, clientId, subTopic, payload); }
- The values of appKey and appSecret can be obtained by clicking the name of the integration application to which the device belongs on the Integration Applications page of the ROMA Connect console and viewing the key and secret from basic information about the integration application.
- The port number is 7443. The values of clientId, subTopic, host, and port can be obtained by clicking the device name after you choose LINK > Device Management on the ROMA Connect console.
- Recompile and run the DeviceControlDemo class. If the device is connected and subscribes to a topic with the SUB permission, the device immediately receives a message and prints it on the IDE console. The request IP address of the API is the same as the IP address for connecting to the device, and the port number is 7443.
- Use the Java editor to open the DeviceControlDemo.java file and change the parameters of the API for sending control messages to the created device information.
- Send messages.
You can set the content and frequency of messages to be sent by a device. For example, you can instruct a device to send a message to LINK every 10 seconds. After the code runs, LINK receives a message every 10 seconds.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
try { final MqttClient client = new MqttClient(host, clientId); client.connect(mqttConnectOptions); System.out.println("Device connect success. client id is " + clientId + ", host is " + host); final MqttMessage message = new MqttMessage(); message.setQos(1); message.setRetained(false); message.setPayload(payload.getBytes(StandardCharsets.UTF_8)); Runnable pubTask = () -> { try { client.publish(pubTopic, message); } catch (MqttException e) { System.out.println(e.getMessage()); } }; client.subscribe(subTopic, (s, mqttMessage) -> { String recieveMsg = "Receive message from topic:" + s + "\n"; System.out.println(recieveMsg + new String(mqttMessage.getPayload(), StandardCharsets.UTF_8)); }); ScheduledExecutorService service = Executors .newSingleThreadScheduledExecutor(); service.scheduleAtFixedRate(pubTask, 0, 10, TimeUnit.SECONDS); }
- The Connect code simulates the function of connecting the MQTT.fx client to the device. After the connection is successful, the device displays "Connected."
- To prevent device disconnection due to network instability or instance upgrade, you are advised to add an automatic reconnection mechanism during device development. If the device demo provided by ROMA Connect is used, the reconnection mechanism is enabled by default. If the open-source MQTT client is used, you need to configure the reconnection mechanism based on the open-source code. If the connection is lost after the automatic reconnection function is enabled, the client keeps automatically reconnecting to the server until the connection is successful.
Last Article: Developer Guide for Device Integration
Next Article: MQTT Topic Specifications
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.