Help Center> IoT Device Access> Best Practices> Device Access> Connecting an OpenHarmony 3.0 Device to IoTDA
Updated on 2023-11-08 GMT+08:00

Connecting an OpenHarmony 3.0 Device to IoTDA

This topic uses BearPi-HM_Nano development board as an example to describe how to connect an OpenHarmony 3.0 device to IoTDA using the huaweicloud_iot_link SDK.

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.

Hardware Environment

You need to prepare a BearPi-HM_Nano development board, E53_IA1 expansion module, Type-C data cable, and PC.

Software Environment

Set up the environment in IDE-based mode or CLI-based mode. The BearPi-HM_Nano main control chip is Hi3861. Install the corresponding environment.

If you install gcc_riscv32 in the environment of the Hi3861 development board, download the gcc_riscv32 image. Otherwise, some plug-ins may fail to be downloaded or installed.

It may take a long time to download a large amount of open-source code. Therefore, reserve sufficient time.

Creating a Product

  1. Log in to the console, choose Products in the navigation pane, and click Create Product in the upper right corner.
  2. Set the parameters as prompted and click OK.

    Basic Information

    Resource Space

    The platform automatically allocates the created product to the default resource space. If you want to allocate the product to another resource space, select the resource space from the drop-down list. If a resource space does not exist, create it first.

    Product Name

    Customize the value. The name can contain letters, numbers, underscores (_), and hyphens (-).

    Protocol

    Select MQTT.

    Data Type

    Select JSON.

    Manufacturer

    Customize the value. The name can contain letters, numbers, underscores (_), and hyphens (-).

    Industry

    Set this parameter based on the site requirements. If the product model preset on the platform is used, set this parameter based on the industry to which the product model belongs. If not, select Default.

    Device Type

    Set this parameter based on the site requirements. If the product model preset on the platform is used, the device type is automatically matched.

    Advanced Settings

    Product ID

    Set a unique identifier for the product. If this parameter is specified, the platform uses the specified product ID. If this parameter is not specified, the platform allocates a product ID.

    Description

    Provide a description for the product. Set this parameter based on the site requirements.

    Figure 1 Creating a product

Developing a Product Model

  1. Click the created product. The product details page is displayed.
  2. On the Model Definition tab page of the product details page, upload a model file BearPi_Agriculture.zip.

    1. In the product list, click the name of a product to access its details. On the product details page displayed, you can view basic product information, such as the product ID, product name, device type, data format, manufacturer name, resource space, and protocol type. The product ID is automatically generated by the platform. Other information is defined by users during product creation.
    2. You can click Delete to delete a product that is no longer used. After the product is deleted, its resources such as the product models and codecs will be cleared. Exercise caution when deleting a product.

Registering a Device

  1. Log in to the IoTDA console.
  2. In the navigation pane, choose Devices > All Devices, click Individual Register in the upper right corner, set parameters based on the table below, and click OK.

    Figure 2 Registering an individual device

    Parameter

    Description

    Resource Space

    Select the resource space to which a device belongs.

    Product

    Select the product to which the device belongs. If no product is available, create a product first.

    Node ID

    Set this parameter to the IMEI, MAC address, or serial number of the device. If the device is not a physical one, set this parameter to a custom character string that contains letters and digits.

    Device Name

    Customize the device name.

    Authentication Type

    Select Secret.

    Secret

    Customize the secret used for device access. If the secret is left blank, the platform automatically generates a secret.

    Figure 3 Obtaining the device secret
    1. Save the device ID and secret. They are used for authentication when the device attempts to access IoTDA.
    2. If the secret is lost, you can reset the secret. The secret generated during device registration cannot be retrieved.
    3. You can delete a device that is no longer used from the device list. Deleted devices cannot be recovered. Exercise caution when performing this operation.

Using the Huaweicloud_iotlink SDK

  1. Download the source code oh3.0_hwcloud_iotlink-master.zip.
  2. Copy the preceding source code to the src > third_party directory of the OpenHarmony source code. Note that the third-party libraries of OpenHarmony and Huaweicloud_iotlink SDK use the OpenHarmony library files, such as cJSON and Mbed TLS.
  3. Add the following code to the OpenHarmony 3.0 source code file device\bearpi\bearpi_hm_nano\app\BUILD.gn and select the demo using the comment symbol (#).

      "//third_party/hwcloud_iotlink/demos/mqtt_tiny_v5_agriculture_demo:mqtt_tiny_v5_agriculture_demo",
    Figure 4 Selecting a demo

    2 in Figure 6 is the demo of connecting an MQTT device to Huawei Cloud. Check the BUILD.gn file, as shown in Figure 5. A contains the library files related to the development board hardware and Wi-Fi. B contains the library files required for connecting an MQTT device to Huawei Cloud, including cJSON, MQTT- and OSAL-related files, and configuration library files. C indicates that the hwcloud_iotlink library needs to be compiled when the file is compiled. Necessary libraries and C files of the file are located based on the specified path for compilation.

    Figure 5 Code compilation file
    Figure 6 Main function file of the demo

    A in Figure 6 shows the library files and DTLS library files required for connecting an MQTT device to Huawei Cloud. The link_main_task_entry() function must be called in the entrypoint function IoTMainTaskEntry() first to ensure the OSAL installation and initialization of other configurations.

  4. Set parameters.

    Figure 7 Modifying parameters

    (1) To connect to the cloud, you need to modify the network configuration information, Wi-Fi hotspot account and password, and device ID and key registered on the cloud based on your device. Note that this device supports only 2.4 GHz Wi-Fi.

    (2) Change the interconnection address to the MQTT access address displayed in the Access Details dialog box on the Overview page of the console.

    (3) Compilation and burning can be performed in IDE mode or CLI mode.

    (4) After burning, press the RESET button on the development board to start the device.

    (5) The preceding code is based on OpenHarmony 3.0. For other versions, modify the OpenHarmony source code path introduced in the BUILD.gn file as required.

Connecting the Device to the Platform

After the code is burnt to the device, restart the device. You need to restart the device twice for the first time. During the first burning, you may need to configure the internal information. After the device is restarted twice, it can connect to Huawei Cloud.

On the platform, you can view details about the reported data and deliver commands to control devices, as shown in the following figures.

Figure 8 Data reporting
Figure 9 Command delivery
Figure 10 Log information