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
-
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 idisf7799da95e347bbc930b201b52a1173ebbad45a7. -
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
-
Download the demo package. The decompressed package contains the following files:
File name
Description
CMakeLists.txtThe main configuration file for the project.
src/samples/MQTTAsync_publish.cContains the logic for connecting the device to IoT Platform and exchanging messages.
src/samples/aiot_mqtt_sign.cGenerates MQTT connection parameters. When
aiot_c_demo.cruns, it calls theaiotMqttSign()function defined in this file to calculate the username, password, and clientId parameters.src/samples/CMakeLists.txtThe configuration file for the demo project.
-
In the
src/samples/MQTTAsync_publish.cfile, 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" -
In the
src/samples/MQTTAsync_publish.cfile,tcp://iot-0***********l.mqtt.iothub.aliyuncs.com:443is 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" -
Overwrite the original files in the Paho MQTT C library directory with the corresponding files from the demo package that you downloaded.
-
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 -
After the compilation is complete, go to the
builddirectory and run the following command to start the program../src/samples/MQTTAsync_publishIf 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