Help Center> IoT Device Access> Best Practices> Device Access> Connecting and Debugging an NB-IoT Device Simulator
Updated on 2023-11-08 GMT+08:00

Connecting and Debugging an NB-IoT Device Simulator

Scenarios

This topic uses a smart street light as an example to describe how to use the device and application simulators provided by the IoTDA console to experience data reporting and command delivery.

Assume that:

A street light reports a data message carrying the light intensity (light_intensity) and status (light_status). The data message is in binary format. The command (SWITCH_LIGHT) can be used to remotely control the street light status.

Prerequisites

  • You have registered a Huawei Cloud account. If you have not registered, click here to complete the registration.
  • You have subscribed to the IoTDA service. If you have not subscribed to the service, go to the IoTDA service page, and click Access Console to subscribe to the service.

(Optional) Creating a Resource Space

A resource space is the commissioning space provided by the platform for applications and devices. You can create different resource spaces for different scenarios.

The system provides a preset resource space, where you can develop product models and codecs for devices online. To create a resource space, perform the following steps:

  1. Visit the IoTDA product page and click Access Console.
  2. In the navigation pane, choose Resource Spaces and click Create Resource Space in the upper right corner.
  3. In the displayed dialog box, specify Space Name and click OK.

Creating a Product

You can develop product models and codecs online. The platform provides device and application simulators for you to debug the product models and codecs.

  1. Visit the IoTDA product page and click Access Console.
  2. In the navigation pane, choose Products.

  3. Click Create Product in the upper right corner to create a product using LwM2M over CoAP. Set the parameters and click OK.

    Basic Information

    Resource Space

    Select the resource space to which the product to create belongs.

    Product Name

    Customize a name, for example, StreetLight.

    Protocol

    Select LwM2M over CoAP.

    Data Type

    Select Binary.

    NOTE:

    If Data Type is Binary, codec development is required for the product. If Data Type is JSON, codec development is not required.

    Manufacturer

    Customize the value.

    Industry

    Select Default.

    Device Type

    Enter streetlight.

Defining a Product Model

  1. Click the name of the product created in 3 to go to the product details page.
  2. On the Model Definition tab page, click Custom Model to add a service for the product.

    • Service ID: Enter StreetLight.
    • Service Type: You are advised to set this parameter to the same value as Service ID.
    • Description: Provide a description, for example, "Define the properties of light intensity and status."

  3. Click the ID of the service added in 2. On the displayed page, click Add Property to define a light intensity property collected by the street light.

    • Property Name: Enter light_intensity.
    • Data Type: Select Integer.
    • Access Permissions: Select Read and Write.
    • Value Range: Set it to 0–100 (light intensity range).

  4. Click Add Property to define a property as the status of the street light.

    • Property Name: Enter light_status.
    • Data Type: Select Integer.
    • Access Permissions: Select Read and Write.
    • Value Range: Set it to 0–1. 0 indicates that the light is turned off. 1 indicates that the light is turned on.

  5. Define the command used to remotely control the street light switch status.

    1. Click Add Command. In the dialog box displayed, set Command Name to SWITCH_LIGHT.
    2. Click Add Command Parameter. Set Parameter Name to SWITCH_LIGHT, Data Type to String, Length to 3, and Enumerated Value to ON,OFF. Click OK.
      Figure 1 Adding a command parameter
    3. Click Add Response Parameter. Set Parameter Name to result and Data Type to Integer.
    Figure 2 Adding a response parameter

  6. Click OK. The product model of the street light is created.

Developing a Codec Online

For purposes of power conservation, devices generally report binary data. The codec converts the binary data into JSON data based on properties defined in the product model so that the platform and application can identify the data. When an application remotely delivers a command, the platform converts the command in JSON format into binary and delivers the binary data to the device.

If a device reports data in JSON format, you do not need to define a codec.

  1. On the product details page of the street light, click the Codec Development tab and click Develop Codec.
  2. Click Add Message and configure a data reporting message as follows to report street light data:

    • Message Name: Enter LightData.
    • Message Type: Select Data reporting.
    • Add Response Field: Select this option. After a response field is added, the platform delivers the configured response data to the device when receiving data reported by the device.
    • Response: Retain the default value AAAA0000.

    1. Click Add Field to add the messageId field, which indicates the message type.

    2. Add the LightIntensity field to indicate the light intensity. Set Data Type to int8u (8-bit unsigned integer) and Length is 1.

    3. Add the LightStatus field to indicate the street light switch status. Set Data Type to int8u (8-bit unsigned integer) and Length is 1.

  3. Click Add Message again to develop a codec for command delivery messages.

    • Message Name: Enter SwitchStatus.
    • Message Type: Select Command delivery.
    • Add Response Field: Select this option. After a response field is added, the device reports the command execution result when receiving the command.

    1. Click Add Field to add the messageId field, which indicates the message type.

    2. Add the mid field to associate the delivered command with the command execution result.

    3. Add the SwitchStatus field. Set Data Type to string and Length is 3.

    4. Click Add Response Field to configure command delivery responses.
      • Add the messageId field to indicate the message type. The command execution result is an upstream message, which is differentiated from the data reporting message by the messageId field.

      • Add the mid field to associate the delivered command with the command execution result.

      • Add the errcode field to indicate the command execution status. 00 indicates success and 01 indicates failure. If this field is not carried, the command is executed successfully by default.

      • Add the result field to indicate the command execution result.

  4. Drag the property and command fields (defined in the product model) in the Product Model area on the right to map the fields in the data reporting and command delivery messages defined by the codec.

  5. Click Save in the upper right corner and click Deploy to complete codec deployment.

