Introduction: Wireless Temperature and Humidity Monitor With ESP8266
The goal of this project is to make a temperature/humidity monitor that wirelessly logs the temperature and humidity to a remote server. We will use an Arduino Uno (or clone), a DHT11, an ESP8266, and ThingSpeak.com for this project. This is a great project to start using the ESP8266 for some basic data logging so let's get started!
Step 1: Gather Your Supplies
For this project I used an Arduino Uno clone but any Arduino board or clone will work.
- Arduino Uno
- USB A to B cable and 5V USB power supply
- ESP8266 wireless transceiver. I used the Esp-01 but any version should work, just make sure you know the pins.
- DHT11 temperature/humidity sensor, I used a DHT11 but a DHT22 would also work
- Jumper wires
Step 2: Create a ThingSpeak Channel
In order to log your temperature and humidity measurements online you need to have a website or webservice that can accept GET http commands. In this Instructable I am using ThingSpeak but there are other, similar services out there so feel free to use something else to log your data. To create a ThingSpeak account click here.
If you use ThingSpeak you will need to make a channel with two fields, one for temperature and one for humidity. They have a number of options for displaying data in graphs so I encourage you to explore these options and see what you like best. I settled on two simple line graphs with a 10 point average to smooth out occasional blips in the data. ThingSpeak will give you a Key for your channel that you will need to put into the code so that your channel will accept your data.
Step 3: Wiring
First we should cover some basics of the ESP8266 and the DHT11. Then we will move on to wiring them to the Uno.
The image above shows the front of the ESP8266 with the 8 broken out pins labeled. The ESP8266 runs at 3.3 V so make sure you connect the Vcc pin to the 3.3 V pin on the Uno. This also means that you will need to use a logic level shifter or make a voltage divider for the serial connection from the arduino board to the ESP8266 or you risk ruining it. If you don't want to buy a logic level shifter then you will need to make a voltage divider which you can do using two resistors. Sparkfun has a great tutorial on this so you can see how they build theirs and calculate which value resistors you will need to use. I used a 220 Ohm resistor and a 470 Ohm resistor for mine which got me close enough to 3.3 V. Since the ESP8266 outputs 3.3 V and the Arduino boards can take that voltage there is no need to do anything to the serial line from the ESP8266 to the Arduino.
The DHT11 spec is only +/- 5% humidity and 2 degrees Celsius so it is not a super accurate sensor. It's also only good for temperatures above freezing. The DHT22 is a bit more accurate so if that's important to you opt for it instead. Both sensors work with voltages between 3.3 - 5 V. The signal pin will need to be connected to a pull-up resistor, which I used a 4.7 kohm resistor for.
The Fritzing diagram above shows the wiring. For ease of use all connections to 5 V or 3.3 V are red and all connections to ground are blue. The wiring connections are as follows:
- Uno | ESP8266
- RXD | TXD
- TXD | RXD (through voltage divider)
- 3.3 V | Vcc, CH_PD, Reset
- GND | GND
- Uno | DHT11
- 5 V | Vcc
- GND | GND
- D7 | Signal (connect to Vcc via pull-up resistor)
Step 4: Code
Before you can use my code you will need to download and install the DHT library from Adafruit. They have a description of how to install and use their library and a link to their GitHub repository. Their library comes with other features such as a heat index calculator which I don't show here but feel free to log that as well if you're interested in it!
The baud for the ESP8266 is set near the top of the .ino file. The version of the ESP8266 I bought comes in either 9600 baud or 115200 baud. Both of the chips I have ordered are set to 115200 baud but if you're trying to use this code and it doesn't seem to work try changing the baud to 9600 as your first step in debugging.
There are several places in code with values in '<' '>' brackets. Those are places where you will need to paste in your own values to make this work. This includes the name and password for your WiFi network and the Key for your ThingSpeak channel. There is code in place to use a red and green LED for debugging and status monitoring but those are not shown in the wiring diagrams. If you'd like to have a quick way to visibly see if things are running smoothly you can wire up a couple of LEDs.
A final thing that tripped me up for a while is that the serial pins CANNOT both be connected between the ESP8266 and the Uno while uploading the sketch. The sketch will fail to upload if they are connected so you must first disconnect one or both serial pins on either the Uno or the ESP8266 and then upload the sketch.
The code is attached to this step and can also be found on my GitHub page here.
Step 5: Use Your Sensor!
Congratulations, you've created your very own wireless temperature and humidity logger! I usually leave mine in my basement so that I can monitor the temperature, and more importantly, the humidity since it's unfinished and I don't want my things getting moldy from too much humidity. I have also mounted it to a piece of cardboard to make it easy to move around. I hope you found this helpful and had fun making it! In the future I plan to program the ESP8266 directly and bypass the Arduino altogether so look for that to come soon.
Participated in the
First Time Author Contest
6 People Made This Project!
- Jani4321 made it!
- Networking Studio made it!
- theeson made it!
- oum_le_dauphin made it!
See 2 More
2 years ago
can this code work on DHt22?
Reply 1 year ago
Yes it will with slight modifications. You will need to adjust the code to utilize the DHT22. Beyond that the rest will work the same.
Question 1 year ago on Step 1
You said "The sketch will fail to upload if they are connected so you must first disconnect one or both serial pins on either the Uno or the ESP8266 and then upload the sketch"
How does the code find it's way into the ESP-01 if the Tx Rx lines are disconnected during upload?
Answer 1 year ago
Hi Patrick, you're uploading the sketch to the uno, not the Esp unit. The uno runs the code and only utilizes the wifi capabilities of the esp. So during code upload only the uno needs to be connected.
6 years ago
Thanks for this! Well done, but if its only about temperature I would definitely go with DS18B20 sensor because of the power consumption and no measurements delay. The DS18B20 consume less than 1 uA (~700nA) which is almost zero, and also gives the possibility to take advantage from PARASITE MODE and use only 2 wires instead of 3, by powering the sensor from the data line. From my experience DHT11 has a big downside in outdoors, can't resist in high moisture.
DS18B20 1 Wire temperature sensor
DHT22 Temperature and Humidty
ESP8266 Module tutorial
Reply 2 years ago
can this code work on DHt22? or if you have circuit diagram and code for DHt22 then kindly send me thankyou
2 years ago
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
Does GPIO 0 not have to be grounded for uploading sketch???
6 years ago
I first got only AT response. Nothing else. The I put 3 AA rechargeable batteries to feed power. That helped and now the system is working great! I changed the frequency to 600000 ms. Otherwise my phone freezes when trying to load Thingspeak charts and they are too heavy. I plan to get readings around my house to see if there is any risk with moist. Thank you so much!
Reply 5 years ago
hey i only get AT response too. where do u plug the AA battery? and can i use 3.3v power supply?
Reply 3 years ago
Hello! Sorry I had a long pause in making these projects. I connected the AA-batteries to connecting board + and -. But I donät use them any more. Now I use USB 5.0 volts, but the system dies in couple of days. I don't know if it is too high voltage, but it does not work with 3.3 v. With 5.0 volts it works for sometime: 1 day .... 2 months.
3 years ago
My system run well for couple of months, but after that it stops working. I was able to get it working again by changing the ESP8266, but then it failed again after a month. Then once more the same thing. I was not able to get it working with 3.3 V. With USB charger 5 V inout it works. According to specification it should work up to 5 V if I remember correctly. But is the high voltage killing? Are you able to get it working with 3.3 V? Are the chips of bad quality?
3 years ago
I am a beekeeper and think this could be great in our apiary. If I build one, I'll share my results!
3 years ago
Hi! excelent project, i'm trying to read the variable with a PHP file from the ESP with Apache server, how can I read that data, y used Hercules setup to read the data and I could did it, but i dont know how to do it with apache server without saving the data in a Data Base, and i dont know if it's possible. thanks for your help.
Reply 3 years ago
i also need this help
4 years ago
Thanks for the well laid out tutorial. The code seemed to upload without any problem, but I cannot get it to work with a baud rate of 9600 or 115200. The serial monitor simply shows "AT". Any suggestions?
Question 4 years ago on Step 5
i have used my channel id in place of ssid in my code and author number in place of pass and API key of the section(write API key )in place as shown in the picture attached and i used a (1k and 2.2 k resistor )for my voltage divider of TXD,but it doesn't work
what should i do
should i add an ASM1117 for having high current forESP8266
my com3 file is attached also what should i type there?and how ESP8266 is connected to my page in thingspeak.com
Question 4 years ago
1.Done descargar la libreria stdlib ?
5 years ago
How to convert fahrenheit to celsius?
Reply 5 years ago
Remove true from this line of code and you will get celsius.
float f = dht.readTemperature(true);
6 years ago
Can you plz look at this
small help thanks