Todos los productos
Search
Centro de documentos

IoT Platform:Conexión de un dispositivo a Alibaba Cloud IoT Platform mediante Paho MQTT C (para sistemas no embebidos)

Última actualización: Sep 03, 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.

Notas importantes

Los pasos de este tema se ejecutan como usuario normal. Si una operación requiere permisos de administrador, anteponga sudo al comando.

Requisitos previos

Se creó un producto y un dispositivo en una instancia de la IoT Platform console. Se obtuvieron un certificado de dispositivo y un endpoint MQTT. La información del certificado incluye ProductKey, DeviceName y DeviceSecret. Para más información, consulte los siguientes temas:

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
    Nota

    Esta demostración se creó con la rama master. The commit id is f7799da95e347bbc930b201b52a1173ebbad45a7.

  2. Ejecute los siguientes comandos para compilar e instalar la biblioteca Paho.

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

Conexión a IoT Platform

  1. Descargue el paquete de demostración. The decompressed package contains the following files:

    Nombre del archivo

    Descripción

    CMakeLists.txt

    Archivo principal de configuración del proyecto.

    src/samples/MQTTAsync_publish.c

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

    src/samples/aiot_mqtt_sign.c

    Genera los parámetros de conexión MQTT. 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

    Archivo de configuración del proyecto de demostración.

  2. In the el archivo src/samples/MQTTAsync_publish.c, 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.

    Nota

    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