すべてのプロダクト
Search
ドキュメントセンター

IoT Platform:Connect a device to Alibaba Cloud IoT Platform using Paho MQTT C (for non-embedded systems)

最終更新日:Jun 19, 2026

Use the open source Paho MQTT C library (for non-embedded systems) to connect a device to Alibaba Cloud IoT Platform and exchange messages.

Important notes

The steps in this topic are performed as a regular user. If an operation requires administrative permissions, prefix the command with sudo.

前提条件

プロダクトとデバイスは、IoT Platformコンソールインスタンスに作成されます。 デバイス証明書とMQTTエンドポイントが取得されます。 証明書情報には、ProductKey、DeviceName、およびDeviceSecretが含まれます。 詳細については、以下のトピックをご参照ください。

Prepare the development environment

This example uses Ubuntu 16.04 LTS as the development environment. Run the following commands to install the required dependencies.

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

Download the Paho MQTT C library

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

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

    This demo was created using the master branch. The commit id is f7799da95e347bbc930b201b52a1173ebbad45a7.

  2. Run the following commands to compile and install the Paho library.

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

Connect to IoT Platform

  1. Download the demo package. The decompressed package contains the following files:

    File name

    Description

    CMakeLists.txt

    The main configuration file for the project.

    src/samples/MQTTAsync_publish.c

    Contains the logic for connecting the device to IoT Platform and exchanging messages.

    src/samples/aiot_mqtt_sign.c

    Generates MQTT connection parameters. When aiot_c_demo.c runs, it calls the aiotMqttSign() function defined in this file to calculate the username, password, and clientId parameters.

    src/samples/CMakeLists.txt

    The configuration file for the demo project.

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

    #define EXAMPLE_PRODUCT_KEY            "Your_ProductKey" 
    #define EXAMPLE_DEVICE_NAME            "Your_DeviceName" 
    #define EXAMPLE_DEVICE_SECRET          "Your_DeviceSecret"
  3. In the src/samples/MQTTAsync_publish.c file, tcp://iot-0***********l.mqtt.iothub.aliyuncs.com:443 is the access domain name.

    説明

    For more information about the endpoint formats for public and Enterprise instances, see View instance endpoint information.

    #define ADDRESS     "tcp://iot-0***********l.mqtt.iothub.aliyuncs.com:443"
  4. Overwrite the original files in the Paho MQTT C library directory with the corresponding files from the demo package that you downloaded.

  5. Run the following commands to compile the project.

    mkdir build    // Run this command in the root directory of the project.
    cd build
    cmake ..
    make -j
  6. After the compilation is complete, go to the build directory and run the following command to start the program.

    ./src/samples/MQTTAsync_publish

    If the program runs successfully, the following log output indicates that the device is connected to IoT Platform.

    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