ESP32 Tutorial 48 - Remote Temperature Monitoring and LED control MQTT | SunFounder's ESP32 IoT kit

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this lesson we are going to learn how  to use the esp32 module with Sounders esp32   extension board and add a fruit IoT to monitor  temperature over the internet as you can see it   shows temperature and humidity and also we will  be able to toggle the LED on and off from the browser esp32 starter kit from SunFounder this  is the best esp32 learning kit from SunFounder it   has this esp32 microcontroller which has built in  Wi-Fi and Bluetooth this board can do everything   or Arduino Uno can do or many other or DS can do  plus extra more features because we have Wi-Fi and   Bluetooth the possibilities are endless you can  get connected to the cloud or do the control or   read information or values via your mobile device  or your desktop or over the cloud from a far location it comes with a camera extension  kit where you can stream the video over   Wi-Fi either through the cloud or to your  network and also it comes with micro SD   card where you can save images on the micro  SD card or you can write from the device any   information log the information on the device  and read it back you can power the board using   this included 18650 lithium battery and it has  built-in charger where you can connect micro USB   and charge the battery the kit comes with 320  pieces of component that you can learn tons of projects come to this page docs.SunFounder.com  scroll down until you see esp32 then click on   esp32 starter kit main on the left side click on  Arduino user and here scroll down to 8. 6 this   is the documentation page for our lesson  we need esp32 board esp32 extension board   breadboard jumper wires resistor LED and dht11  temperature sensor we're going to use Ada fruit   IO and we're going to open our account we will  send information to the server and also turn on LED and here the wiring we will install two libraries at the fruit  MQTT library and DHT Library which we already uh   used here is a code which I'm going to explain it  to you there is some explanation for the code and   as how to control LED on and off and also we  will be able to read temperature and humidity online let's get introduced to MQTT MQTT stands  for message queuing Telemetry transport uh why we   need it because it's lightweight and efficient  MQTT is bidirectional MQTT can be scaled to   millions of things and it uses reliable messaging  delivery system it is secure and it is persistent   over unreliable Network so first we have to  understand the elements of MQTT we always need to   have a broker and we have MQTT subscriber we have  MQTT publisher and the most important thing in the   broker is the topic uh for which the messaging is  being published or subscribed now this is internet   of thing from mt.org and we have client broker and  another client which is subscriber in this case   we have a temperature sensor sends information to  the broker and the temperature is listed or queued   on the cloud and our subscriber can read or fish  a particular Topic in this case temperature the   topic is temperature and we read the temperature  and if we use a micro controller such as Arduino   or esp32 this uh in this case we use it as a  publisher and we have MQTT Broker in this case   we are using Ada fruit IO and then we have MQTT  client in this case a subscriber a lap mobile   device or a computer so the temperature from  temperature sensor is read by esp32 and because   this acts as a publisher we are publishing it as a  topic of temperature to the broker and it is being   list and queued there it just Waits until the  request comes or uh the device is connected so we   can read the temp pressure on the screen this was  how it is used as a publisher now let's see how   we can use it as a subscriber we have ADD fruit  uh MQTT broker service and then esp32 is used   now here as a subscriber and we have subscribed  to a topic called LED and then MQTT client is   publisher is publishing on off uh to the MQTT  broker so when they touch or click on the icon   on or off the information is queued here for the  LED topic remember this is a topic and we can have   millions of topics so one topic is called LED and  because we have subscribed to that topic of led   the information comes either that topic says high  or low or on or off depending on that we interpret   and read the information and turn on and off the  LED based on the uh action that is taking on the screen now let's click on add a fruit IO we're  going to create an account click here on the sign in let's create an account here by C sign up so once you open an account log in now once you logged in you can  click on dashboard here click on   dashboard you're on this screen let's  create a dashboard so we can put all   the contents for our projects in that  dashboard click new and let's name it um ESP 32 1 create so this has been created now click  on the dashboard that we created and from this   Arrow let's create new block first we are going to  create a toggle button for Led click and on this   is screen name it l and this create button now  is activated and I move the mouse and it has been   created now select it to go to the next step and  here we can have title for this led this could be off and this is on let's put it like that  and create blog now this has been created   now click on feeds you will see the LED  is here let's go back to the dashboard   click and here again let's C create  a block for temperature this time we   select text and then here we can create  feed for the block as well so let's call it then um temper and click create selected after  that click next and here let's title it   temperature select here the font size that it  displays the temperature let's select big I'm   not going to select anything just create click  it will be lock temperature let's create one more text again this time humidity create and then select  humidity go to next step and then title humidity select B create  block now in this screen we can   select again edit layout this will allow you to resize like that and also temperature for example if you want to  move something here or there let's put   the temperature in here humidity and then three  of them click save this is now done after that   we click on this icon and we get our username  and active key we need this do not worry I'm   not worried if you see this because as soon  as I click here it will create a new key so   it's okay you can change it anytime my username  will not change and the key will change so the   username is my username whatever you have and  then the active key I'm creating and copying it when you use esp32 with SunFounder esp32  camera extension module like this it comes   also with a battery on the package and it comes  a built-in charger so you can connect it and   charge it and disconnect and later on you can  use it with a lot of power so you can power up   your application very easily and here's the  wiring first I've connected these two wires   to 3.3 volts and zero from here and this red  is my 3.3 volts and then blue is my ground   which is connected to the black so I have 3.3  volts on ground so for the D HT I've inserted   such that the left pin is connected using red  wire to this red line 3.3 volts as shown here   to 3.3 volts the second pin is connected here to  the third pin the second pin using yellow wire   it has been connected to pin 13 this is the  third pin you can connected directly to this connector and if you're using the module alone  without the extension board you can connect it   directly to the third pin in here so this is 3.3  volt and that's ground when the USB is facing this   way and then the third pin is not connected from  the left the last pin is connected to the ground   here this pin is not connected and the this is  connected to the ground and for the LED I hold   it like that such Dash longer wire is on this  side I've inserted it this is uh the anode this   is a cathod cathod is connected to the ground  to this blue line and anode is connected using   um 220 Ohm resistor to a point where it is  connected using this yellow wire to pin 15   here so the DHT is connected the third pin from  that side and led is connected the third pin   from this side or you can connect it to the  third pin on the module if you want to use it alone I'm opening Arduino IDE let's open our project by clicking  on file open on the left side click on   downloads then esp32 Starter kit main  on the right side double click to open   it double click on C double click on codes  and then scroll down on this screen until   you see IoT T6 Ada fruit IO so this is  the code we need to install add fruit MQTT I'm selecting it go to library  here and then post type it in add   Adafruit MQTT you will see the install  button just click install and install   all the required code that is needed  then we need to install the Ada fruit DHT here it says DHT sensor by  Ada fruit you will see the install   button click and install it so these  are the two library that we needed so this line is for Wi-Fi this is for Wi-Fi  client MQTT main library m m client Library   here you enter the SS ID for your Wi-Fi make  sure to respect the case for example if your   SS ID is book and you type book it will  not work so the upper case and lower case   matters and then enter your password this is  the line where it get connect to the MQTT uh   broker URL this is the main page this is the  port we do not change this then uh in here you   will enter your username for Ada fruit uh  key username and then this is the key this   portion is initializing the uh creating an object  called client for Wi-Fi client secure client and   then MQTT client uh we are creating from this  MQTT uh Library an object called mQTT and we   pass all these information that we have username  password server and all of this to get connected   with the key this is a root certificate for  certificate Authority the ca to create a secure connection um because we are using three elements  we are using a subscriber and public so we are   subscribing to a topic of and so we are creating  an object LED as a subscriber another object   called humidity for the publisher and another  object called temperature but these objects have   the following parameters here we go to MQTT and we  pass through this uh username feat and feed name   so this will be Robo Jax which is at the top and  then forward SL feeds forward slash LED so this   was we are uh subscribing to this LED and we are  publishing to Robo Jack feeds humidity Robo Jax   feeds and temperature this could be anything  if you have multiple temperature or multiple   Ed just put some kind of number LED 1 LED 2 or  LED for power supply LED for something make it   unique in your own account so we are subscribing  and then so these three line are very important   and humidity temperature and led this is the LED  pin that is connected to the LED on the board and   this is the DHT Library we are using pin 13  for DHT here DHT 11 is used if you're using   dht22 just change this to 22 it will work just  uh fine 22 and then from this Library we are   creating an object an instance of the library  called DHD and we pass the pin and the type so   this is initializing it inside the setup we are  initializing the serial monitor with 115,200 and   10 millisecond delay this is just printing a text  these are all printing text as I've explained this   one multiple time before this is initializing  the Wi-Fi connection with SS ID and password   with two minutes 2 second delay and uh this is  is running if the connection is not equal true   continuously every half a second it will print  a DOT so this will print a DOT and stays until   the connection is true and it will pass from this  and this will print a new line and it will print   this text that we have been connected to which IP  address this is initializing the uh certificate   the root certificate uh for the connection this  is register the call back call back for the feed   uh this set call back it goes to this function  called LED callback which I'm going to explain   it this is H setting up the subscription for the  LED and then initializing LED pin and DHT this   is initializing that DHT sensor inside the loop  we run in ptd Connect so this will ensure the   connection this indicator for the publish you will  need them and every 10 second we are reading the   subscription message and led call back which was  at the top the job of this was to go and read the   sub subscription message and then the subscription  message here is checking so this line is turning   the LED on and this is turning it off you can  see this is the reverse of what you think high   and low but it it depends on the on how you do  the wiring for the LED and this an indicator uh   for the publish we are reading we are reading the  humidity and storing it in a variable called hum   value and temperature is stored in a temp value  and then this line is checking if there is any   error I've already explained this in a previous  lesson so this will stay there temperature this   line says if the temperature publish is not equal  true this means not then it will print this failed   else it will print the temperature on the serial  monitor the same way for humidity if the humidity   has issue it will print failed else it will just  print it on the serial Monitor and then mq connect   is a function that is called and this will try the  connection uh so this connection is established   every 5 seconds and it retri and it's when it's  connected it will print this on the screen showing   that it has been connected if you use this code  that as is this uh you will get error and the   reason for the error is this expired certificate  let's get the latest one because we just install   the library go to file examples and scroll down  until you see Ada fruit series and the Adafruit   MQTT library we can get it from almost all of  this but let's get it from Ada fruit secure esp32 and here scroll down on line 50  you will see the certificate select it   right click copy go to our code and  then select it right click and paste   so now we are ready to use this and the  code will not the connection will not get error now let's see how we can select the esp32  board we can click here there the select board and   type here esp32 de as soon as you type Dev you  will see Dev board you can select it and click   okay so the board have been selected now we have  to select the port the other way to select the   board is Click On Tools board esp32 and select  the esp32 dev module now we have to select the   port if I click here it shows two port and I don't  know which one belongs to my device sometimes you   will see you will not see the number properly  so the best way to be sure the right click on   the start menu go to the device manager and you  will see here the ports if I click on this Arrow   it will will show me the ports one is USB serial  ch340 one the other is USB serial device and here   now it's connected if I disconnect this one of  them disappear the one that disappeared is my board so six stays and it disappeared if I connect it so it is my comport ch340 now it is my  comp port and I can select it or I can click   on tool port and here you will see it you can  select whichever you want ours is come it now   we have successfully selected the board and the  port and this is very important it must be done first click to upload it now it says not authorized and then MQTT is  also connected and the temperature humidity   everything works fine and if you don't see the  text if you see something like this make sure   that the this value is 115,200 and you will  be able to see so this portion is completed   MQTT is connected this is very important  you must see that now let's go to this dashboard and we should be able to  see the value here it is connected the temperature so now everything is  connected let's click and as you   can see the LED was turned off oh the text is incorrect on off so if you if you see do you see the values  are reverse click here and then edit layout after   that click on this icon and edit block LED is  selected click next step and let's change it to On and Here off update now the value if I click save the  value is reflecting now if I click on it's on   and then off so it depends how you want to see  it let's turn the light off so you can see it better now let me apply some heat with heat gun so more [Applause] three every 5 Seconds this will read and the value will be reflecting soon you see   33° and it's going up every 5 seconds  until it reaches to the steady state value now let's change the value so we can see  the temperature uh symbol and humidity symbol here   make it a little fancy click on edit layout and  then let's say for temperature click here and then   edit block temperature next step on this screen  we can select icon and here type w column and you   will see degree Celsius I wish it was on the other  side oh this is done it shows click on humidity   edit block humidity is selected click next and  then here shows select show icon select here type w and type here H humidity and you see W humidity   like that update block and save layout  now it shows uh better values for the   LED also you could do some icon uh I'm  leaving it for you so you can play with this
Info
Channel: Robojax
Views: 1,038
Rating: undefined out of 5
Keywords: robojax, sun, founder, sunfounder, course, tutorial, kit, learn, learning, step by step, esp32, arduino, iot, wifi, mqtt, client, subscriber, publisher, queue, ON, OFF, LED, adafruit, DHT, DHT11, DHT22, Humidity, temperature, control, remote
Id: M9BQweAsHJM
Channel Id: undefined
Length: 29min 0sec (1740 seconds)
Published: Sat Feb 17 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.