Skip to content

Implementing MQTT in Python

by HiveMQ Team
(updated on ) 9 min read

MQTT and Python can be used together to create efficient IoT systems. MQTT is a popular messaging protocol for IoT use cases because it is lightweight and sends messages efficiently. It uses a publish/subscribe (Pub/Sub) model that works well for devices that need to communicate in real time with minimal resources.

Python is a popular programming language in IoT because it’s open source, it has many support libraries, it’s versatile, and it’s easy to use. You can use a Python MQTT client to connect to an MQTT broker, publish messages, and subscribe to topics to receive messages.

You can implement MQTT in many ways, including with the popular scripting language Python.

How to Use an MQTT Python Client?

There are several Python MQTT clients, including HBMQTT and gmqtt, but the Paho Python Client by the Eclipse Foundation is by far the most popular. HBMQTT and gmqtt are both smaller options that are built on top of the asyncio Python library. They allow users to set asynchronous breakpoints for some performance advantages, but have less extensive documentation and support than the Paho library and require users to work with Python version 3.4 or later.

The Paho Python Client supports MQTT versions 5.0, 3.1.1, and 3.1, and Python versions 2.7.9 or later or version 3.6 or later. All Python MQTT client libraries allow users to write MQTT Python applications to connect to a broker, publish messages, and subscribe to topics to receive messages. MQTT’s pub/sub architecture means that devices don’t need to poll a server to check if there is new data available. By subscribing to a topic, they receive new data when it comes in automatically.

To implement MQTT in Python, you first need to install the client library you’ve chosen to work with.

For this example we’ll use Paho, but the same procedures would apply to other options.

pip3 install paho-mqtt<2.0.0

Then, using Python you can import the MQTT client.

import paho.mqtt.client as paho

from paho import mqtt

client = paho.Client(client_id="", userdata=None, protocol=paho.MQTTv5)

How to Configure an MQTT Broker in Python?

MQTT Broker plays a very important role in MQTT. Each device can connect to one broker to share information with countless other devices with only one open connection. HiveMQ has both an open source option, HiveMQ CE, and a hosted option, HiveMQ Cloud Serverless.

To connect to HiveMQ Cloud Serverless using Python, first enable TLS for a secure connection.

client.tls_set(tls_version=mqtt.client.ssl.PROTOCOL_TLS)

Then set your username and password.

client.username_pw_set("username", "password")

Then you can connect to HiveMQ Cloud on port 8883, the default for MQTT.

client.connect("14b5793c334743769b3e9fb1e4008401.s2.eu.hivemq.cloud", 8883)

To connect to HiveMQ CE Broker, you enable TLS and set the username as described for HiveMQ Cloud Serverless, and then us the following statement:

client.connect("ssl://localhost", 8883)

Once you have a connection established you can publish messages and subscribe to topics to send and receive data.

Code Examples of Using MQTT in Python

Publishing MQTT Messages in Python

  • a single publish, this can also be done in loops

client.publish("encyclopedia/temperature", payload="hot", qos=1)

Subscribing MQTT Messages in Python

  • subscribe to all topics of encyclopedia by using the wildcard "#"

client.subscribe("encyclopedia/#", qos=1)

These code examples come from the guide Getting Started with Paho Python from HiveMQ Cloud.

MQTT Security Best Practices While Using Python with an MQTT Broker

Because MQTT is optimized for efficient communication between IoT devices, not secure communication, it’s important to follow security practices while implementing MQTT using Python. IoT devices often have limited memory and computing capabilities, which makes paying attention to security even more important.

MQTT itself includes minimal security measures. One way to add security to your MQTT communication is to use a secure network or VPN. MQTT implementations often build on other security standards, such as SSL/TLS. SSL/TLS is a popular security standard often used for transport level encryption. This makes sure data is encrypted during transmission and includes identity verification methods on both sides to keep data secure. The Python MQTT Client includes methods for enabling SSL/TLS, such as in the following example code.

client.tls_set(tls_version=mqtt.client.ssl.PROTOCOL_TLS)

When using MQTT and Python you can also use a client identifier and username and password credentials to increase security. This lets you authenticate devices on the application level. For example, when connecting to HiveMQ Cloud Serverless, you input your username and password to keep your data secure.

Use Case Examples for MQTT in Python

MQTT is commonly employed with Python in use cases where its lightweight and efficient messaging approach is beneficial, such as remote monitoring, home automation, telemetry, and sensor networks. MQTT Python applications can share data between numerous devices in real time. For example, in a smart home you could write an application using Python and data shared using MQTT to turn on the air conditioner if the temperature in a room rises above a set threshold.

Conclusion

Using MQTT with Python allows you to seamlessly create MQTT Python applications and efficiently share data in real time. When deciding how to build an application, consider your specific requirements, constraints, and project objectives to make an informed choice about using MQTT in your Python projects. Click here to learn more about MQTT Client libraries.

FAQs on Implementing MQTT in Python

HiveMQ Team

The HiveMQ team loves writing about MQTT, Sparkplug, Industrial IoT, protocols, how to deploy our platform, and more. We focus on industries ranging from energy, to transportation and logistics, to automotive manufacturing. Our experts are here to help, contact us with any questions.

HiveMQ logo
Review HiveMQ on G2