How to Communicate with A MQTT Broker Using Paho Clients on Dusun Gateways

The Dusun IoT gateway is based on Linux operating systems, then compiling the Paho project has little difference for Linux PC and Dusun gateways. Here we compile and test the Paho client on a Linux PC and then show how to compile the Paho client for the gateway whose cpu architecture is MIPS.

1) Compiling Paho MQTT client and test it on PC

Compiling Paho is easy on a Linux PC with the following commands:

git clone GitHub - eclipse/paho.mqtt.c: An Eclipse Paho C client library for MQTT for Windows, Linux and MacOS. API documentation: https://eclipse.github.io/paho.mqtt.c/

cd paho.mqtt.c

make

sudo make install

Then we can find the compiled binaries on ./build/output directory. In the ./build/output/samples directory, we can see the paho_c_pub/paho_c_sub and MQTTClient_publish/MQTTClient_subscribe which we described above and will be used for testing here.

We should install an eclipse mosquitto server before testing as the following commands:

sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa

sudo apt-get update

sudo apt-get install mosquitto

After the mosquitto is installed, we start the mosquitto broker (1888: port number);

Mosquitto –p 1888 -v

We test the paho client by starting the Paho clients, the figures below show the results:

  1. Cross compiling Paho MQTT client for Dusun gateway.

Revise the makefile in the Paho directory (e.g. /paho.mqtt.c): add the following lines, omit cc?=gcc in the file. you should change /OpenWrt-SDK to your OpenWrt SDK folder.

CC = ./OpenWrt-SDK/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mipsel-openwrt-linux-gcc

CFLAGS = -I /OpenWrt-SDK/staging_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/usr/include

LDFLAGS =-L/OpenWrt-SDK/staging_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/usr/lib -L/home/guojie/Software/OpenWrt-SDK/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/lib

In a console, type the following commands, and the Paho client for gateway will be compiled.

cd paho.mqtt.c

sudo make

Finally, copy the output files to the gateway. The program is ready for using to publish/subscribe MQTT commands. Users may revise MQTTClient_publish.c/ MQTTClient_subscribe.c to suit your needs.