The Paho MQTT client library for Python is a popular choice for building MQTT applications in Python. Here’s how to use it in Python3. Paho MQTT Client provides a simple and easy-to-use API for publishing and subscribing to MQTT topics. It supports MQTT 5.0, 3.1.1, and 3.1 protocols.
Install Paho MQTT Client
To install Paho MQTT Client library, you can use the following command:
1
pip3 install paho-mqtt
Prepare the MQTT Broker
Before using the Paho MQTT Client library, we need to prepare the MQTT broker. We can use any MQTT broker that supports the MQTT 5.0, 3.1.1, or 3.1 protocols. Here we use the public free MQTT broker: EMQX, below is broker information.
To use the Paho MQTT Client library in the Python code, need to import it first.
1
from paho.mqtt import client as mqtt_client
Create a MQTT Connection
Before create a MQTT connection, specify the MQTT client id, MQTT broker address, port and mesage topic. We can use the python random.randint() function to generate a random client id.
Next, we need to write the on_connect callback function in order to connect to the proxy. This function is called after the client successfully connects, we can use the rc parameter to check the connection status. Typically, we also create a client object that is also connected to “broker.emqx.io”.
1 2 3 4 5 6 7 8 9 10 11
defconnect_mqtt(): defon_connect(client, userdata, flags, reason_code, properties): if reason_code == 0: print("Connected to MQTT Broker!") else: print("Failed to connect, return code %d\n", reason_code) client = mqtt_client.Client(client_id=client_id, callback_api_version=mqtt_client.CallbackAPIVersion.VERSION2)
When execute the connect_mqtt() function, it will return a Client instance that is connected to the MQTT broker, and printed the connection status.
MQTT Connected to Broker
Publish Messages to MQTT Topics
publish messages to MQTT topics, let’s create a publish() function that will publish a message to the specified topic every second. Using client.publish() method, we can publish a message to the specified topic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
defpublish(client): msg_count = 1 whileTrue: time.sleep(1) msg = f"messages: test {msg_count}" result = client.publish(topic, msg) status = result[0] if status == 0: print(f"Send `{msg}` to topic `{topic}`") else: print(f"Failed to send message to topic {topic}") msg_count += 1 if msg_count > 5: break
MQTT Publish Message
Subscribe to MQTT Topics
To subscribe to MQTT topics, we can call the subscribe() method of the Client instance. We can define a callback function that will be called when a message is received on a subscribed topic. The callback function on_message() will be called when a message is received on the subscribed topic.
1 2 3 4 5 6 7
defsubscribe(client: mqtt_client): defon_message(client, userdata, msg): print(msg) print(f"Received `{msg.payload.decode()}` which sent by `{client._client_id}` from `{msg.topic}` topic")
To run the MQTT client, we can use loop_start() method to run the client in a separate thread, loop_stop() method to stop the client loop. We also can call the loop_forever() method of the Client instance. This method will block the current thread and run the client indefinitely.
1 2 3 4 5
client.loop_start() # start the client loop client.loop_stop() # stop the client loop
# or client.loop_forever() # block the current thread and run the client indefinitely
Full Code
Here’s the full code that uses the Paho MQTT Client library to connect to the MQTT broker, publish messages to a topic, and subscribe to a topic.
defpublish(client): msg_count = 1 whileTrue: time.sleep(1) msg = f"messages: test {msg_count}" result = client.publish(topic, msg) status = result[0] if status == 0: print(f"Send `{msg}` to topic `{topic}`") else: print(f"Failed to send message to topic {topic}") msg_count += 1 if msg_count > 5: break
defsubscribe(client: mqtt_client): defon_message(client, userdata, msg): print(msg) print(f"Received `{msg.payload.decode()}` which sent by `{client._client_id}` from `{msg.topic}` topic")