MQTT C example

From RidgeRun Developer Wiki


What is MQTT?

MQTT (Message Queuing Telemetry Transport) is a lightweight, publish-subscribe network protocol that transports messages between devices. It is designed for connections with remote locations where a small code footprint is required or network bandwidth is limited. It is widely used in Internet of Things (IoT) applications.

How to use MQTT in C?

Using MQTT in C typically involves using an MQTT client library. One popular library for MQTT in C is the Paho MQTT library by the Eclipse Foundation. Paho API can be found here. Paho library can be installed with the following steps:

git clone https://github.com/eclipse/paho.mqtt.c.git
cd paho.mqtt.c
mkdir build
cd build
cmake ..
make
sudo make install

Creating Publisher

Paho provides an example publisher application here. In order to test this app, follow these steps:

  • Copy the content of the code to a pub_async.c file
  • Compile the file:
gcc -o pub_async pub_async.c -lpaho-mqtt3c
  • Run the app:
./pub_async
  • You will see something like this:
Waiting for publication of Hello World!
on topic MQTT Examples for client with ClientID: ExampleClientPub
Message with token value 1 delivery confirmed

The configuration of the publisher is as follows:

#define ADDRESS     "tcp://mqtt.eclipseprojects.io:1883"
#define CLIENTID    "ExampleClientPub"
#define TOPIC       "MQTT Examples"
#define PAYLOAD     "Hello World!"

Where each parameter represents:

  • ADDRESS: Address where the message broker server is running. This address typically includes the protocol (e.g., tcp), the broker's hostname or IP address, and the port number.
  • CLIENTID: Identifier of the client connected to the broker
  • TOPIC: Topic to which the publisher will send messages
  • PAYLOAD: This is the example message content that will be published, this is important in the context of this app, but it will change dynamically depending on the application.

Creating Subscriber

Paho provides an example subscriber application here. In order to test this app, follow these steps:

  • Copy the content of the code to a sub_async.c file
  • Compile the file:
gcc -o sub_async sub_async.c -lpaho-mqtt3c
  • Run the app:
./sub_async
  • You will see something like this:
Subscribing to topic MQTT Examples
for client ExampleClientSub using QoS1

Press Q<Enter> to quit
  • The app will wait for the publisher app to send a message, if you run the publisher app once you will see the following message:
Message arrived
     topic: MQTT Examples
   message: Hello World!
  • The app will remain active listening for messages until the user closes it.

The configuration of the subscriber is as follows:

#define ADDRESS     "tcp://mqtt.eclipseprojects.io:1883"
#define CLIENTID    "ExampleClientSub"
#define TOPIC       "MQTT Examples"

Where each parameter represents:

  • ADDRESS: Address where the message broker server is running. This address typically includes the protocol (e.g., tcp), the broker's hostname or IP address, and the port number.
  • CLIENTID: Identifier of the client connected to the broker
  • TOPIC: Topic to which the subscriber will connect to receive messages.


For direct inquiries, please refer to the contact information available on our Contact page. Alternatively, you may complete and submit the form provided at the same link. We will respond to your request at our earliest opportunity.


Links to RidgeRun Resources and RidgeRun Artificial Intelligence Solutions can be found in the footer below.