Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

An In-depth Look at QoS Levels in MQTT for Intermediate C and Python Programmers

August 8, 2023

An In-depth Look at QoS Levels in MQTT for Intermediate C and Python Programmers

MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol that is widely used in IoT (Internet of Things) and other resource-constrained environments. One of the key features of MQTT is its support for Quality of Service (QoS) levels, which allow you to control the message delivery reliability and order. In this blog post, we will take an in-depth look at QoS levels in MQTT, and provide extensive examples in both C and Python for intermediate programmers.

Understanding QoS Levels

MQTT supports three levels of QoS: 0, 1, and 2. Each level provides a different level of message delivery guarantee:

Using QoS Levels in C

Let’s demonstrate the usage of QoS levels in C using the Eclipse Paho MQTT client library. First, we need to create an MQTT client and connect it to a broker:

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

#define ADDRESS "tcp://mqtt.eclipse.org:1883"
#define CLIENTID "ExampleClientSub"
#define TOPIC "mqtt_example"
#define QOS 1

int main(int argc, char* argv[]) {
    MQTTClient client;
    ...
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;

    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    ...
    pubmsg.qos = QOS;
    ...
    MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
    ...

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

In the code snippet above, we set the QoS level to 1 when publishing a message using the MQTTClient_message struct.

Using QoS Levels in Python

Now, let’s explore the usage of QoS levels in Python using the Paho MQTT client library. First, we need to create an MQTT client and connect it to a broker:

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

def on_connect(client, userdata, flags, rc):
    ...
    client.subscribe("mqtt_example", qos=1)

def on_message(client, userdata, msg):
    ...

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

client.connect("mqtt.eclipse.org", 1883, 60)
client.loop_forever()

In the Python code snippet above, we subscribe to a topic with QoS level 1 using the subscribe method of the MQTT client.

Conclusion

In this blog post, we have provided an in-depth look at QoS levels in MQTT for intermediate C and Python programmers. We have explained the concepts of QoS levels and provided extensive examples in both C and Python. Understanding and effectively using QoS levels is essential for building reliable and efficient MQTT-based applications, especially in resource-constrained environments. With the examples and explanations provided in this post, we hope that you have gained a strong foundation in leveraging QoS levels in MQTT for your programming projects.


➡️ Optimizing Memory Footprint in MQTT Clients for C and Python Development


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


Go back to Posts.