Debugging the Codec Online Using a Physical Device

Simulators debug the functions of devices and applications. You can debug the defined product model and codec by simulating data reporting and command delivery.

  1. On the product details page of the street light, click Online Debugging and click Add Test Device.
  2. In the dialog box displayed, set the parameters and click OK.

    • Device Type: Select Physical device.
    • Device Name: Customize a name.
    • Node ID: Enter the IMEI of the device. The node ID will be carried by the device for device access authentication. You can view the node ID on the NB-IoT module.
    • Registration Mode: Select Unencrypted. Note: If DTLS is used for device access, select Encrypted and keep the secret properly.

    The newly added device is in the inactive state. In this case, online debugging cannot be performed. For details, see Device Connection Authentication. After the device is connected to the platform, perform the debugging.

  3. Click Debug to access the debugging page.

  4. Simulate a scenario where a control command is remotely delivered. Specifically, under Application Simulator, select StreetLight for Service, SWITCH_LIGHT for Command, ON for SWITCH_LIGHT, and click Send. The street light is turned on.

Debugging the Codec Online Using a Virtual Device

  1. On the product details page of the street light, click Online Debugging and click Add Test Device.
  2. In the dialog box displayed, select Virtual device and click OK. The virtual device name contains Simulator. Only one virtual device can be created for each product.

  3. Click Debug to access the debugging page.

  4. Simulate a data reporting scenario. Specifically, under Device Simulator, enter the hexadecimal code stream 002000, and click Send. (The first byte 00 indicates the messageId, the second byte 20 indicates the light intensity, and the third byte 00 indicates the switch status.) Under Application Simulator, "Light_Intensity": 32, "Light_Status": 0 should be displayed.

  5. Simulate a command delivery scenario. Specifically, under Application Simulator, select StreetLight for Service, SWITCH_LIGHT for Command, ON for SWITCH_LIGHT, and click Send. Under Device Simulator, 0100014F4E should be displayed.

Debugging Using an Offline Simulator

The NB-IoT device simulator is used to simulate the access of NB-IoT devices (devices using LwM2M over CoAP) to the platform for data reporting and command delivery.

  1. Obtain the node ID and secret generated during device registration in 2.
  2. Download and decompress the NB-IoT device simulator, and double-click NB-IoTDeviceSimulator_zh.jar to run the simulator.

    • The simulator can run on Windows, but not macOS.
    • Ensure that the JDK has been installed. Otherwise, the JAR file cannot be executed.

    The package contains the following files:

    • NB-IoTDeviceSimulator_en.jar: simulator
    • Californium.properties: simulator configuration file
    • setting.properties: configuration file for connecting the simulator to the platform

  3. When a message is displayed requesting you to confirm whether to enable DTLS encrypted transmission, click No if a secret is not entered during device registration, or Yes if a secret is entered.

  4. Enter IP address, VerifyCode, and psk, and click Register Device to bind the simulator to the platform.

    Note: If DTLS encryption transmission is disabled, you do not need to enter a PSK.

    Set the three parameters as follows:
    • IP address: access domain name displayed on the IoTDA console. (You can also use the IP address. You can run the ping [domain name] command to obtain the IP address.)
    • VerifyCode: device ID, for example, aaaaa11111.
    • psk: secret set during device registration, for example, aaaaa11111aaaaa.

    On the IoTDA console, choose Devices > All Devices and view the device status. If the device is displayed as Online, the simulator is bound to the platform.

  5. Simulate a data reporting scenario. Specifically, under the NB-IoT device simulator, enter the hexadecimal code stream 001400, and click Send Data. (The first byte 00 indicates the messageId, the second byte 14 indicates the light intensity, and the third byte 00 indicates the switch status.)

    The data is reported successfully. You can return to the management console and view the latest reported data on the device details page of aaaaa11111. The latest reported data is "Light_Intensity": 20, "Light_Status": 0.

  6. Simulate the remote command delivery scenario. On the management console, locate the target product and click the product to go to the product details page. Click the Online Debugging tab and click Debug on the right of the aaaaa11111 device.

    Under Application Simulator, select StreetLight for Service, SWITCH_LIGHT for Command, ON for SWITCH_LIGHT, and click Send.

    The data delivered by the CIG is ON in the log area, and the simulator displays a message indicating that the downstream message from the platform is received and asking whether to respond. Click Yes. On the Application Simulator page of the IoTDA console, you can see that the command is in the Delivered state.

    Strings are parsed using ASCII in the codec. You need to deliver printable characters. Non-printable characters are not displayed in the simulator.