How to use DHT11 DHT22 with NodeMCU ESP8266 to read temperature over WiFi - IoT

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi, welcome to an esp8266 tutorial by Robojax. my name is Ahmad Shamshiri presenting this review from Canada. in this tutorial we are going to learn how we can use esp8266 to measure the temperatures using this dht11 or dht22 or DHT21 temperature sensors and access it over the Wi-Fi from the same network of your home either using your mobile phone or your computer. Here are the topics covered in this tutorial. first we'll have introduction to web server. then we will explain the wiring. after that we will show you how to prepare your arduino ide for esp8266 boards. then i'm going to explain the code. after that i'm going to do the demonstration with dht11 temperature and humidity sensor. then demonstration with dht22 quickly. and then i'm going to demonstrate it on the mobile phone. and will show you how to access and read temperature by typing http://robojaxDHT you can get the code for this by clicking on the link below the video in the description which will take you to www.robojax.com/learn/arduino let's get started with this with variability of internet of thing IoT or esp8266 Wi-Fi microcontroller module you can measure temperature over the Wi-Fi and we are using in this tutorial dht11 and you can use dht22 as well either as a PCB module or without a PCB a barre module which i am going to show you how to wire this with another simple resistor. This microcontroller will read the temperature from the sensor and make it available over the Wi-Fi so you can use either your browser or your mobile phone to monitor or see the temperatures wherever you put this microcontroller. Web server (sorry for misspelled title) explained I want to see how we can use esp8266 Wi-Fi microcontroller module with dht11 humidity and temperature sensor for dht22 humidity and temperature sensor. I have separate video explaining dht22 and dht11 for both of these we have separate video the links will be below the video in the description. Now let's to see how we can use esp8266 Wi-Fi microcontroller module with dht11 humidity and temperature sensor for dht22 humidity and temperature sensor I have separate video explaining dht22 and dht11 for both of these we have separate video the links will be below the video in the description in order to use the DHT 11 or 22 with esp8266 NodeMCU or D1 Miniover the Wi-Fi we will configure esp8266 as a webserver now let's see how it works for example you have your mobile device laptop which is connected by a Wi-Fi to a web server so let's say when you type an IP address 192.168.1.1 server will respond and this communication can be where your internet service provider ISP for example when you type robojax.com the information goes to the your entire self provider to the internet and from there the server will respond back and you get the information based on the configuration of the web server. and your communication could be within your network over the Wi-Fi and in this case you can type an IP address of your local IP address it goes to the Wi-Fi router and from Wi-Fi router well it is connected to the web server it will respond. now we can replace that web server which is a computer whether esp8266 as a tiny web server. so this can be configured at a webserver and will respond to our requests. so esp8266 will communicate with a Wi-Fi router and from Wi-Fi router the communication has established with your laptop and this way you can communicate with the esp8266 and the information will be displayed on your computer screen or your mobile device. now we can connect dht11 to esp8266 so this can read the temperature or humidity values and then it will communicate to the router and from router the information comes on your computer screen we can just replace that dht11 with dht22 it will act exactly the same way. or maybe you can replace your laptop with your mobile device because it has a web browser in both cases we are using web browser and you will see the response of esp8266 on your mobile device screen as well. your esp8266 can read and pressure from dht11 or dht22 then make sure to use it with the serial monitor do not try it over the Wi-Fi make sure that you're able to read and if you have problem with that then go back to arduino uno or arduino compatible boards to make sure that you can read the value on the serial monitor. i will provide you the link that shows that dht11 or dht22 is used with arduino uno or arduino compatible boards. Wiring Explained Let me explain the wiring. I'm gonna explain first with the PCB then I'm going to this we have three wires it has been labeled as here as a + in this side - and the middle is out which will give you the signal this will be connected to five volts the - will be connected to the ground and this is a signal out which will be connected to GPIO2 which is labeled as 4 on the module that has CP 2102 USB chip it is labeled at 3.3 volts where you will connect it and on the module with a CH 3 4 0 USB chip it is labeled as 3v so that is also 3.3 volts and ground but you can connect it to any other 3V around in this module. and here is how you connect the wires so on the positive he will connected to 3.3 volts at that pen which is labeled as 3 and if you have and if you have this module it will be called 3.3 volts then the last one is negative has been connected to G here and if you have this type of module it will be GND or ground and then the middle pin is the output the output is connected to D4 here or D4 in here. okay the first thing that we do is when I insert this here for this we need an extra resistor. on this module they already have the resistor here so they make it easy but here doesn't matter you for this module we we have four pins I've insulted here a 5kΩ to 10kΩ resistor between pin 1 and pin 2 and then here the other side is exactly as the other module except here 1 2 and then the third one is empty and the white is at the 4th pin you always leave the third pn unused. Preparing Arduino IDE for ESP8166 first thing that you need to prepare is your Arduino IDE for esp8266. I've separate video explaining that but here I can tell you quickly click on File-> Preferences and then ->Additional board managers URL click here and then paste the link that I provided I will provide you this one so you copy and paste it here. once you paste it you click OK and then click OK. this link will make Arduino to know where to look for the board. click on Tools->Board manager and then board manager wait for this to complete and then you will be able to type. once you are able to type type esp8266 then you will see this is ESP8266 by esp8266 community in this case it is installed and I have a remove button for in your case if you have not installed it you will see here install button click on it and wait and tell the bar graph finishes and then close. then we need to select the board by clicking Tools->Boards and then scroll down until you see esp8266 Generic esp8266 modules select that. then we have to tell the port that the device is connected to. now I'm connecting my board if you don't know which port it is right click on Start menu go to device manager and then under the port collapse this arrow and this is a port USB serial CH 3 4 0 or maybe you see another one but this is a comp or 11 then we have to go in arduino click on Tools and come to the port and then make sure to select the port that is connected to your USB in my case it will be 11. then your arduino IDE is ready to accept the code for upload it to ESP8266. Installing DHT libray you also need to download the DHT driver and make sure it works I will provide you the zip once you download the zip file click on Sketch and then ->include library-> add dot zip library and then pointed to where you've saved it. browse in my case let's say this was the .zip library for DHT and click open once you click open it is installed. Code explained and here this line it says constant integer refresh this is the refresh rate of how often that temperature is read or displayed on the screen the three minutes three second if you want to speed it up make it one second every one second or make it ten second so this will read it and that's line it says start of dht11 and that is the end this include the HT dot H is a header file which is part of the library defined the DHTPIN I'm using GP I open - which is labeled as d4 you can change it accordingly if you wish and then this library can work with dht22 21 dec 22 and dht11 whichever you're using make sure to uncomment it. in this case DHT11 is uncommented and if I was using DHT22 would uncomment this and then comment the above. so you can select the one that you want to use. and then we are creating an instance of this DHT and we call it again DHT with lowercase and we pass this DHT pin and we pass the type, two values 2 parameter is passed to this class this constructor will initialize it and then float T value or temperature value is the value that we send over the web which will be used later keep that in mind. these four lines esp8266 Wi-Fi Wi-Fi client web server and thus multicast dns these are all part of preparation of Arduino for esp8266 this you don't have to worry about it. here enter your SSID and this is your password for your Wi-Fi I have separate router for this purpose so I'm not worried if you know this password it's not a security risk I will turn it off when the recording is finished. and then we pass those values here and now they are assigned to a lower cases ID and password and then we initializing the server we initialize a server with a port 80 HTTP the different ports so for HTTP we use port 80. and this is a function that I have written sendTemp the job of this one is to send the temperatures and printed like a page this is a code for a page you see all this text these are the HTML for that which means behind-the-scene is these text so we have document type or dactype at the top and then we have HTML and HTML close you see the slash / at the beginning that says opening tag that's a closing body and then body and then we have some text this is paragraph Robojax Test and then the read I'll provide you the link if you want to play with it so I'm printing those values but because I'm assigning the page as string, I have to put quotation at the beginning and end this backslash \n backslash n is a to new line and then page we are concatenated this meta tag HTTP equivalent refresh and then content is the time so here we put three or four or five which is here the refresh is coming from the top which we define and we convert it to a string and this will be concatenated using this plus two page the page that we define above slowly we are stitching this text to it and then the number after that disclosing the HTML body and then this text temperature and then the style for read for temperature and then actually T value this is a temperature value with two decimal place we are converting get to string and at stitching it to page string and then the P close the paragraph the body is close HTML s close and then this page is holding all the elements of a page that is displayed on browser. and then using server.send we are we are we have three parameters this 200 means the pages okay or responded okay and then here this called a Miami or this is a type that is telling to the browser if this content is image, text audio video or type of content so it says this is text HTML so the browser knows how to treat it. and the page is also passed so this is the content this is a type. this handle not found do not worry about it this is needed in case if there is something goes wrong this will be printed. and then inside the setup we initialize a DHT.began this is for serial monitor with it 115200 baud per second and their Wi-Fi station is defined the mood and then we pass a society and password so other Wi-Fi begins and then this just runs a new line. and then waiting for correction so while Wi-Fi status is not equal to Wi-Fi connected then if it is not connected every 500 milliseconds it will print a dot which I was I'm going to show you next so because this this is print not parent Ln you see dot and then another dot another that will be printed every 500 millisecond and tell the connection happens when the connection happened this will be automatically false and while will exit. and then this prints again new line println and transverse text prints the SSID name and IP address this is a text and this will print IP address. and here multicast DNS begins here you can type whatever value you want us I put the robojaxDHT and F death returns true or initialized we print this text on the screen and then this is where it goes to the root of the server and runs this content which is a page. and this is also needed in case if you have different arguments or parameters this will also work. and if server not found this is a handling internally from the function . it will that server not found server. begin we'll begin the server and then it will start this text for us. inside the loop solver handle client will start the server and this will start the multicast DNS updated. this dht.readTemperature will read the temperature in Celsius and store it in C of type float this C will be our temperature. if you want humidity, humidity will be an H and then these two are the same except inside we have true if this is true the value will be Fahrenheit and it will be stored in F. and then in a serial monitors we adjust Plan C and then we get the t-value temperature and we assigned the latest value to the T value which will be printed on the page and we have a 300 millisecond the loop will continuously take care of these tasks. Demonstration I'm uploading the code now so just have done. let's open the serial monitor you see the dot that I mentioned now it is reading and let me stop this so I can explain it it shows the IP address 192.168.1.100 and multicast DNS also started so we can access our temperature sensor reading on this IP address or we can type our own value here robojaxDHT you don't worry about the IP and anyways. today is 21st birthday of Google. now let's type the IP here 192.168.1.100 now pay attention this blue refresh shows that time that we set. and you see the none that you saw here that was due to the time that when the temperature is not responding the page refreshed and it did not get the value because 300 millisecond delay now let me apply some heat here and as you can see the temperature increased and start increasing at 40 now it will slowly cool down. still 41 it got 40 now if I disconnect it pay attention you see that non and if I connect it again it will go back on line 39. now I have this one set up exactly the same I'm going to connect the wires to this so are you I'm using the same color so no worries. now as you can see that temperature has decreased to 25 degrees Demonstration No. 2 Here if you connect DHT22 it'll also work fine except you have to uncomment this portion for DHT22 and comment this dht11. Demonstration of MDNS and let's now access it by typing http://robojaxDHT DHT and here you can see it that it shows with regard VHT as well Measuring temperature and displaying it on browser on mobile phone now let's access it via your mobile phone first you have to be connected to your Wi-Fi I'm connected now to Robjax Network. let's open the browser and then type 192.168.1.100. I have already typed it and here it shows the temperature and let me heat up so this will refresh and 3 seconds and you see that it is already 36 and maybe will increase to 40 or something apply some heat the value will change I would like to thank my Patrons from Patreon.com Subtitle typed and corrected by Ahmad Shamshiri in Canada on July 30, 2020 I did my part to help you learn. Now it's your turn. You can subscribe to my channel, thumb up 👍 the video and say "thank you" in the comment section. The link to my PayPal account is below this video if you want to send me chase donation. Thank you. thank you for watching therefore how to use the esp8266 with dht11 or dht22 to read a temperature over your Wi-Fi within the same network if you learned something and found this useful please thumb up 👍 as this will help my video in the search algorithm of YouTube. if you have comment our question posted at the comment section below I try to answer and reply. if you want to get updates of my upcoming videos you may subscribe now. you
Info
Channel: Robojax
Views: 45,692
Rating: undefined out of 5
Keywords: electronic, tutorial, training, video, robojax, arduino, code, download, dht, dht11, dht22, dht21, temperature, sensor, am2302, esp8266, nodemcu, wifi, wireless, control, display, measure, humidity, canadian, canada, ahmad, shamshiri, browser, driver, library, web, server, show, view, mobile, phone, tablet, mdns, example, project
Id: 1A4-6hDARQc
Channel Id: undefined
Length: 23min 55sec (1435 seconds)
Published: Wed Oct 02 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.