ESP32 Tutorial 46 - Remote Temperature Monitoring using HiveMQ MQTT | SunFounder's ESP32 kit

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to Internet of Things tutorial from  Robojax. in this video we are going to use MQTT   And subscribe and uh record the information or  the temperature online for example if I press   a button the temperature have been recorded if I  press it again another recording and also I can   send the message to turn on this led from the web  goes to the MQTT broker this is just one way and   then this temperature is the temperature is listed  or cute on the cloud here let's get started with   this 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   Arduino Uno can do or many other or Windows 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 in this lesson we are going to use the  NTC or uh the temperature sensor with an LED   and with a push button with uh esp32 right now  it's connected to the battery so every time we   want to publish the temperature on the on the  MQTT server so we just press this button this   has been programmed like this the temperature from  this will be published to the internet and that's   it so anybody else who needs it they can pull it  up then we are going to use this as a subscriber   we will subscribe and pull the information and  on the screen we can say for the LED to be on   or off esp32 will pull down the information from  subscriber from specific topic and if the message   says off it will turn it off if the message says  on it will turn it on let's have a look briefly at   MQTT so MQTT stands for message queuing Telemetry  transport and why it's important because it's   lightweight and very efficient it's bidirectional  which means you can send and receive it can scale   to millions of things it's reliable messaging  delivery it is secure and it's persistent over   unreliable Network so we have a mobile device  or or a computer and then there is a temperature   sensor that is used with ESV 32 for example to  measure the temperature and we want to see the   temperature on this screen for that we need  to have a mqtt broker this is a cloud service   that is ready to log or receive information so  this device is now sending or publishing this   information to the cloud and then this device is  a subscriber it subscribes to certain topic and   it will receive the information this device if  this device is off the information will go and   stay there until you can pull it up pull it down  or when they so so this publisher does not care   about the person who is receiving it and here  in of thing for example there is a connection   and we have a temperature sensor data then it  goes to the MQTT broker this is just one way   and then this temperature is the temperature is  listed or cued on the cloud here and the client   is a subscriber they subscribe to certain  uh data list or topic and They will receive information 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 then scroll to   8.4 IoT communication with MQTT this is the  documentation page where we will learn how   to utilize MQTT protocol and communicate and  send and receive information using our esp32 board we're going to use our esp32 board the  camera extension board Bread Board jumper wire   resistors LED push button switch on our ther here  is the wiring we already used all of this we had   we had separate project for push button please  make sure to watch that video so you understand   it I'm not going to explain it we we had a  an LED fade or LED turn on uh video please   watch that I've explained how LED works and  how to um connect and uh fade it or turn it   on and off and we had another video for the  thermistor or NTC and please watch that we   are combining them all together here and we  will be using PB sub client Library which we   used in a previous lessons this is the code  which I'm going to explain it here is some   explanation for the code AS how it works and  how to modify the code for our purpose which   is a we need to use HiveMQ web client which I'm  going to show you step by step let's click on this from this page at this point just  click it will open this screen for you our code is ready so we just click here to  connect now this is connected and ready for us   here we create a new topic let's name  it s f Sun founder for example LED and   create subscribe one is created and now click  another one and let's create Sounder temp or temperature and subscribe this is now ready when you use esp32 with Sounders 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 now let me show you the wiring first  we will start with a thermostat the ther has   been inserted the right pin is connected to the  blue line here I have set this blue from bottom   all the way this is uh ground and this side the  red side is 3.3 volt where I connected from the   ground this black wire to the ground the second  pin is ground also the second pin on this side is   ground and then 3.3 volt is connected to the  first pin on the left side where the USB is   facing this way this pin is connected the right  pin is connected to the ground the right pin is   connected to the ground and then the left pin is  connected to a 10 K resistor where it is connected   to a point the 10 km resistor is connected from  the left pin to some point here and from here I   I've connected a red wire to 3.3 volts and a  red wire is connected to 3.3 volts the line   where the left pin of thermistor and resist are  meat at this point it has been connected to pin   36 using orange wire and I've connected orange  wire to pin 36 here the USB is this way this is   36 you can connect it also in here now let's go  to the Led has a longer pin this is positive is   facing this way I've inserted such that from the  left pin a yellow wire is connected to the ground   here from the left pin black wire is connected  to to the ground from the positive pin or from   anode it is connected using 220 Ohm resistor  to a point and 220 Ohm resistor is connected   to this point and from that point a yellow wire  is connected to pin 4 and from this point yellow   wire is connected to pin 4 in here let's go to  the push button push button is inserted on both   side of this line and the right pin is connected  using if the push button doesn't fit just rotate   it it will fit the right pin is connected to 3.3  volts the right pin is connected to 3.3 volts   here and the left pin is connected using 1 Kil  or 10 km to a point this resistor is connected   from this side to a point in here and it is  connected to the ground and from here it's   connected to the ground from the pin the left  pin of push button and the resistor we have a   blue wire connected to pin 14 from this point  I've connected this blue wire to pin 14 and here I'm opening Arduino IDE let's open our project by clicking  on file open on the left side click on   downloads then esp32 stter kit main  on the right side double click to   open it double click on C double  click on codes and scroll down to IoT IoT 4 MQTT double click and  select the fire the file click open we have installed this in a previous  lesson this Pub sub if I select and   right click copy if you have not installed  it click on this icon right click here and   paste it and you will see Pop sub  by Nick ol and you will see install   button if you have not installed it  I've already inst Sol it so this is done here we will enter our SSID for  our Wi-Fi mine is Dar and respect   the D if it's lower case or upper case  that capital D will not work in my uh Network this portion is already ready this is the  MQTT server this is unique identifier and and then   from poab we create an object called client and  pass the ESP client to it and these are variable   that are holding the massage length so LED our  LED is connected to pin 4 and push button is   connected to pin 14 the thermistor pin is 36 and  then the reference voltage this is all for the   therer 3.3 volt is our volt voltage the the NTC  value that we are using is 10,000 and the maximum   value the beta for a calculation is which I've  explained it and this is the nominal temperature   that we use for our calculation and this is the  nominal resistance this was a resistor that we   put and this is the resistor for our NTC which  I've already explained it we are initializing   the serial monitor setting up the Wi-Fi this is a  function this function is in here 10 millisecond   delay it prints this text including the SSID and  this is connecting with SSID and password this   function is checking if the status of the Wi-Fi  is not connected this is a constant it Compares   it if it's not then print the dot every uh half a  second and this is just printing a text and what   we'll print our IP address now let's go this  uses a client test server this will connect to   this port and then we have a client set call  back the call back is a function within our   program which I'm going to show you here is a  call back and when we call this function uh uh   to pass it to this function what it does is it  passes it gets a topic and message and L and   it comes here it will print the topic the print  topic and this text and then we are getting the   message I've explained it before one by one and  we're printing it as a character because it will   print each character one by one and then we will  attach each character to this message uh variable   and here we check it if the topic is equal SF LED  then we print this on the screen and then we SF   and founder for example LED and create subscribe  what we check if the uh so first we check the M   the topic make sure we are in the same topic of  subscription and then we check if the message is   equal on then we print this on the screen and  then using digital right we turn the Ed pin to   high else we turn it off I can send a message to  turn turn on this led from the web and turn it off so this was a call back and this is turning  the push button as a setting the push button as   an input and then LED pin as an output here  let's have a look at the Loop inside the loop   this function checks if that if it's not connected  then we use the function reconnect and reconnect   comes here it checks if the client is not  connected it tries again in MQTT it prints   this text and it checks this uh text and this  function and if the result is equal true it   will print connected and then it goes to client  and sub subscription of this uh path for us else   then it will print this on the screen and it  will try again if in 5 seconds 5 seconds is   this weight time and it does all this all over  again so and if the connection is okay then we   come to the client Loop it goes to the client Loop  which is part of the library it will run all the   tasks and here we we use using digital read we  read the push button if push button is pressed   we remember the current time using millis()  whihc is a function that runs from the time   that you turn on your esp32 for Arduino and in 1  second this value will be 1,000 in 10 second it   will be 10,000 because it's millisecond and we  hold it here in a variable of long and then we   check if now is now because this value is always  greater than any other previous value we check   it if this if we subtract the value from the last  message if the difference is greater than 5 second   then we remember the now which is this value  we store it in a variable called last message   which is defined at the top of the code and then  we Define an array of character of length 8 and   then from this uh temperature stct the we get  the value of string uh temperature extract it   from this th which is at the top which is at the  top of the code and we store it in this variable   and then we P publish it using client. publish to  this feed so this value is published to this feed   now let's see the ther ther is a function  that reads the temperature I've explained   it in previous uh lesson please watch that so  this is simply returning the temperature and fil now let's see how we can select the esp32  board we can click here there the select board   and type here esp32 D 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 show  me the ports and is USB serial ch340 one the   other is USB serial device and there 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  comport 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 now our code is ready let's  click to upload it to the board hello okay so as you can see it says connecting   and it's connected now we can click  on this push button to see if it can   send the temperature so when I pressed  it we read the temperature let's go to this and here it it's it shows that  temperature already have arrived   because we have published it here and  I'm going to press it one more time and   another temperature came so I can press  it again and do that so this is for the   message now led this P turn on the topic  FF LED and here this is off I I wrote it incorrect so now it's ready if I click on this  the LED is is on and if you want to turn it   off just say off that's a message and it's off  and at the bottom here it shows what we did so on and it will work and the temperature is also shown and if I press the button you will see that  the temperature is red the last uh record is in here and the is on let let's turn it off and  click publish pay attention and we can do that   now this way we have subscribed and we are sending  temperature publishing it on this portal on high   and the people who have subscribed will pull  it up for example for the LED also we pulled   it which says SF and the value was off when we  pulled it this was turned off and the same way   if somebody writes their on and when we pull  the information you can see that this is on now
Info
Channel: Robojax
Views: 1,143
Rating: undefined out of 5
Keywords: robojax, sun, founder, sunfounder, course, tutorial, kit, learn, learning, step by step, esp32, arduino, iot, wifi, mqtt, subscribe, brokerage, read, LED, temperature, NTC, push button, IoT, wiring, code, PubSubClient
Id: XwTCfnHDASg
Channel Id: undefined
Length: 23min 51sec (1431 seconds)
Published: Wed Jan 03 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.