Help Center/ Intelligent EdgeFabric/ Best Practices/ Connecting to the MQTT Broker Using the Open-source C Language Library
Updated on 2024-09-19 GMT+08:00

Connecting to the MQTT Broker Using the Open-source C Language Library

Scenario

MQTT is a message protocol in publish/subscribe mode. It works even on remote devices with poor hardware performance and in poor network conditions.

This article describes how to connect and use IEF's built-in MQTT broker using an open-source C language library, Eclipse Paho C Client Library.

Procedure

  1. Prepare a Linux computer and download the source code.

    git clone https://github.com/eclipse/paho.mqtt.c.git

    You can also download the ZIP package from https://github.com/eclipse/paho.mqtt.c and decompress it.

  2. Install the compilation dependency tool.

    For Ubuntu, run the following commands:

    apt-get install build-essential gcc make cmake cmake-gui cmake-curses-gui

    apt-get install fakeroot fakeroot devscripts dh-make lsb-release

    apt-get install libssl-dev

    apt-get install ninja-build

    For CentOS, run the following commands:

    yum install build-essential gcc make cmake cmake-gui cmake-curses-gui

    yum install fakeroot fakeroot devscripts dh-make lsb-release

    yum install openssl-devel

    yum install ninja-build

  3. Modify the sample code.

    Add the following line to the src/samples/ paho_cs_pub.c file of the source code.

    ssl_opts.enableServerCertAuth =0;

    Add the following line to the src/samples/paho_cs_sub.c file.

  4. Compile the sample program.

    mkdir /tmp/build.paho

    cd /tmp/build.paho

    cmake -GNinja -DPAHO_BUILD_STATIC=TRUE -DPAHO_BUILD_SHARED=FALSE -DPAHO_WITH_SSL=TRUE -DPAHO_BUILD_SAMPLES=TRUE {paho.mqtt.c directory}

    ninja package

    {paho.mqtt.c directory} is the directory where the source code paho.mqtt.c is stored, for example, /root/work/paho.mqtt.c.

  5. Go to the directory where the compiled sample program is located, and copy the generated binary files paho_cs_pub and paho_cs_sub to the edge node.

    cd /tmp/build.paho/src/samples/

  6. Download the edge node certificate.

    1. Log in to the IEF console, choose Managed Resources > Edge Nodes in the navigation pane, and click an edge node name on the displayed page to view the edge node details page. Click the Certificates tab, and click Add Certificate.
      Figure 1 Adding a certificate
    2. In the displayed dialog box, enter the certificate name and click OK.
    3. Copy the downloaded certificate to the edge node and decompress it.

  7. Run the sample program.

    For example, to publish a message to a specified topic, run the following commands to view the paho_cs_pub command instructions.

    The following is an example of publishing a message:

    ./paho_cs_pub -c ssl://127.0.0.1:8883 -q 0 -m "xxx" -t "aaa" --cert /root/mqtt_cert/xOEMIsYVpw_private_cert.crt --key /root/mqtt_cert/xOEMIsYVpw_private_cert.key

    Running this command will send a message whose content is xxx to the aaa topic in the built-in MQTT broker. 127.0.0.1:8883 is the address of the built-in MQTT broker of the edge node. /root/mqtt_cert/xOEMIsYVpw_private_cert.crt and /root/mqtt_cert/xOEMIsYVpw_private_cert.key are the edge node certificates.