Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Building Scalable MQTT Systems for C and Python Projects: Intermediate Implementation

September 13, 2023

Building Scalable MQTT Systems for C and Python Projects: Intermediate Implementation

In this blog post, we will explore intermediate implementation techniques for building scalable MQTT (Message Queuing Telemetry Transport) systems in C and Python. MQTT is a lightweight messaging protocol specifically designed for IoT (Internet of Things) scenarios, where bandwidth and resources are limited.

Understanding MQTT

Before we dive into the implementation details, let’s briefly recap some fundamental concepts of MQTT.

Broker

The MQTT broker is a server that receives all messages from the clients and then routes these messages to the appropriate destination clients.

Client

An MQTT client is any device that runs an MQTT library and connects to an MQTT broker over a network. Clients can either publish messages to a specific topic or subscribe to topics to receive messages.

Topics

In MQTT, messages are published to topics. Clients can subscribe to specific topics to receive messages that are published to those topics.

QoS (Quality of Service)

MQTT supports three levels of QoS for message delivery:

Setting Up the Environment

We will use the Eclipse Paho MQTT C client library for C implementation and the Eclipse Paho MQTT Python client for Python implementation.

For C, you can install the Eclipse Paho MQTT C client library using the following commands:

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

For Python, you can install the Eclipse Paho MQTT Python client using pip:

1
pip install paho-mqtt

C Implementation

Let’s start by implementing a simple MQTT client in C that connects to an MQTT broker, publishes a message to a topic, and subscribes to receive messages on a different topic.

 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTClient.h"

#define ADDRESS "tcp://iot.eclipse.org:1883"
#define CLIENTID "ExampleClientC"
#define TOPIC_PUBLISH "testtopic/pub"
#define TOPIC_SUBSCRIBE "testtopic/sub"

int delivered = 0;

void deliveredCallback(void* context, MQTTClient_deliveryToken token) {
    printf("Message with token value %d delivery confirmed\n", token);
    delivered = 1;
}

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, deliveredCallback, NULL);

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

    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    pubmsg.payload = "Hello from C";
    pubmsg.payloadlen = strlen(pubmsg.payload);
    pubmsg.qos = 1;
    pubmsg.retained = 0;

    MQTTClient_publishMessage(client, TOPIC_PUBLISH, &pubmsg, NULL);

    MQTTClient_subscribe(client, TOPIC_SUBSCRIBE, 1);

    while (!delivered) {
        usleep(10000);
    }

    MQTTClient_disconnect(client, 10000);
}

In this example, we first define the address of the MQTT broker, the client ID, and the topics for publishing and subscribing. We then create an MQTT client, set up connection options, connect to the broker, and publish a message on the TOPIC_PUBLISH topic. We also subscribe to the TOPIC_SUBSCRIBE topic to receive messages.

Python Implementation

Now, let’s implement the same functionality in Python using the Paho MQTT client library.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import time
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("testtopic/sub")

def on_message(client, userdata, msg):
    print("Received message: "+msg.payload.decode())

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("iot.eclipse.org", 1883, 60)

client.publish("testtopic/pub", "Hello from Python", qos=1)

client.loop_forever()

In this Python example, we define callback functions for connecting to the broker and receiving messages. We then create an MQTT client, connect to the broker, publish a message, and start the client loop to receive messages.

Conclusion

In this blog post, we have explored intermediate implementation techniques for building scalable MQTT systems in C and Python. By leveraging the Eclipse Paho MQTT client libraries, we can easily create MQTT clients, connect to brokers, publish messages, and subscribe to topics. With these techniques, you can build robust and scalable MQTT-based systems for your C and Python projects.

Happy coding!


➡️ ESP32 programmer


⬅️ Integrating MQTT with C and Python Web Applications: Intermediate Development Strategies


Go back to Posts.