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
Run the following command to clone the Paho MQTT library for C:
git clone https://github.com/eclipse/paho.mqtt.c.git
NoteIn this topic, the master branch is used to develop the sample code. The
commit ID
isf7799da95e347bbc930b201b52a1173ebbad45a7
.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
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.
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"
In the
src/samples/MQTTAsync_publish.c
file,tcp://iot-0***********l.mqtt.iothub.aliyuncs.com:443
indicates an endpoint.NoteFor 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"
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.
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
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