
Introduction
Hello folks , welcome back to Techrover . Have you ever tried making an IoT application for controlling your smart devices ? . Its so crazy to make your own application that controls your smart devices.
Contents
Overview
Today we gonna learn how easily we can attach an MQTT service with our Django application.
I hosted an MQTT service on my server and publish some data over a channel. We are going to setup an MQTT client in our Django application and spin-up the MQTT as service to listen the channel.
Prerequisites
- A running Django application
- Hosted or locally running MQTT server
Installation
To get it working we need to install an MQTT client along with our Django application packages . I have chosen ‘paho mqtt client’ because its more compatible with python. To install paho mqtt client ,
pip install paho-mqtt
Always remember to use a virtual environment when installing packages , as I always say.
Coding
To write our mqtt logic we need to create a file called ‘mqtt.py’ . Inside this we will be writing our program logic , subscriptions and listeners. For example,
import paho.mqtt.client as mqtt
import json
def on_connect(client, userdata, flags, rc):
if rc==0:
client.connected_flag=True #set flag
client.subscribe("update")
client.subscribe("ping")
print("MQTT connected OK")
else:
print("Bad connection Returned code=",rc)
def on_message(client, userdata, msg):
topic = msg.topic
print(msg)
print("message received")
#msg.append(m)#put messages in list
#q.put(m) #put messages on queue
#Do something .....
mqtt.Client.connected_flag=False#create flag in class
broker="127.0.0.1" #replace with your mqtt server ip
client = mqtt.Client("server") #create new instance
client.on_connect=on_connect #bind call back function
client.on_message = on_message #bind an action method
client.username_pw_set(username="your mqtt username",password="your mqtt password") # replace with your credentials
print("Connecting to broker ",broker)
client.connect(broker) #connect to the broker
Now all we have to do is to create a loop that listen to the activities around the mqtt server which we have configured . We cannot create a while loop to do the task . The best practice is to do this is to write it in the __init__.py file . Instead of using loop_forever() we are using loop_start() , so that it will not effect the main background thread.
from . import mqtt
mqtt.client.loop_start()
Testing
I have subscribed a topic called ‘ping’ in our mqtt.py file so that it can listen to the topic ping . I am using my command prompt to publish data to the mqtt server. Every clients subscribed to that topic will get notified when I publish data. Let’s see,

The left screen is belongs to our Django application console and the right one is command prompt. We can observe that the message is received at our end .
This can be used to control and monitoring your smart devices , your home security application baby monitoring or anything .
Conclusion
What I have written is just a basic block of codes to get connected with your mqtt broker with a username and password . You can make it more secure by setting up TLS ports tokens and many more things .
I hope you got a basic idea of integrating MQTT client with our Django application. Do comment if you have any doubts or need any assistance for building you application . Please do share the article so that it will encourage me to write more posts.
Quite simple and brief informative content..!
Looking forward for many more topics
Thank You Anu.