ESP32: MQTT Setup and Testing with Raspberry Pi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to bald guy diy in this video i'm going to show you how you can get the esp32 microcontroller communicating with the raspberry pi and other devices using the mqtt protocol [Music] as you've probably seen on my channel before i love the esp32 microcontroller the price point the full feature set i'm always discovering new things that it can do and finding new ways to use it i've been making a lot of different projects with it lately some of which i've posted videos about but lately i've been delving into something new which is communicating with the esp32 and a raspberry pi and in order to do that i've been using the mqtt protocol which is a message queuing telemetry transport protocol though it sounds very complicated it's actually super simple any message that's being sent has a topic and if the device on the receiving end is listening for messages of that topic it receives the message when it's sent out and likewise when it wants to send a message it says the topic that it's sending and the message and if another device is listening to it the message is sent and received the theory behind it is very simple and i'm not going to delve into that a lot i'll show you a resource that i use to get it up and running while the technology could be quite complicated to explain i'm not going to do that i'm simply going to show you how to set up an esp32 and a raspberry pi in order to send these mqtt messages and trigger a response when they're received so if that's the kind of thing you're interested in without further ado let's jump into it mqtt uses what's called a broker which acts as a server or traffic director or mail carrier which passes messages back and forth there's a great tutorial on the random nerd tutorials website i'll basically be following that as i show you how to install the mosquito mqtt broker on a raspberry pi you're simply going to do a pseudo app update so that you're getting the most current version and then do a pseudo apt install mosquito the default is going to say yes automatically and install the client software as well when it's installed it's basically going to be running the mosquito server already the last thing we want to be able to do is install a service so that it'll continue to run in the background every time we have the pi up and running that last command there is enabling the service and as you can see when you finish and do a mosquito dash v you do see that the server is running and already working correctly to direct messages to be controlled by the broker we need to know the host name of that particular device so in this case you could do a hostname search you could also hover over the wi-fi logo in the top right hand corner and you can see what the currently assigned ip address is of this raspberry pi now the random nerd tutorial was great but if you find the one by pie my life up you'll see that it has some additional features and a couple steps that i want to refer to is how you can wait for messages and publish messages using the terminal window and not having to just immediately go to some actual code so if you google pi my life up and esp32 mqtt you'll find two commands here and mosquito underscore sub and a mosquito underscore pub command which has some variables here the mosquito underscore sub is how we subscribe or listen to a particular topic and message from a particular host so you can see here mosquito underscore sub-h localhost is the name of the host you could also put the ip address you would need to if it's a different device but because we're running it on the same pi that is running the server local host works then a dash t is gonna t is going to designate which topic it's listening to and when we hit enter you can see it's just gonna sit there and wait it's listening for messages that have that topic and it'll just sit and sit and sit waiting for any messages for that topic and as long as it takes to get them if we open up another terminal window we can use that second command which is mosquito underscore pub or publish the first one sub or subscribe this publish is actually going to send messages and it follows the same kind of structure so the dash h to designate the host dash t does it designate the topic and i'm just going to make sure the topic that i used when i subscribed which is my underscore topic is going to be the same topic that i'm going to publish a message to here the additional switch is dash m which includes message text that you're going to be sending as soon as you press enter you get almost an immediate result where the message is received by the listening device if we were to change that message to something else you can see still testing or still waiting and hit enter bang it's there immediately almost instantaneous handling of that message depending on your network traffic of course this is hosted right on the same device so it's very very quick but even posting from other devices on the same network in my use anyway it's been almost instantaneous really really quick and easy to use as long as you're subscribed to a topic you're going to get every message that's sent to that topic so let's take a look at what the esp32 requires and for this i'm going back to a random nerd tutorial esp32 mqtt how to publish and subscribe with arduino ide this is basically a sample script that i got from there it tells you how to install the pub sub client there's a few different ones in the library this one was not in the default arduino ide library system and it did need to be installed separately by using the wi-fi and pub sub client libraries we're going to connect to a wireless network and then we're going to be able to join these topics and receive and send messages there are some default functions and initializations that need to be done if you look through the tutorial be able to see everything that's used there i've really stripped away all of the unnecessary stuff because a lot of these tutorials tend to add specific use cases and in this one i wanted to make it as generic as possible so it was easy to see the setup loop has very simple things just setting up wi-fi connecting to the mqtt server which is our broker and then declaring in here the setup of wi-fi it's just making sure that it's going to have a successful connection if you have trouble with an esp32 connecting to wi-fi check out the video that i made recently callback function this is going to be where it receives all of these messages so this is the most important area for editing and creating the right kind of message handling for when a message is received it's going to run through a for loop here which is going to convert all of the message that's received into a variable called message temp and this little section here is where you're going to be able to put all of your message handling if you want to trigger an action when a topic is received then you can do that there if you want to trigger the action when a message is specifically received to a topic you can do that and if you want to send out messages on the topic you can simply publish just like you could on that terminal window on this particular case if i receive the topic of my topic and the message of message i'm going to send back a message or publish one to the my topic topic and just say the message has been received there's an lcf here that isn't really necessary i've just left from the previous example code i was using and that's basically it for message handling if we go to the reconnect function this is both connect and reconnect and so it's important here to have the designation for what happens to subscribe the most important thing from this of course is that it's going to reconnect if it loses connection but hidden in there is the need to subscribe to any topics that you desire to be notified of and to receive messages for for each one that you want to be notified of you just put client dot subscribe and then put the topic name in the quotation marks inside that's basically the handling and then in the loop you're just going to reconnect if necessary and just keep looping through listening for those messages of course you could also add additional things to the loop function so that you could run other processes on your device but all the message handling for mqtt happens in this callback function if we load that we see here that it is connected to mqtt and now if we send a message out using that terminal window and the message being just message you can see immediately on our listening code the message was sent out and we got a response back from the esp32 which said message receive if we look on the output of the serial monitor for the esp32 we also see here that it received the message and that it sent back the message received string that it was programmed to do now let's look at the library required to do this on python and you can use here the peho mqtt library and this could be on a raspberry pi or on a windows or mac machine you're simply going to do a pip install there's a lot of information on the page there are great instructions on the pi pi repository so check that out you're just going to do a pip install pajo dash mqtt if i look at some sample code here and it's very easy to find sample code online you can see here that it's going to run and kick out a message to my topic with and the message just being hi from python on a mac which is the computer that i'm using here it receives it on the listening code right away exactly like it did from the esp32 now if i was to install pejo.mqtt on a raspberry pi it'll behave exactly the same because it uses python just like you can run on a desktop computer so if i install it here using the pip3 install payo mqtt and then run the exact same script that i was running on my mac laptop i'm going to change the device name to raspberry pi so that it's a little bit different and unique and then i'm simply going to bring up that listening window run our code and you'll immediately see once i've actually clicked run that we're going to get a message hi from python on a raspberry pi so any device that's capable of connecting and sending these messages is able to communicate together which means you can now send information and receive it from the esp32 and any other device that's capable of sending these mqtt messages that's extremely powerful one more time i'm just simply going to send that original message which we have message handling for on the esp32 and because it's always just listening even after doing all of those other messages it's going to respond immediately with its programmed response so there you can see once you have the protocol initialized and the default functions established choosing what message to send and what actions to follow when a message is received it's very simple and you can create hundreds maybe even thousands of different messages that are sent and received using this system it's great to communicate between unlike things so esp32 to raspberry pi to computer and many different other things i've also been using it a lot lately with the node-red platform and i might show that in some future videos because that's truly incredible how you can link these visual nodes together and have the communication triggering all sorts of incredible events both device to device and also through a web browser that you could use on a computer or smartphone or anything like that if you'd like to see that kind of video let me know in the comments and i'll be glad to share it in the upcoming week what kind of application do you have for mqtt and what would it make your life simple there's tons of home automation escape room all sorts of other kinds of uses for it and for me personally i just can't wait to see the kinds of things that i can do with it in the future and to hear from you the kinds of projects that you've been using it for if you like this kind of content please give the video a thumbs up subscribe to the channel and check back regularly as i post a new video every week on a wide variety of diy topics some electronics some 3d printing building all sorts of different things i have many many interests if you have a comment or question let me know in the comments section or send me an email my information is in the description below and that's all i have for this week so until next time in all your diy projects however you get the message across don't be afraid to be balder
Info
Channel: BaldGuyDIY
Views: 8,857
Rating: undefined out of 5
Keywords:
Id: YbCP0WQiDlw
Channel Id: undefined
Length: 11min 40sec (700 seconds)
Published: Sat Aug 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.