How to setup MQTT for Raspberry Pi and ESP32 for IOT and Robotics projects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi friends today let's see how modern embedded devices such as raspberry pi and esp32s can seamlessly exchange data using popular mqtt protocol there are enough reasons to learn mqtt implementation and this video can give you a head start to apply this technique in your next iot or robotics project so here is our demo setup with a raspberry pi and two esp32 microcontrollers the client programs running in these devices are going to publish and subscribe sample information on mqtt network using the three topics as shown the client programs are written in a fail-proof manner in case wifi or mqtt broker is down clients can auto-connect to them on availability without any manual intervention and ensure flow of information let's see the demo and code for this implementation on my computer screen you can see that i have taken remote of my raspberry pi and on this camera stream you can see the two esp32 microcontrollers esp32 is an amazing piece of hardware it has built-in wi-fi and many other advanced features at a very low cost these are the client python scripts placed in download folder i have already installed mqtt on my raspberry pi using these commands we can check the status of broker it is showing active now let's run our python scripts the first script successfully connected to the broker and waiting for data now when i run the other script which publishes sample data we can see that data started appearing on the first script the code for the script is simple and less than 50 lines we simply create a mqtt object and it connects mqtt broker running on the same machine if your broker is running on any other linux machine just change this ip address here callback functions are added to handle the incoming data on these three topics inside the callback functions i am just printing the received data on terminal you can place your data handling logic here the other script is used only for publishing sample data it also connects to the broker and starts publishing a counter value every two seconds on this topic so if i stop the script you can see that the other script also stopped receiving the broadcast message now let's power up one esp through laptop's usb port so that we can see its code activity on serial monitor after booting it first connected to wi-fi obtained its ip address and then establish connection with mqtt broker now we can see that our script on raspberry pi started receiving data from this esp the data is just a fixed value confirming that things are working this part of the system is now working let's power up the other esp using a power bank it also connects itself to wi-fi and broker and starts publishing data which can be seen here this part of the system is also working now now let's turn on our broadcast script notice that this broadcast message is received by raspberry pi and esp simultaneously the complete setup is now working stopping the broker would result in disconnection of all the clients the moment broker is stopped you can see both the client scripts and esp lost connection but they are still running and trying to reconnect so when the broker is again up the clients start connecting to it automatically without any manual intervention and the data flow is resumed as expected if any of the device loses its power or gets disconnected temporarily when it is powered back it resumes its role as programmed both the esps run identical code with a little difference let me show you the difference while connecting to the mqtt broker they should connect with a unique name they can receive the same broadcast but they can be programmed to process this information differently they publish to different topics rest of the code is identical with these differences in mind you can create a swarm of these devices all reporting their individual sensor data to a central location and getting control direction from a master device the links for source code and written guide for this project are available in the description below also check out this website for more projects on robotics and iot i hope you enjoyed this video stay tuned for more such videos and thanks for watching
Info
Channel: Jitesh helloworld
Views: 18,640
Rating: undefined out of 5
Keywords: mqtt, mqtt broker on esp32, mqtt project, mqtt raspberry pi arduino, mqtt raspberry pi 4, esp32 iot projects, home automation esp32, raspberry pi esp32, iot projects with source code
Id: ebsXSCKsHeQ
Channel Id: undefined
Length: 6min 22sec (382 seconds)
Published: Tue Jul 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.