Setup and Install Mosquitto Broker- Raspberry Pi4 as MQTT Server

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends welcome to another vaspberry Pi programming lesson and in this lesson I'm going to show you how to install mosquito broker on Raspberry Pi 4 which will make our Raspberry Pi as a mqtt server and then we will test the mqtt publish And subscribe messages and then I will show you how to write a Python 3 script to publish And subscribe the mqtt messages and finally we will test the mqtt messages with a third party client mqtt Explorer thanks to Altium software for sponsoring this video Altium is all in one platform for all the needs of visible design the cloud-based RTM 365 offers seamless collaboration between Engineers along with Version Control capabilities please visit the link in the video description to learn more about Altium 365 with built-in octo Parts Engineers can quickly search electronic components with accurate and complete data for millions of electronic components you can download free trial with the link given in a video description also you can sign up with the linkultium.com forward slash YT slash binary updates let's first install the mosquito broker in order to install the mosquito broker we have to first update and upgrade our Raspberry Pi operating system so run sudo apt update and hit enter so this will update the Raspberry Pi OS then run sudo apt upgrade and if in case you face any trouble to update and upgrade any error or something I have already made a video I have given a link in a video description in case if you need but it is very important to update and upgrade your Raspberry Pi operating system before you install mosquito broker on your Raspberry Pi now once you done with update and upgrade now we have to run a command sudo apt hyphen get installed mosquito and mosquito hyphen clients and hit enter this will install the mosquito broker and you can see in my case it is already installed now to check version of mosquito you can just type mosquitto base minus V for version and hit enter and now as you can see the mosquito broker is already running it's a 2.0.11 and it is already running so let me run a clear command so clear and hit enter so we will have a little bit of space in order to auto start the mosquito when the Raspberry Pi boots we have to run a command it's a pseudo system CTL and then enable and then same mosquito dot service and hit enter and this basically auto start the mosquito server so whenever you boot your Raspberry Pi it will run the mosquito server right in the background onto the Raspberry Pi 4 and then we have to do some settings we can test publish And subscribe messages so to test messages we have to run a clear command here and then we need a two terminal so we have this first terminal let me run clear again and this is the first terminal that I have and I open one more terminal so I have now two terminal I can minimize this put one terminal on the right side and one is on the left side let me run clear command and then the one terminal will be a subscriber and the other terminals will be publisher so to subscribe the mqtt topic we have to run a command mosquito excuse itto I'm just a little bit conscious if I make a spelling mistake so mosquito underscore sub to subscribe minus t for message to debug and then minus t for topic and then we have to give the topic that we want to subscribe and let's say we have to subscribe to the topic binary slash updates okay and hit enter and this will subscribe the mqtt message binary slash update and on the other terminal I'm going to publish so mosquito underscore Pub or publish minus D to debug messages minus t for the topic and the topic that we want to publish on is binary slash update this is the message and minus M for message what message we want to send best iot and Embry courses right so let me hit enter and you can see now I received this message best iot and embedded courses on to the terminal where we have subscribed to this mqtt message binary slash updates okay now I show you the most tricky part let me just minimize this both nominal if I open the mqtt XP loader and you can see here we give the name here anything let's say I would give Raspberry Pi 4 R Pi 4 and guess what the mqtt server's IP address would be the IP address of our Raspberry Pi right because now our Raspberry Pi is our mqtt server so if I take the IP address of my Raspberry Pi so let me copy from my bitwise SSH client and let me put it here and when I click on Save and when I click on connect it doesn't connect look at this on a top right corner it says disconnect from server because the server has to give a permission to the client and client is mqtt Explorer so this would not happen automatically for this we have to take a couple of steps okay so let me run Ctrl C and for this to enable our server to give permission so that other mqtt line could able to communicate with it we have to edit one file and let me do that sudo Nano and this is no longer needed right otherwise this will confuse you so let me just skip it minimum possible here so sudo Nano and then slash CDC slash foreign key till we reach to the end of the file and then we have to add two lines of code and the one line of code is listener l-i-s-d-e-n-e-r listener and we have to enter the mqtt port and it is 1883 and make sure you don't make any spelling mistake okay and then we have to say allow and underscore Eno nimas and then we have to say true we can give a lot more settings here we have to add the username and password and all other things but right now to keep it simple our Raspberry Pi is a mqtt server and it will listen to Port 1883 which is an mqtt port and this will allow any Anonymous client to connect to our server right that means our Raspberry Pi now to save the file Ctrl o and hit enter so this will save the file Ctrl X to get out of the file and then if you want you can just say sudo reboot this will reboot our Raspberry Pi and this is basically a good practice so let me close this all Terminals and let's test what happens when it reconnects so let me wait a little bit and you can see as it trying to reconnect we have to click on accept for this connection and now we have a new terminal and let me just open two terminals right so one is on the left side and the other one as the right side right so now we can test the mqtt messages so let me run the previous command that we run to subscribe let me and let me enter and now you can see mosquito underscore sub minus D minus t binary slash update so this is the topic which is subscribed to show you now it is working let me just open mqtt Explorer right so this is our mqtt Explorer and now you remember previously when I tried to connect This mqtt Server it was failed and now you see when I hover over it says online right because our Raspberry Pi is now exiting the connection so let me just pause this stop for now and let me just show you disconnect and now let me show you this is our Raspberry Pi's IP address and then click on save click on connect and now you see it is connected it doesn't show us a rate right it shows us a green and it says online so what I'm gonna do is I have this publish section here so I would say binary slash updates and again I would send a message hello from omesh so my name is Umesh so when I hit on publish button here you can see the message has been received on the Raspberry Pi which is our server and this mqtt Explorer is our client right so let me just now subscribe to the topic here now to subscribe the topic on mqtt Explorer let me just minimize this otherwise this will be this will look extremely ugly right and maybe not very good now to show you how to subscribe on mqtt Explorer we have to click on disconnect first and then we have to click on Advanced here and in advance we have to put the topic that we want to subscribe and our topic is binary slash updates so this is the topic that we have to subscribe for so let's put the topic and then click on this add plus button here and you see now the topic is added and then click on back and click on Save and click on connect and now our client mqtt Explorer is connected to our mqtt server which is our Raspberry Pi now if I go to Raspberry Pi terminal here let me just make it small oh it looks extremely ugly right so let me keep it here and let me explain I mean not split but let me share the screen between this two okay now I think you can see it's quite visible right and if I run the message here mosquito underscore pop minus D minus t and then the topic is binary slash updates and best iodn embedded system causes let me hit enter and you can see now here on our left side I have received this message best iot and embedded system courses right now I think you know now how you can publish And subscribe mqtt messages so I hope you get my point right how we can publish And subscribe the messages and now apart from that I want to show you something cool okay so let me just press Ctrl C to stop blind and I will make this bigger one and let me run the clear command on on the boat terminal this is like a manually publishing and subscribing the messages using mqtt commands now what if you want to write python code and you have to publish And subscribe using the python code so for that you have to go to open up the browser and then you have to just say GitHub binaryupdates.com and this is the GitHub page I have already shared the link with you so don't worry this is the repository mqtt client and once you come to this page this is github.com binaryupdate slash mqtt client I have posted this link here so you can just quickly come to this page and then click on this code button here arrow and then we can click on this button here which will basically copy this URL GitHub URL so click on this so it will copy otherwise you can manually copy this entire URL and then we will go to our Raspberry Pi terminal and we will clone the GitHub repository so sudo git clone and then I will paste the URL and let me hit enter in a home directory now if I run LS command you can see I have this mqtt client folder shows up right so even on the right side of the terminal if I run you can see mqtt client shows up on both sides so let me go inside the mqtt client folder cdmqtt client again on our right side cdmq GT client and then I run clear command on both terminals so we are into the mqtt client directory and run LS and LS and you can see I have given one publisher python script and one subscriber python script okay the same on the both side of the terminal so on the one side of the terminal I will open sudo Nano subscriber dot pi and on the right side of the terminal I will open sudo Nano publisher.pi so this is the Python 3 code that I have written just to show you how you can write a python script and then publish And subscribe your mqtt messages so you can see here mqtt host here so this is the IP address of the mqtt server so let me just come and replace the IP address so the IP address of our Raspberry Pi that will be our mqtt server's IP address so 225 192.168.225.52 okay this is the IP address of Raspberry Pi and this basically subscribe to hello world topic and the message is basically it doesn't matter because this is a subscriber script and this will subscribe to hello world topic so let me press Ctrl o enter so this is saved on the right side of the terminal you can see there's again the IP address that we have to replace so let me go to this line and here 225.52 that's the Raspberry Pi's IP address because it's our server and then you can see in the publisher we have this topic hello slash world and we will publish this message Hollow mqtt okay so let me just save the file Ctrl o enter now we can run this python script okay Ctrl X to get out the file Ctrl x on a word terminal on the left side of the terminal I would run python subscriber dot pi and once I run this it says there is this module cannot found which is a power mqtt so this is the mqtt library that we have to install so sudo with install hyphen mqtt so this is the python Library python client library for mqtt protocols so our client has to support the mqtt protocol and that is why we have to run this command let me hit enter and this will install the mqtt library on our Raspberry Pi and it's done all right now if I run python oh this is spelling mistake python subscriber dot by hit enter and now you can say subscribe to mqtt topic and you remember the topic hello slash World okay and now on the right side of the terminal I run this command python oh again I make a spelling mistake so on the right side of the terminal I should type Python publisher.pi and when I hit enter guess what hello mqtt will be printed on the left side of the terminal okay because it will publish the mqtt message hello mqtt let me hit enter bam and you can see on the right side of the terminal I received hello mqtt okay let me now test with our mqtt test client and it is mqtt Explorer so here is our mqtt Explorer and here is our topic that let me just close and let me put the topic Hollow slash world and the message that I have to say hello from let's say binary update and let me click on publish there's no publish but and here's a publish button let me click on publish and you can see this message is coming from our client python client Python 3 script client to our mqtt Explorer and from mqtt Explorer to the Python 3 client okay you can test the other way around as well you have to first disconnect click on disconnect and then go to advance and now you have to remove this topic binary updates and just say hello slash World click on ADD back click on Save click on connect and now we have to subscribe from we have to publish from this Raspberry Pi terminal so let me hit python space publisher.pi file hit enter you say hello mqtt and now if I show you the mqtt Explorer you can see the message shows up here hello mqtt so this is how the Python 3 script is sending from our Raspberry Pi to our mqtt Explorer and how mqtt Explorer is sending back to our Python 3 mqtt client which is running on our Raspberry Pi I hope you have found this video educational and entertaining you have learned how to make your Raspberry Pi as an mqtt server and then how you can connect mqtt Explorer as a client to our to your Raspberry Pi as a mqtt server you can connect node MCU using Pub sub client library or you can correct any iot device to your Raspberry Pi and treat your Raspberry Pi as a mqtt server because it has the mqtt broker mosquito installed on it if you want to learn more about mqtt protocol and iot platform related video lessons then please check the link in the video description I have already covered many of such a Topics in Greater details I hope you have found this video educational and entertaining will see us into the next video Lesson bye bye for now
Info
Channel: BINARYUPDATES
Views: 12,398
Rating: undefined out of 5
Keywords: Mosquitto, MQTT, Broker, Raspberry Pi, Raspberry Pi4, Server, Install
Id: BFyPzC6No8k
Channel Id: undefined
Length: 15min 0sec (900 seconds)
Published: Fri Aug 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.