Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Using MQTT for Real-time Data Streaming in C and Python Applications

August 4, 2023

Using MQTT for Real-time Data Streaming in C and Python Applications

Real-time data streaming is essential for many applications such as IoT, telemetry, and monitoring systems. MQTT (Message Queuing Telemetry Transport) is a lightweight and efficient protocol designed for reliable real-time communication between devices. In this blog post, we will explore how to utilize MQTT for real-time data streaming in C and Python applications. We will cover the basic concepts of MQTT, setting up a broker, and implementing MQTT clients in C and Python with extensive examples.

Introduction to MQTT

MQTT is a publish-subscribe messaging protocol that provides efficient and reliable communication between devices. It is designed to be lightweight, making it suitable for resource-constrained devices and low-bandwidth networks. MQTT operates on top of the TCP/IP protocol and uses a message broker to facilitate communication between clients.

The basic components of MQTT are:

Setting up an MQTT Broker

Before we can begin using MQTT in our applications, we need to set up an MQTT broker. There are various open-source MQTT brokers available, such as Mosquitto and VerneMQ, that can be easily installed and configured on most operating systems.

For this example, we will use Mosquitto as our MQTT broker. To install Mosquitto on Ubuntu, you can use the following commands:

1
2
sudo apt update
sudo apt install mosquitto mosquitto-clients

After installing the MQTT broker, you can start the Mosquitto service by running:

1
sudo systemctl start mosquitto

Implementing an MQTT Client in C

To demonstrate using MQTT in a C application, we will create a simple publisher and subscriber using the Eclipse Paho MQTT C client library. The publisher will send temperature sensor data to the broker, and the subscriber will receive and display the data.

First, let’s install the Eclipse Paho MQTT C client library by cloning the GitHub repository and building the library:

1
2
3
git clone https://github.com/eclipse/paho.mqtt.c.git
cd paho.mqtt.c
make install

Now, we can create a C program for the publisher:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "MQTTClient.h"

#define ADDRESS     "tcp://localhost:1883"
#define CLIENTID    "ExampleClientPub"
#define TOPIC       "temperature"

int main(int argc, char* argv[])
{
    MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    int rc;

    MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;

    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect, return code %d\n", rc);
        exit(-1);
    }

    char payload[10];
    int i = 0;
    while (1) {
        sprintf(payload, "%d", rand() % 100);
        MQTTClient_publish(client, TOPIC, strlen(payload), payload, 0, 0, NULL);
        printf("Message published: %s\n", payload);
        usleep(2000);
    }

    MQTTClient_disconnect(client, 10000);
    MQTTClient_destroy(&client);

    return rc;
}

In this example, we initialize an MQTT client, connect to the broker, and publish random temperature sensor data to the “temperature” topic at regular intervals.

Next, let’s create a C program for the subscriber:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <string.h>
#include "MQTTClient.h"

#define ADDRESS     "tcp://localhost:1883"
#define CLIENTID    "ExampleClientSub"
#define TOPIC       "temperature"

volatile MQTTClient_deliveryToken deliveredtoken;

int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    printf("Message arrived\n");
    printf("  topic: %s\n", topicName);
    printf("  message: %.*s\n", message->payloadlen, (char*)message->payload);
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    return 1;
}

void delivered(void *context, MQTTClient_deliveryToken dt)
{
    printf("Message delivered\n");
    deliveredtoken = dt;
}

int main(int argc, char* argv[])
{
    MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    int rc;

    MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;

    MQTTClient_setCallbacks(client, NULL, NULL, msgarrvd, delivered);

    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        printf("Failed to connect, return code %d\n", rc);
        exit(-1);
    }

    MQTTClient_subscribe(client, TOPIC, 1);

    while (1)
        usleep(100000);

    MQTTClient_disconnect(client, 10000);
    MQTTClient_destroy(&client);

    return rc;
}

In this subscriber example, we initialize an MQTT client, connect to the broker, subscribe to the “temperature” topic, and wait for incoming messages. When a message is received, the subscriber will display the topic and message payload.

Implementing an MQTT Client in Python

In addition to C, we can also utilize MQTT in Python applications using the Paho MQTT Python client library. We will create a publisher and subscriber similar to the C examples to demonstrate the usage of MQTT in Python.

First, we need to install the Paho MQTT Python client library:

1
pip install paho-mqtt

Now, let’s create a Python program for the publisher:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import time
import random
import paho.mqtt.client as mqtt

broker_address = "localhost"
client = mqtt.Client("ExampleClientPub")
client.connect(broker_address)

while True:
    temperature = random.randint(0, 100)
    client.publish("temperature", temperature)
    print("Message published:", temperature)
    time.sleep(2)

Similar to the C example, the Python publisher sends random temperature sensor data to the “temperature” topic at regular intervals.

Next, let’s create a Python program for the subscriber:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import paho.mqtt.client as mqtt

def on_message(client, userdata, message):
    print("Message received")
    print("  topic:", message.topic)
    print("  message:", str(message.payload.decode("utf-8")))

broker_address = "localhost"
client = mqtt.Client("ExampleClientSub")
client.on_message = on_message
client.connect(broker_address)
client.subscribe("temperature")
client.loop_forever()

In this subscriber example, the Python client connects to the broker, subscribes to the “temperature” topic, and runs the message-handling loop indefinitely. When a message is received, the subscriber will display the topic and message payload.

Conclusion

In this blog post, we have explored the usage of MQTT for real-time data streaming in C and Python applications. We covered the basic concepts of MQTT, set up an MQTT broker using Mosquitto, and implemented MQTT publishers and subscribers in both C and Python with extensive examples.

By leveraging the MQTT protocol, developers can enable efficient and reliable real-time communication in their applications, making it well-suited for a wide range of use cases such as IoT, telemetry, and monitoring systems. The lightweight nature of MQTT and the availability of client libraries in various programming languages make it a versatile choice for real-time data streaming.

I hope this blog post has provided valuable insights into utilizing MQTT for real-time data streaming in C and Python applications. Thank you for reading!


In this blog post, we covered the basic concepts of MQTT, setting up an MQTT broker using Mosquitto, and implementing MQTT publishers and subscribers in both C and Python with extensive examples. We explored the usage of MQTT for real-time data streaming in C and Python applications, enabling efficient and reliable communication between devices. I hope you find this post helpful in implementing real-time data streaming in your own C and Python applications using MQTT!


➡️ Advancing your career with clean code mastery in C and Python programming


⬅️ Code review techniques for enforcing clean code standards in C and Python projects


Go back to Posts.