Fast Access
This section uses the smoke detector demo to create a product model, configure and start the demo, and use basic SDK functions. For details about the demo source code, see SmokeDetector.cs. A smoke detector product model is provided to help you understand the product model. This smoke detector can report the smoke density, temperature, humidity, and smoke alarms, and execute the ring alarm command. The following uses the smoke detector as an example to introduce the procedures of message reporting and property reporting.
Creating a Product
A smoke detector product model is provided to help you understand the product model. This smoke detector can report the smoke density, temperature, humidity, and smoke alarms, and execute the ring alarm command. The following uses the smoke detector as an example to introduce the procedures of message reporting and property reporting.
- Access the IoTDA service page and click Access Console. Click the target instance card.
- Choose Products in the navigation pane and click .
Figure 1 Creating a product
- Set the parameters as prompted and click OK.
Basic Info
Resource Space
The platform automatically allocates the created product to the default resource space. You can also select an existing resource space from the drop-down list or create one.
Product Name
Customize the product name. The name can contain letters, numbers, underscores (_), and hyphens (-).
Protocol
Select MQTT.
Data Type
Select JSON.
Device Type Selection
Select Custom.
Device Type
Select smokeDetector.
Advanced Settings
Product ID
Leave this parameter blank.
Description
Set this parameter based on service requirements.
Figure 2 Creating a product - MQTT
Uploading a Product Model
- Download the smokeDetector product model file.
- Click the name of the created product to access its details page.
- On the Basic Information tab page, click Import from Local to upload the product model file obtained in 1.
Figure 3 Product - Uploading a product model
Registering a Device
- In the navigation pane, choose Devices > All Devices, and click Register Device.
- Set the parameters as prompted and click OK.
Parameter
Description
Resource Space
Ensure that the device and the created product belong to the same resource space.
Product
Select the created product.
Node ID
Customize a unique physical identifier for the device. The value consists of letters and digits.
Device Name
Customize the device name.
Authentication Type
Select Secret.
Secret
Customize the device secret. If this parameter is not set, the platform automatically generates a secret.
After the device is registered, save the node ID, device ID, and secret.
Accessing the Device
- Obtain the access address by choosing Overview > Access Details on the console.
Figure 4 Access information - MQTT access address on the device side
- Enter the MQTTS access address, device ID, and device secret of the device.
// Create a device and initialize it. IoTDevice device = new IoTDevice(mContext, "ssl://iot-mqtts.cn-north-4.myhuaweicloud.com:8883", "5eb4cd4049a5ab087d7d4861_demo", "secret"); device.init();
IoT platform provides two authentication modes: secret and certificate. If MQTTS is used, place the downloaded BKS certificate (renamed to DigiCertGlobalRootCA.bks) in the src/main/assets directory.
- Check the device running status. On the device details page of the console, check whether the device is online and the reported product model data.
Figure 5 Device list - Device online status
The SDK provides automatic reconnection by default. After the device is initialized, if the connection fails due to an unstable or unreachable network, or proactive disconnection by the platform, the SDK automatically initiates reconnection in the backend.
Reporting a Message
Message reporting is the process in which a device reports messages to the platform.
- Report messages to the platform with the reportDeviceMessage method in the example of iot-device-demo/java/com/huaweicloud/sdk/iot/device/demo/MainActivity.java.
DeviceMessage deviceMessage = new DeviceMessage(); deviceMessage.setContent("content"); deviceMessage.setId("id"); deviceMessage.setName("name"); Device.getDevice().getClient().reportDeviceMessage(deviceMessage); - On the IoTDA console, choose Devices > All Devices and check whether the device is online.
Figure 6 Device list - Device online status
- Select the device, click View, and enable message trace on the device details page.
Figure 7 Message tracing - Starting message tracing
- Check the message tracing result to see whether the platform has received messages from the device.
Figure 8 Message tracing - Viewing device_sdk_java tracing result
Message tracing may be delayed. If no data is displayed, wait for a while and refresh the page.
Reporting Properties
- A device can report current property values to the platform. For the code example, see iot-device-demo/java/com/huaweicloud/sdk/iot/device/demo/PropertyActivity.java.
Map<String, Object> properties = new HashMap<String, Object>(); properties.put("temperature", "28"); ServiceProperty serviceProperty = new ServiceProperty(); serviceProperty.setServiceId("smokeDetector"); serviceProperty.setProperties(properties); // Create properties. List<ServiceProperty> serviceProperties = new ArrayList<ServiceProperty>(); serviceProperties.add(serviceProperty); // Report properties. device.getClient().reportProperties(serviceProperties); - Run PropertyActivity.java and check the reported result. In the navigation pane, choose Devices > All Devices. Click the button to access the device details page, and check the reported property values on the Device Shadow tab page.
Figure 9 Device shadow - temperature
If no property value is reported, check whether the product model has been uploaded. For details, see Uploading a Product Model.
Delivering Commands
You can set a command listener to receive commands delivered by the platform. The callback API needs to process the commands and report responses.
- In the following code, the messageBroadcastReceiver function serves as the command listener. When a command is delivered, this function is called and the action value is IotDeviceIntent.ACTION_IOT_DEVICE_SYS_COMMANDS.
MessageBroadcastReceiver messageBroadcastReceiver = new MessageBroadcastReceiver(); LocalBroadcastManager.getInstance(this).registerReceiver(messageBroadcastReceiver, new IntentFilter(IotDeviceIntent.ACTION_IOT_DEVICE_SYS_COMMANDS));
- When the device receives a command, the messageBroadcastReceiver function is automatically called. For details about how to compile the messageBroadcastReceiver function, see iot-device-demo/java/com/huaweicloud/sdk/iot/device/demo/MessageActivity.java.
private class MessageBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "onReceive: " + intent.getAction()); if (IotDeviceIntent.ACTION_IOT_DEVICE_SYS_COMMANDS.equals(intent.getAction())) { requestId = intent.getStringExtra(BaseConstant.REQUEST_ID); Command command = intent.getParcelableExtra(BaseConstant.SYS_COMMANDS); edtLog.append("The command delivered by the platform is " + JsonUtil.convertObject2String(command)); } } }
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