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.
Preparations
- Downloading SDKs
ROMA LINK supports the standard Message Queue Telemetry Transport (MQTT) protocol. You can use the open-source Eclipse Paho MQTT Client to connect to ROMA LINK.
Download link: Eclipse Paho MQTT Client
- Downloading a device integration demo
Log in to the ROMA Connect console and choose LINK > Device Management. Click Download 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 on Device Management page of 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 ROMA LINK every 10 seconds. After the code runs, ROMA 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 caused by unstable networks or instance upgrade, add the device status detection and automatic reconnection mechanisms during device development.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot