All Products
Search
Document Center

IoT Platform:Use the Paho MQTT for C library to connect a device to IoT Platform

Last Updated:Apr 24, 2023

This topic describes how to use the open source Paho MQTT project for C to connect a device to IoT Platform and enable messaging.

Usage notes

In this topic, the permissions of a common user are used to perform all operations. If you want to perform specific operations that require administrator permissions, run the sudo command.

Prerequisites

A product and a device are created in an instance in the IoT Platform console. The device certificate and an MQTT endpoint are obtained. The certificate information includes the ProductKey, DeviceName, and DeviceSecret. For more information, see the following topics:

Prepare a development environment

In this example, Ubuntu 16.04-LTS is used as a development environment. Run the following commands to build the development environment:

sudo apt-get update
sudo apt-get install build-essential git sed cmake

Download the Paho MQTT library for C

  1. Run the following command to clone the Paho MQTT library for C:

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

    In this topic, the master branch is used to develop the sample code. The commit ID is f7799da95e347bbc930b201b52a1173ebbad45a7.

  2. Run the following commands to compile and install the Paho MQTT for C library:

    mkdir build
    cmake ..
    make -j
    sudo make install
    cd ..
    rm -rf build

Connect a device to IoT Platform

  1. Download a demo package from Demo package and decompress the package to a directory. The following table describes the files that are included in the directory.

    File

    Description

    CMakeLists.txt

    The configuration file of the project.

    src/samples/MQTTAsync_publish.c

    This file contains the logic code that is used to connect a device to IoT Platform and enable communication.

    src/samples/aiot_mqtt_sign.c

    This file contains the code that is used to obtain the MQTT connection parameters. When you run the aiot_c_demo.c file, the aiotMqttSign() function is called to obtain the values of the username, password, and clientId parameters.

    src/samples/CMakeLists.txt

    The configuration file of the demo project.

  2. In the src/samples/MQTTAsync_publish.c file, replace the ProductKey, DeviceName, and DeviceSecret with your device certificate information.

    #define EXAMPLE_PRODUCT_KEY            "The ProductKey of the product" 
    #define EXAMPLE_DEVICE_NAME            "The DeviceName of the device" 
    #define EXAMPLE_DEVICE_SECRET          "The DeviceSecret of the device"
  3. In the src/samples/MQTTAsync_publish.c file, tcp://iot-0***********l.mqtt.iothub.aliyuncs.com:443 indicates an endpoint.

    Note

    For more information about the endpoints of public instances and Enterprise Edition instances and the formats of the endpoints, see View the endpoint of an instance.

    #define ADDRESS     "tcp://iot-0***********l.mqtt.iothub.aliyuncs.com:443"
  4. Replace the files described in the "Download the Paho MQTT library for C" section of this topic with the files described in Step 1 based on the directories in which the files are stored.

  5. Run the following commands to compile the project:

    mkdir build    // Run the command in the root directory of the project.
    cd build
    cmake ..
    make -j
  6. After you compile the project, run the following command in the build directory to execute the program file:

    ./src/samples/MQTTAsync_publish

    After you execute the program file, the following local logs are generated:

    clientid: ty***1&a1****kc|timestamp=2524608000000,_v=paho-c-standard-1.0.0,securemode=3,signmethod=hmacsha256,lan=C|
    username: ty***1&a1****kc
    password: 1BB**************************************************3621
    Waiting for publication of Hello World!
    on topic /sys/a1*****kc/ty********1/thing/event/property/post for client
    Successful connection
    Message with token value 1 delivery confirmed
    Successful disconnection