Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Understanding the MQTT Protocol: Intermediate Concepts for C and Python Developers

July 15, 2023

Understanding the MQTT Protocol: Intermediate Concepts for C and Python Developers

MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol designed for constrained devices and low-bandwidth, high-latency, or unreliable networks. In this blog post, we will explore intermediate concepts of the MQTT protocol, with extensive examples in C and Python, to deepen our understanding of how to implement MQTT communication in our projects.

MQTT in a Nutshell

Before diving into intermediate concepts, let’s recap the basic principles of MQTT. MQTT operates on a publish-subscribe model, where clients (publishers) send messages to a broker, and other clients (subscribers) receive messages from the broker based on their subscriptions. The key MQTT entities are the broker, publishers, subscribers, and topics, which act as communication channels.

Intermediate Concepts

Quality of Service (QoS)

Quality of Service in MQTT determines the level of assurance for message delivery. MQTT supports three levels of QoS:

  1. QoS 0 (At most once): The message is delivered at most once or not at all. This level offers the lowest assurance of delivery.
  2. QoS 1 (At least once): The message is guaranteed to be delivered at least once, possibly more than once.
  3. QoS 2 (Exactly once): The message is guaranteed to be delivered exactly once. This level provides the highest assurance of delivery but incurs higher overhead.

Let’s see how to implement QoS in C and Python.

Example in C:

1
2
3
4
5
6
7
// Publishing with QoS 1
char message[] = "Hello, MQTT!";
MQTTClient_message pubmsg = MQTTClient_message_initializer;
pubmsg.payload = message;
pubmsg.payloadlen = strlen(message);
pubmsg.qos = 1;
MQTTClient_publishMessage(client, "sample-topic", &pubmsg, &token);

Example in Python:

1
2
# Publishing with QoS 2
client.publish("sample-topic", "Hello, MQTT!", qos=2)

Retained Messages

Retained messages are an advanced feature of MQTT that allows a broker to store the last message sent on a specific topic and deliver it to new subscribers immediately upon subscription.

Example in C:

1
2
3
4
5
6
7
// Publishing a retained message
MQTTClient_message pubmsg = MQTTClient_message_initializer;
pubmsg.payload = message;
pubmsg.payloadlen = strlen(message);
pubmsg.qos = 1;
pubmsg.retained = 1;
MQTTClient_publishMessage(client, "retained-topic", &pubmsg, &token);

Example in Python:

1
2
# Publishing a retained message
client.publish("retained-topic", "Hello, MQTT!", qos=1, retain=True)

Last Will and Testament (LWT)

Last Will and Testament allows a client to specify a message that the broker will send automatically if the client unexpectedly disconnects.

Example in C:

1
2
3
4
5
6
// Setting Last Will and Testament
MQTTClient_willOptions will = MQTTClient_willOptions_initializer;
will.message = "I'm offline";
will.qos = 1;
will.retained = 0;
MQTTClient_setWill(client, "status-topic", &will);

Example in Python:

1
2
# Setting Last Will and Testament
client.will_set("status-topic", payload="I'm offline", qos=1, retain=False)

Conclusion

In this blog post, we delved into intermediate concepts of the MQTT protocol, including Quality of Service, retained messages, and Last Will and Testament. We provided extensive examples in both C and Python to illustrate the implementation of these concepts.

Understanding these intermediate concepts is crucial for building robust and reliable MQTT communication in real-world projects. As C and Python developers, mastering these concepts will enable us to leverage the full potential of MQTT in our applications.

Remember to refer to the official documentation and MQTT libraries for detailed information and best practices when implementing these concepts in your projects.

Happy coding!


➡️ Writing modular and reusable code in C and Python


⬅️ Embracing functional programming concepts for clean code in Python


Go back to Posts.