Hello and welcome to High voltages. I am back with another video of the HOW to
MQTT series. In this video we will implement the MQTT protocol
in python and make a dashboard that will show data coming from ESP8266 through MQTT protocol. So, we will start with installing the library
for MQTT protocol in Python, then we will write code for communication with MQTT broker,
we will learn how to publish and subscribe to a message and in the end we will make a
dashboard to show subscribed data and to publish data. You should know what MQTT protocol is and
how the publish/subscribe model works and how to select the topic names, which I have
explained in my last videos in detail. If you have not watched those videos, watch
that before coming to this one as I will be referring to a few things from my previous
videos. The data which we will be showing on our dashboard
will be coming from the ESP8266 DHT11 sensor, which we programmed in our last videos and
also if you prefer MATLAB, I have made a video that tells how to use MQTT protocol in MATLAB. The reason for choosing python is that python
is easy to learn and use, and the community of python is very strong and there are hundreds
of libraries and frameworks that you can use in python for free. Another reason is that when I made the last
video MQTT in MATLAB, people complained that MATLAB is not free, so they can use python
and they can do anything with data on python that they can do on MATLAB as well. For this video, we will be using REYAX technologies
RYC1001 MQTT cloud, Whish is built on stable AWS services. REYAX technologies are the sponsor of this video So now let's start the project. I am using Pycharm and python 3 and i have
created a new blank project, so we will first go to terminal and write pip3 install paho-mqtt
to install MQTT library and then i will remove the Hello world code
to write our own. Then we will import the library in our project. And we will write few variables that will
contain the information about broker, port , topic for publishing data, topic to subscribe,
client Id, user name , password and device Id. You will get values of all these parameters
from the cloud service you are using. Then we will create a connect_mqtt function
that will help us to connect to mqtt broker, first we will make a client by using the mqttclient.client
function and passing clientid as parameter, and then we will set username and password,
that will help us to authenticate, and then define that when client is connected call
this on connect function, which we do not have right now . Lets create it. So i will create a on connect function that
will take few arguments as a parameter and we will check if we are connected to broker
or not by checking the rc or return code, if it is 0, that means we are connected to
broker. And in the end we will return the client . Now lets create a function for subscribe,
that will take client as argument and we will write client.subscribe and pass
the topic name to which we want to subscribe, and we will write that client..on message
that means if there is any message on our subscribed topic, call on message function,
which we do not have now, lets create it. So we will create on message function with
these arguments. And in the function, we can just print the
message received for now. Then we will create a main function, in which
we will create a connection by calling connect_mqtt function which we created and then calling
the subscribe function so we can subscribe to our topic, and then we will write a command
for loop so that mqtt can work in a loop. And then we will write a code to call the
main function at the execution of code, and you will see that we are connected to broker
and receiving message but the actual message is not printing, i think i am missing something
, lets fix that . I used a normal string but it should be f-string so lets put f so we
can see values of variable instead of variable names. So now if i run this , you can see that data
is coming in form of JSON format. Now lets copy this JSON data and put it in
online JSON formatter so we can extract and parse name-key values. You can see our parameters temp and humidity
are inside notification and parameters. So now lets import JSON library in python
so we can parse data. And in the on message function we will load
the json data in a variable and read the temp and humidity values that are inside notification
and parameters key, now if i will run the program you can see that instead of long JSON
response we are just getting the data which we want. Now we will read temp and humidity value in
separate values like this and lets print it. Now lets make publish function, we will first
define a message that we want to publish, in our case we will be publishing led status
1 or 0 to turn it on or off, and then we will call client.publish that is a function of
pahomqtt library and pass topic name and msg to publish, if that returns 0, it means message
has been published otherwise you should check your credentials or there is some problem
with connectivity. Okay, that was a minimal code for making MQTT
connection, subscribing and publishing message, Now lets create a dashboard so i will create
a new file, and import tkinter library that is used for creating Graphical user interface
in python and then i will create a main window, set the title of our main window, and set
it on loop . and if you will run it you should see a blank window on screen. Now let’s set the geometry of our window
and lets set the resizability off. Now you will see a window with a certain size
and we will not be able to resize it. Now i will add few graphics in our project
that i am going to use in our dashboard so i will paste them in the same directory. And we will now create a canvas to show image
on window, and we will write where to place it in window by giving x and y values, then
we will create photoimage and write address of our image and then place the image in canvas. Similarly i will add other images and will
just change the position of them. Then we will create a flag to store value
of LED and set it to off by default. And then create a label that will show the
status of LED on GUI, so the GUI will now looks like this. Now we will add images for LED on and off. And then we will create a button that will
contain the led off image on default and when it will be pressed switch function will be
called , lets create it now. So we will define switch function, and read
the status of LED, if LED is on we will make it off and change the picture and label and
change the flag value and if it is off we will use the LED on image and change the label
picture. For now just print the values. So now you can see if i am pressing button,
status is changing , now lets add another graphic on footer. And now we will add labels that will show
temp and humidity values. And our dashboard is ready , now we just need
it to connect with MQTT code, so lets do it now. We will first copy the libraries and the parameters. Then we will copy all the functions except
the main one and paste it after the parameters definition. Then in the on_messge function we will update
the values of temp and humidity label on dashboard. And in the switch function we will call the
publish function and publish the value. And in the end just before window.mainloop()
we will make call connect_mqtt function and subscribe function and call the loop of mqtt
but note that this time we cannot use loop forever command as loop of mqtt and loop of
gui will make a conflict so we will call loop_start and will call the loop_stop after window.mainloop()
and thats it The mqtt dashboard is ready to test. Lets run it and see the working . You can
add your graphics and i hope after this tutorial you will be able to make your own customised
dashboards with your own graphics. Thank you for watching and in the next videos
i will add the realtime plots in the same dashboard and we will save all the data in
excel file. I will see you in the next video.