MQTT C example: Difference between revisions

From RidgeRun Developer Wiki
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
<seo title="MQTT C Example | RidgeRun Developer" titlemode="replace" keywords="MQTT, message broker, publisher, subscriber, publisher-subscriber, metadata, message sharing, C example."  description="RidgeRun explains how to use MQTT with C. Learn how to easily test an MQTT C example!"></seo>
=What is MQTT?=
=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.
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.


Line 36: Line 37:
</pre>
</pre>


 
The configuration of the publisher is as follows:
<pre>
#define ADDRESS    "tcp://mqtt.eclipseprojects.io:1883"
#define CLIENTID    "ExampleClientPub"
#define TOPIC      "MQTT Examples"
#define PAYLOAD    "Hello World!"
</pre>
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==
==Creating Subscriber==
Line 64: Line 76:
</pre>
</pre>
* The app will remain active listening for messages until the user closes it.
* The app will remain active listening for messages until the user closes it.
The configuration of the subscriber is as follows:
<pre>
#define ADDRESS    "tcp://mqtt.eclipseprojects.io:1883"
#define CLIENTID    "ExampleClientSub"
#define TOPIC      "MQTT Examples"
</pre>
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.
{{ContactUs}}
[[Category:HowTo]]

Latest revision as of 02:05, 5 June 2024


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.


RidgeRun Resources

Quick Start Client Engagement Process RidgeRun Blog Homepage
Technical and Sales Support RidgeRun Online Store RidgeRun Videos Contact Us
RidgeRun.ai: Artificial Intelligence | Generative AI | Machine Learning

Contact Us

Visit our Main Website for the RidgeRun Products and Online Store. RidgeRun Engineering information is available at RidgeRun Engineering Services, RidgeRun Professional Services, RidgeRun Subscription Model and Client Engagement Process wiki pages. Please email to support@ridgerun.com for technical questions and contactus@ridgerun.com for other queries. Contact details for sponsoring the RidgeRun GStreamer projects are available in Sponsor Projects page.