MQTT on Raspberry Pi and ESP8266 with Mosquitto and Micro Python | IoT Essential

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to my channel today in  this video we will discuss about mqtt protocol   mqtt stands for message queue telemetry  transport which is essential for iot application   in this video we will discuss how  we can establish communication   between a raspberry pi and esp8266 node  mcu using mqtt protocol so let's start   in this video we will cover basics of mqtt  setup mqtt broker server on raspberry pi   testing of mqtt protocol setup micropython  on esp8266 micropython code for mqtt   client and then the final testing for mqtt  communication between raspberry pi and esp8266 mqtt is a lightweight publish subscribed network  protocol that transports messages between devices   mqtt protocol defines two types of network  entities a broker server and number of clients broker server receives messages from clients and  route them to the appropriate destination clients   clients are nothing but any devices  that are connected to the broker server   and that clients can be either a mobile phone  or a smart bulb or any electrical appliances   which runs on the low power when clients want  to publish a message it sends a control signal   to the broker server the control signal  contains name of the topic and message   then broker server broadcast  this message to the network   the connected clients which are subscribing to  that particular topic will receive the message so in this video for mqtt broker server i will be  using a raspberry pi where i will set up the mqtt   broker server which is mosquito server and then  i will use an esp8266 node mcu as a mqtt client   enough theory let's starts with the practical   in order to setup mqtt server on  raspberry pi first login to raspberry pi then update the raspberry  pi using single apg update once the update is done then install two library packages install mosquito and mosquito clients okay so once done then we need to enable  the auto startup of mosquito server once   the raspberry pi is restarted to do that you  have to write down this command sudo system cdl another mosquito dot service so this will enable auto startup  now your mqtt broker server is ready in order to test the mqtt service here i  am using two ssh window of raspberry pi   one window i'm using as a publisher which  will send data and another window i'll be   using as a subscriber which will  receive the data so now let's set   the first window as a subscriber  using mosquito underscore sub command followed by the topic test now from the second window i'll be publishing data   to the topic test using  mosquito underscore of command followed by topic test followed by the message now as you can see once i publish the data hello  world from the receiving side it appears here   so this way mqtt protocol can be used  to communicate between devices next   i will replace this first window which i set up  as a subscriber with a small wi-fi device named   esp8266 i will program esp8266 as a mqtt client  so that it can receive message from the publishers   on a particular topic such as test based  on the message received esp8266 can perform   certain operation such as turning on or  off the light or any electrical devices   for this video i will be using the inbuilt led  of esp8266 to turn it on and off based on the   mqkt message this is esp8266 a small wi-fi device  which i will program to use as an mqtt client   this small device has several gpio pins which can  be used to control external appliances or sensors   there are several ways to program this device  most common ways are either using arduino ide   with c plus plus or micro python in this video i  will be using micropython to program this device   in order to set up micropython in esp8266 you  must have python installed in your machine   then you have to install two libraries in your  machine using pip command first one is esp tool   you need to write down pin install esp tool in my case it is already installed but  if you are doing it for the first time   you have to install it second you have to install adafruit this is also installed in my system next you need to download the micro python  firmware for esp8266 so for this you need to go to   micropython.org website then go to download then select  your board this is esp8266 in my case and then i have to choose the category  of the board so nodemcu always have 2   mb plus flash memory so we have to select 2mb plus  flash memory and under this category will have   lot of firmwares available so i will suggest  to download the latest format which is 1.17 now after downloading the format you need to  connect the esp8266 with your computer using   the usb cable so this is micro usb   port and after you connect the esp8266  you can see a blue light flash here next you need to check the port number where  esp8266 is connected to get this port number   you need to open device manager of your computer  then you have to check for the ports com lpt if   you expand then in my case this is the esp8266  driver which is installed and the port number is   com3 so i will be using this port number  to erase and write flash memory of esp8266 now first i will erase the existing firmware  of esp8266 so to do that i will use esp tool dot i followed by a port number which  is com3 in my case then erase underscore flash now this will erase the flash memory of esp8266 next i will upload the micro  python firmware to esp8266   so i'll be using the same esp  tool dot phi followed by 4 then write flash flash size equal to detect zero and then i will put the firmware file  the bin file that we have downloaded previously okay now this will take a while to upload  the firmware to esp8266 flash memory now esp8266 is ready for micropython programming this is a simple micropython code i have written  for this project where i set up the mqtt server   this is the ip address of mqtt server and i define  a client id client id is unique for each device   then i defined the mqtt topic in my case it is  test and then wi-fi ssid and password in this line   i defined the pin number two which is  gpio2 of esp8266 as an output mode this   gpio2 of esp8266 is connected to the inbuilt led  so i'll be using inbuilt led to turn it on and off   based on the message i received next in  this method i define the wi-fi connectivity after that i define the mqtt callback  service so when this device receive message   then it will check the topic and  the message so based on the topic   and the message it will turn  on the led or turn off the led next under the try catch block first i initialize  the pin the gpio pin with the high value   okay and then i connect to the wi-fi then i  am initializing the mqtt client okay then i   am setting up the callback service and then the  mqtt is connected and last i'm start subscribing   the message for that particular topic in my  case which is test okay so after that under   a continuous while loop i am waiting for the new  message coming from the mqtt server so once mqtt   server sends the message from the other client  this particular client will receive the message   and then it will call the callback service so  under the callback service the topic and message   will be checked and based on the topic and message  it will turn on the led or turn off the led now this code i saved with the name main dot phi  now this is very important in micro python we   have to save the main python file with the name  main dot pi and this is the file which will be   triggered automatically once  you power on the esp8266 after finishing the code i'll  be uploading the main.pi file   into esp8266 to do that i will  be using the adafruit mv command   mp minus p followed by the port number first i  will check the directory structure of esp8266   with ls minus l command so now as you can see  currently we have only one file present in esp8266 next i will be uploading the main.pi file using  mp minus p followed by port number then put   followed by the full path of the main dot pi file now the main dot pi file has been uploaded to  esp8266 we can verify with the ls minus l command   so as you can see currently we  have two files boot.i and main.5 once we power on the esp8266 the boot.pi will get  executed for the first time and then it executes   the main dot pi if you put any other name in  the python file it will not going to execute   so we have to put the main dot pi  as a name of the main python file   now i open one ssh window of raspberry  pi this ssh window i'll be using as a   publisher so from here i will publish message  and esp8266 will receive the message and   turn on the led or turn off the led based on  the message so let's starts with uh mosquito up followed by the topic test followed by the  message if i send the message on it will   turn on the led so as you can see the  led is turned on now the same message   or in the same command i'll change the message  from on to off this will turn off the inbuilt led this way we can establish communication between  devices with mqtt protocol with this mqtt protocol   concept we can build iot applications such as home  automations and other iot applications so in my   next video i will start a project of building  diy home automation system and i will be using   mqtt server as one of its component you can  get all this code and step by step process   in the description and in my github page  so stay tuned till then thank you and bye
Info
Channel: AS Tech-Workshop
Views: 13,634
Rating: undefined out of 5
Keywords: mqtt in raspberrypi and esp8266, esp8266 publishing dht22 readings with mqtt to raspberry pi, esp8266 raspberry pi wireless communication, nodemcu mqtt client, esp8266 talking to raspberry pi, esp8266 mqtt client, esp8266 mqtt example, How to MQTTesp8266 mqtt tutorial, Esp8266 mqtt, How to get started with Mosquitto MQTT broker, mqtt, micropython, esptool, ampy, esp8266, connect nodemcu to raspberry pi, 1MQTT How it works, mqtt explained, What is MQTT, mqtt tutorial internet of things
Id: nFLWFJrqLDw
Channel Id: undefined
Length: 16min 57sec (1017 seconds)
Published: Thu Nov 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.