MQTT c client on raspberry pi

What you will learn here about MQTT c client on raspberry pi

  • How to install MQTT c client on raspberry pi
  • MQTT c client as MQTT subscriber on raspberry pi
  • MQTT c client as MQTT publisher on raspberry pi

How to install MQTT c client on raspberry pi

Please follow the following steps to install MQTT c client on raspberry pi:
1)In order to use MQTT c client on raspberry pi, you need a MQTT broker. If you do not know how to install MQTT broker on raspberry pi please click on the below link.

Steps to Install MQTT Broker on Raspberry Pi

2)Assuming you have successfully installed MQTT broker on raspberry pi

3)Now open the raspberry pi terminal.

4)Now execute the following command to go on Desktop

cd Desktop

5)Now please create a MQTTClients folder on Desktop which is shown below.

mkdir MQTTClients

 make folder on raspberry pi

6)Execute the following command to navigate in MQTTClients folder.

cd MQTTClients

7)Now please execute the following command to download MQTT c client on raspberry pi which is shown below

sudo git clone https://github.com/janderholm/paho.mqtt.c.git

download mqtt client on raspberry pi

8)Now execute the following command to download libssl files which is shown below

sudo apt-get install libssl-dev

download libssl on raspberry
9)Now execute the following to go in paho.mqtt.c folder

cd paho.mqtt.c

10)Execute the following command

sudo make

sudo make
11)Execute the following command

sudo make install

sudo make install
If you are able see following response means, MQTT clients installed successfully.
MQTT C client success
12)Execute the following command to go in samples folder which is inside src folder

cd src/samples

13)Execute the following command to make all .c file as executable which is shown below

sudo chmod 777 *.c

make mqtt c client executable

MQTT c client as MQTT Subscriber on raspberry pi

please follow the following steps to know, howto use MQTT c client as MQTT subscriber

1)Assuming you have followed above all steps successfully

2)Now open the raspberry terminal

3)Now please go to the samples folder. Please execute the following command to go in sample folder.

cd Desktop/MQTTClients/paho.mqtt.c/src/samples


4)Execute following command to compile MQTTClient_subscribe.c file which is shown below

sudo cc MQTTClient_subscribe.c -o MQTTSUB -lpaho-mqtt3c

compile c client code
5)Now execute the following command to run MQTTSUB file which is compiled the file. Once you run MQTTSUB file, Your subscriber will start listening on topic “MQTT Examples” which is shown below

./MQTTSUB

run c compiled code on raspberry pi
6)Now please open another terminal on raspberry pi

7)Use the following command to publish data on the topic “MQTT Examples” which is shown below

mosquitto_pub -t “MQTT Examples” -m “temp:50”

raspberry pi terminal as mqtt publisher

MQTT c client as MQTT publisher on raspberry pi

please follow the following steps to know, how to use MQTT c client as MQTT subscriber

1)Assuming you have followed above all steps successfully

2)Now open the raspberry terminal

3)Now please go to the samples folder. Please execute the following command to go in sample folder which is shown below

cd Desktop/MQTTClients/paho.mqtt.c/src/samples


4)Execute following command to compile MQTTClient_publish.c file which is shown below

sudo cc MQTTClient_publish.c -o MQTTPUB -lpaho-mqtt3c

Compile publisher code
5)Now please open another terminal on raspberry pi

6)Use the following command to subscribe data on the topic “MQTT Examples”

mosquitto_sub -t “MQTT Examples”

7)Now execute the following command from first terminal to run MQTTPUB file which is compiled file. Once you run MQTTSUB file, Your subscriber will start listening on topic “MQTT Examples” which is shown below

 ./MQTTPUB

raspberry pi terminal as mqtt subscriber
Please click on the below link to become master in MQTT.

Master MQTT Protocol

You may also like...

Leave a Reply