Categories
Linux Networking Python Ubuntu

MQTT Python Client

In this article, I want to create a python module which would connect to mosquitto server. We can connect a MQTT client as subscriber or publisher of message.

Mosquitto python module has been donated to Eclipse Paho project, which is open-source client implementations of MQTT.  This module can be installed using pip command.

$ sudo pip install paho-mqtt

Subs.py – The code given below is subscribe module. You can look at comment to understand in detail.


#The line imports python mqtt module.
import paho.mqtt.client as mqtt

#Here we initilize variables used in the code.
server = "192.168.0.XXX"
port = 8883
keep_live = 45
topic = "msgTopic"

# Define routines to register.
# Client - The instance at the client side.
# userdata - Data to be passed by the user.
# flags - response flag sent by the server.
# rc - Return code, 0 is successful connection.
# mid - message id
# granted_qos - Quality of service granted by the server, default 0.
# payload - the message received on the topic.
def on_connect(client, userdata, flags, rc):
 mqttc.subscribe(topic, 0)
def on_subscribe(client, userdata, mid, granted_qos):
 print("Subscribed to: "+str(mid)+" "+str(granted_qos))
def on_message(client, userdata, msg):
 print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))

# Initilize a client instance and ser password for the connection.
mqttc = mqtt.Client()
mqttc.username_pw_set(username="username",password="password")

# register the routines.
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe

# connect to the server and wait for message.
mqttc.connect(server, port, keep_live)
mqttc.loop_forever()

Pubs.py – The code given below is publisher module. You can look at comment to understand in detail.

#The line imports python mqtt module.
import paho.mqtt.client as mqtt

#Here we initilize variables used in the code.
server = "192.168.0.191"
port = 8883
keep_live = 45
topic = "msgTopic"

# Connect and publish modules
def on_connect(client, userdata, flags, rc):
 mqttc.subscribe(topic, 0)
def on_publish(client, userdata, mid):
 print "Message Published..."

# Initilize a client instance and ser password for the connection.
mqttc = mqtt.Client()
mqttc.username_pw_set(username="mqtt",password="mqtt")

# register the routines.
mqttc.on_publish = on_publish
mqttc.on_connect = on_connect

# Connect and publish the message on a topic.
mqttc.connect(server, port, keep_live)
mqttc.publish(topic,"Hello! How are you?")
mqttc.disconnect()
 Output – Below is the Publish and Subscribe module in action on raspberry pi.
subs_pub

By Abhishek Kumar

My name is Abhishek Kumar. I am a Software Engineer from India. I stay in Pune a city in south western India. Pune is a city known for IT services companies.

The main purpose of the writing this blog is to keep collection of my projects done by me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s