Updated on 2024-03-28 GMT+08:00

Quick Experience Based on C Sample Code

Overview

This section describes how to connect a device to Huawei Cloud IoTDA through MQTTS/MQTT using C code, implement southbound data reporting and command delivery using platform APIs, and receive messages subscribed by the northbound server using the application-side sample code. Taking a smart street light as an example, the device reports information such as luminance to IoTDA, and an application receives device data pushed by IoTDA.

Prerequisites

You have installed Linux and GCC (4.8 or later).

Uploading a Product Model

A product model is a JSON file that describes device capabilities. It defines basic device properties and message formats for data reporting and command delivery. Defining a product model is to construct an abstract model of a device in the platform to enable the platform to understand the device function.

  1. Access the IoTDA service page and click Access Console.
  2. Choose Products in the navigation pane and click Create Product on the left.

    Figure 1 Creating a product

  3. In the displayed dialog box, set parameters based on your requirements.

    Figure 2 Creating an MQTT product

  4. Download the model file. For details about the development process, see Developing a Product Model Online.
  5. After the product is created, click the product, and then click Import from Local to upload the downloaded model file. The model file does not need to be decompressed, and the package name cannot contain brackets.

    Figure 3 Uploading an MQTT product model

Creating a Device

  1. In the navigation pane, choose Devices > All Devices, and click Individual Register.

    Figure 4 Registering a device

  2. In the displayed dialog box, configure the parameters by referring to the following figure (select the created product), and click OK. If you do not specify Secret, a secret will be automatically generated by the platform. In this example, the secret is automatically generated.

  3. After the device is created, save the device ID and secret, which will be used for device connection.

Importing Sample Code

  1. Download the sample code quickStart(C).
  2. Copy the code to the Linux running environment. The following figure shows the code file hierarchy.

    Description of the directories:

    • src: source code directory

      mqtt_c_demo: core source code of the demo

      util/string_util.c: utility resource file

    • conf: certificate directory

      rootcert.pem: certificate used by the device to verify the platform identity. It is used for login authentication when the device connects to the platform.

    • include: header files

      base: dependent Paho header files

      openssl: dependent OpenSSL header files

      util: header files of the dependent tool resources

    • lib: dependent library file

      libcrypto.so*/libssl.so*: OpenSSL library file

      libpaho-mqtt3as.so*: Paho library file

    • Makefile: Makefile

Compiling Library Files

  • Compiling the OpenSSL library
    1. Download OpenSSL, upload it to any directory on the Linux compiler, and run the following command to decompress it:
      tar -zxvf openssl-1.1.1d.tar.gz
    2. Generate a makefile.
      Run the following command to access the OpenSSL source code directory:
      cd openssl-1.1.1d
      Create a directory (for example, /home/test) for OpenSSL compilation.
      mkdir /home/test
      Create a directory for OpenSSL compilation.
      mkdir /home/test/openssl
      Create a configuration file directory.
      mkdir /home/test/openssl/ssl
      Run the following configuration command:
      ./config shared --prefix=/home/test/openssl --openssldir=/home/test/openssl/ssl

      In this command, prefix is the installation directory, openssldir is the configuration file directory, and shared is used to generate a dynamic-link library (.so library).

      If an exception occurs during the compilation, add no-asm to the configuration command (indicating that the assembly code is not used).

      ./config no-asm shared --prefix=/home/test/openssl --openssldir=/home/test/openssl/ssl

    3. Generate library files.

      Run the following command in the OpenSSL source code directory:

      make depend

      Run the following command for compilation:

      make

      Install OpenSSL.

      make install

      Find the lib directory in home/test/openssl under the OpenSSL installation directory.

      The library files libcrypto.so.1.1, libssl.so.1.1, libcrypto.so and libssl.so are generated. Copy these files to the lib folder of quickStart(C) and copy the content in /home/test/openssl/include/openssl to include/openssl of quickStart(C).

      Note: Some compilation tools are 32-bit. If these tools are used on a 64-bit Linux computer, delete -m64 from the makefile before the compilation.

  • Compiling the Eclipse Paho library file
    1. Download the paho.mqtt.c source code.
    2. Decompress the package and upload it to the Linux compiler.
    3. Modify the makefile.
      1. Run the following command to edit the makefile:
        vim Makefile
      2. Run the following command to display the number of lines:
        :set nu
      3. Add the following two lines (customized OpenSSL header files and library files) after line 129:
        CFLAGS += -I/home/test/openssl/include
        LDFLAGS += -L/home/test/openssl/lib -lrt

      4. Change the addresses in lines 195, 197, 199, and 201 to the corresponding addresses.

    4. Start the compilation.
      1. Run the following command:
        make clean
      2. Run the following command:
        make
    5. After the compilation is complete, you can view the libraries that are compiled in the build/output directory.

    6. Copy the Paho library file.

      Currently, only libpaho-mqtt3as is used in the SDK. Copy the libpaho-mqtt3as.so and libpaho-mqtt3as.so.1 files to the lib folder of quickStart(C). Go back to the Paho source code directory, and copy MQTTAsync.h, MQTTClient.h, MQTTClientPersistence.h, MQTTProperties.h, MQTTReasonCodes.h, and MQTTSubscribeOpts.h in the src directory to the include/base directory of quickStart(C).

Establishing a Connection

To connect a device or gateway to the platform, upload the device information to bind the device or gateway to the platform.

  1. Configure the parameters. Change the values of username and password only. For details, see Obtaining Resources.
  2. Start the connection.

    1. Run the make command for compilation. Delete -m64 from the makefile in a 32-bit OS.
    2. Run export LD_LIBRARY_PATH=./lib/ to load the library file.
    3. Run ./MQTT_Demo.o.

  3. If the connection is successful, the message "connect success" is displayed. The device is also displayed as Online on the console.

    Figure 5 Device online status

Reporting Properties

A device reports its properties to IoTDA. The sample code implements scheduled reporting. You can view the data reported by the device in IoTDA. For details, see Device Reporting Properties.

1
2
//publish data
char *payload = "{\"services\":[{\"service_id\":\"BasicData\",\"properties\":{\"luminance\":32},\"eventTime\":NULL}]}";
  • The message body payload is assembled in JSON format, and service_id must be the same as that defined in the product model. properties indicates a device property.
  • luminance indicates the street light brightness.
  • eventTime indicates the UTC time when the device reports data. If this parameter is not specified, the system time is used by default.

If the property reporting is successful, the message "publish success" is displayed in the demo.

The reported properties are displayed on the device details page.

Receiving a Command

After subscribing to a command topic, you can deliver a synchronous command on the console. For details, see Synchronous Command Delivery to an Individual MQTT Device.

If the command delivery is successful, the command received is displayed in the demo:

Obtaining Data Reported by a Device from the Cloud

After the platform receives, an application can receive push messages using AMQP. For details, see Obtaining Data Reported by a Device from the Cloud.

Additional Information

For more development guides, see Using IoT Device SDKs for Access and Using MQTT Demos for Access.