Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Exploring MQTT Message Filtering in C and Python Development: Intermediate Concepts

September 21, 2023

Exploring MQTT Message Filtering in C and Python Development: Intermediate Concepts

MQTT, or Message Queuing Telemetry Transport, is a lightweight messaging protocol that is widely used in the Internet of Things (IoT) and other real-time communication applications. One of the key features of MQTT is its support for message filtering, which allows clients to receive only the messages that are relevant to their specific interests. In this blog post, we will explore the concepts of MQTT message filtering in C and Python development, focusing on intermediate concepts and providing extensive examples in both languages.

Understanding MQTT Message Filtering

Before we dive into the implementation details, let’s first understand the concept of MQTT message filtering. MQTT provides a flexible mechanism for clients to subscribe to topics using wildcard characters to specify multiple levels within a topic hierarchy. The two wildcard characters used in MQTT message filtering are + (single-level wildcard) and # (multi-level wildcard). The + wildcard matches any single level within a topic, while the # wildcard matches multiple levels within a topic.

For example, a client can subscribe to the topic sensors/+/temperature to receive temperature data from multiple sensors, or they can subscribe to the topic sensors/# to receive data from all sensors regardless of the specific measurement being transmitted.

Implementing MQTT Message Filtering in C

In C programming, the Eclipse Paho MQTT client library provides a robust and feature-rich implementation of the MQTT protocol. To demonstrate MQTT message filtering in C, let’s take a look at a sample program that uses the Paho MQTT library to subscribe to a topic with wildcard characters.

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

#define ADDRESS "tcp://localhost:1883"
#define CLIENTID "ExampleClientSub"
#define TOPIC "sensors/+/temperature"
#define QOS 1
#define TIMEOUT 10000L

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);
    }

    printf("Subscribing to topic %s\nfor client %s using QoS %d\n\n", TOPIC, CLIENTID, QOS);
    MQTTClient_subscribe(client, TOPIC, QOS);

    /* ... */

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

    return rc;
}

In the above C program, we create an MQTT client and subscribe to the topic sensors/+/temperature using the MQTTClient_subscribe function. This allows the client to receive temperature data from any sensor publishing to a specific sub-topic within the sensors topic hierarchy.

Implementing MQTT Message Filtering in Python

In the Python ecosystem, the Eclipse Paho MQTT client library is also available and provides a convenient way to interact with MQTT brokers. Let’s explore a similar example in Python to demonstrate MQTT message filtering using the Paho MQTT library.

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

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

def on_message(client, userdata, msg):
    print(msg.topic + " " + str(msg.payload))

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

client.connect("localhost", 1883, 60)

client.loop_forever()

In the above Python program, we create an MQTT client using the Paho MQTT library and subscribe to the topic sensors/+/temperature in the on_connect callback using the client.subscribe method. When a message is received on the subscribed topic, the on_message callback is triggered, and the message payload is printed to the console.

Conclusion

In this blog post, we have explored the intermediate concepts of MQTT message filtering in C and Python development. We have demonstrated how to subscribe to MQTT topics with wildcard characters using the Eclipse Paho MQTT client libraries and provided extensive examples in both C and Python. Message filtering is a powerful feature of MQTT that enables efficient and targeted message delivery in IoT and real-time communication applications.

If you are interested in diving deeper into MQTT programming or learning more about other MQTT features, I highly recommend exploring the official documentation and community resources for the Eclipse Paho MQTT client libraries in C and Python.

I hope this blog post has provided valuable insights into MQTT message filtering and its implementation in C and Python development. Happy coding!


➡️ MQTT Best Practices for Intermediate C and Python Programmers: Tips for Efficient Implementation


⬅️ Managing MQTT Session Persistence in C and Python Applications: Intermediate Techniques


Go back to Posts.