Example of Run Logs
Description
This section describes how to use the Java SDK to report messages, trigger run logs to be transferred to LTS, and view message reporting logs on the IoT platform page.
Development Environment Requirements
JDK 1.8 or later has been installed.
Prerequisites
1. The device has been registered with the IoT platform.
2. The run logs of the new version has been enabled and configured, and the device message log switch has been enabled.
Configuring the SDK on Devices
- Configure the Maven dependency of the SDK on devices.
<dependency> <groupId>com.huaweicloud</groupId> <artifactId>iot-device-sdk-java</artifactId> <version>1.1.4</version> </dependency>
- Configure the SDK and device connection parameters on devices. Note: Replace the domain name (domain), device ID (deviceId), and device secret (secret) in the actual code.
// Load the CA certificate of the IoT platform. For details about how to obtain the certificate, visit https://support.huaweicloud.com/intl/en-us/devg-iothub/iot_02_1004.html#section3. URL resource = BroadcastMessageSample.class.getClassLoader().getResource("ca.jks"); File file = new File(resource.getPath()); // The format is ssl://Domain name:Port number. // To obtain the domain name, log in to the Huawei Cloud IoTDA console. In the navigation pane, choose Overview and click Access Details in the Instance Information area. Select the access domain name corresponding to port 8883. String serverUrl = "ssl://{domain}:8883"; // Device ID created on the IoT platform String deviceId = "{deviceId}"; // Secret corresponding to the device ID String deviceSecret = "{secret}"; // Create a device. IoTDevice device = new IoTDevice(serverUrl, deviceId, deviceSecret, file); if (device.init() != 0) { return; }
- Report a message.
1 2 3 4 5 6 7 8 9 10 11
device.getClient().reportDeviceMessage(new DeviceMessage("hello"), new ActionListener() { @Override public void onSuccess(Object context) { log.info("reportDeviceMessage ok"); } @Override public void onFailure(Object context, Throwable var2) { log.error("reportDeviceMessagefail: "+ var2); } });
Testing and Verification
- Run the SDK code on the device. If the following information is displayed on the console, the device goes online and reports messages successfully.
2023-04-27 17:05:26 INFO MqttConnection:88 - Mqtt client connected. address :ssl://{domain}:8883 2023-04-27 17:05:26 INFO MqttConnection:214 - publish message topic = $oc/devices/{deviceId}/sys/messages/up, msg = {"name":null,"id":null,"content":"hello","object_device_id":null} 2023-04-27 17:05:26 INFO MessageSample:43 - reportDeviceMessage ok
- View run logs on the console. You can view the records of device login and logout and messages reported by devices.
Figure 1 Viewing run logs on the console
----End
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