Connecting to the MQTT Broker Using the Open-source C Language Library
Scenario
MQTT is a message protocol in publish/subscribe mode. It works even on remote devices with poor hardware performance and in poor network conditions.
This article describes how to connect and use IEF's built-in MQTT broker using an open-source C language library, Eclipse Paho C Client Library.
Procedure
- Prepare a Linux computer and download the source code.
git clone https://github.com/eclipse/paho.mqtt.c.git
You can also download the ZIP package from https://github.com/eclipse/paho.mqtt.c and decompress it.
- Install the compilation dependency tool.
For Ubuntu, run the following commands:
apt-get install build-essential gcc make cmake cmake-gui cmake-curses-gui
apt-get install fakeroot fakeroot devscripts dh-make lsb-release
apt-get install libssl-dev
apt-get install ninja-build
For CentOS, run the following commands:
yum install build-essential gcc make cmake cmake-gui cmake-curses-gui
yum install fakeroot fakeroot devscripts dh-make lsb-release
yum install openssl-devel
yum install ninja-build
- Modify the sample code.
Add the following line to the src/samples/ paho_cs_pub.c file of the source code.
ssl_opts.enableServerCertAuth =0;
Add the following line to the src/samples/paho_cs_sub.c file.
- Compile the sample program.
mkdir /tmp/build.paho
cd /tmp/build.paho
cmake -GNinja -DPAHO_BUILD_STATIC=TRUE -DPAHO_BUILD_SHARED=FALSE -DPAHO_WITH_SSL=TRUE -DPAHO_BUILD_SAMPLES=TRUE {paho.mqtt.c directory}
ninja package
{paho.mqtt.c directory} is the directory where the source code paho.mqtt.c is stored, for example, /root/work/paho.mqtt.c.
- Go to the directory where the compiled sample program is located, and copy the generated binary files paho_cs_pub and paho_cs_sub to the edge node.
cd /tmp/build.paho/src/samples/
- Download the edge node certificate.
- Log in to the IEF console, choose Managed Resources > Edge Nodes in the navigation pane, and click an edge node name on the displayed page to view the edge node details page. Click the Certificates tab, and click Add Certificate.
Figure 1 Adding a certificate
- In the displayed dialog box, enter the certificate name and click OK.
- Copy the downloaded certificate to the edge node and decompress it.
- Log in to the IEF console, choose Managed Resources > Edge Nodes in the navigation pane, and click an edge node name on the displayed page to view the edge node details page. Click the Certificates tab, and click Add Certificate.
- Run the sample program.
For example, to publish a message to a specified topic, run the following commands to view the paho_cs_pub command instructions.
The following is an example of publishing a message:
./paho_cs_pub -c ssl://127.0.0.1:8883 -q 0 -m "xxx" -t "aaa" --cert /root/mqtt_cert/xOEMIsYVpw_private_cert.crt --key /root/mqtt_cert/xOEMIsYVpw_private_cert.key
Running this command will send a message whose content is xxx to the aaa topic in the built-in MQTT broker. 127.0.0.1:8883 is the address of the built-in MQTT broker of the edge node. /root/mqtt_cert/xOEMIsYVpw_private_cert.crt and /root/mqtt_cert/xOEMIsYVpw_private_cert.key are the edge node certificates.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot