Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Integrating MQTT with C and Python IoT Projects: Tips and Tricks for Intermediate Programmers

July 31, 2023

Integrating MQTT with C and Python IoT Projects: Tips and Tricks for Intermediate Programmers

As the Internet of Things (IoT) continues to grow, the need for efficient and reliable communication protocols becomes increasingly important. MQTT (Message Queuing Telemetry Transport) has emerged as a popular choice for IoT communication due to its lightweight nature and publish-subscribe messaging paradigm. In this blog post, we’ll explore how to integrate MQTT with C and Python in IoT projects, providing tips and tricks for intermediate programmers.

Understanding MQTT

Before diving into the integration of MQTT with C and Python, let’s briefly understand the core concepts of MQTT:

Integrating MQTT with C

Example: MQTT Publisher in C

Let’s start by creating a simple MQTT publisher in C using the Eclipse Paho MQTT C client library. Below is an example of a C program that connects to an MQTT broker and publishes a message:

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

#define ADDRESS     "tcp://broker.example.com:1883"
#define CLIENTID    "publisher"
#define TOPIC       "iot/data"
#define PAYLOAD     "Hello, MQTT!"
#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);
    }

    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    pubmsg.payload = PAYLOAD;
    pubmsg.payloadlen = strlen(PAYLOAD);
    pubmsg.qos = QOS;
    pubmsg.retained = 0;
    MQTTClient_deliveryToken token;
    MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);

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

Tips and Tricks for MQTT in C

Integrating MQTT with Python

Example: MQTT Subscriber in Python

In Python, integrating MQTT with an IoT project is straightforward using the paho-mqtt library. Here’s an example of a Python program that acts as an MQTT subscriber and listens to a specific topic:

 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("iot/data")

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("broker.example.com", 1883, 60)

client.loop_forever()

Tips and Tricks for MQTT in Python

Conclusion

Integrating MQTT with C and Python in IoT projects can empower developers to establish efficient and scalable communication within their applications. By understanding the core concepts of MQTT and leveraging the respective client libraries, intermediate programmers can build robust MQTT-based solutions for their IoT initiatives. Keep exploring the capabilities of MQTT and experiment with C and Python code to enhance your understanding and skills in IoT development.

Happy coding!


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


⬅️ Leveraging modern tooling and IDE features for clean code development in C and Python


Go back to Posts.