Configuring Device Integration
Scenarios
This section describes how to configure device integration for device access as well as message sending and receiving.
Device integration supports the standard MQTT protocol. You can use the open-source Eclipse paho MQTT Client to connect to LINK. In this example, the demo uses the Java SDK.
Prerequisites
- You have obtained the device access information. For details, see Preparations.
- You have installed the development tool and Java development environment. For details, see Preparations.
- Download the LINK Demo.
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.
Configuring Device Connection Information
- Decompress the demo package and find the DeviceConnectDemo.java file in the bottom-layer path of the src directory.
- 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 software packages on which the demo project depends are stored in the lib directory. When using the demo, you need to set the lib directory of the demo to the lib directory of the current project.
//Device connection address: tcp://ip:port final String host = ""; //Device client ID final String clientId = ""; // The example is used only for testing or illustration. The username for device authentication is sensitive. Do not hardcode it. final String userName = ""; // The example is used only for testing or illustration. The password for device authentication is sensitive. Do not hardcode it. 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
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.
public static void main(String[] args) { // The example is used only for testing or illustration. The AppKey for API authentication is sensitive. Do not hardcode it. String appKey = ""; //The example is used only for testing or illustration. The AppSecret for API authentication is sensitive. Do not hardcode it. 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.
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."
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