Random Nerd Tutorials
Shares

ESP8266 DHT11/DHT22 Temperature and Humidity Web Server with Arduino IDE

Shares

In this project you’ll create a standalone web server with an ESP8266 that displays the temperature and humidity with a DHT11 or DHT22 sensor using the Arduino IDE. The web server you’ll build can be accessed by any device that has a browser on your local network.

Recommended Resources:

If you like the ESP8266 and you want to build more projects, take a look at our course: Home Automation using ESP8266.

First, watch the video demonstration below


Installing Libraries

For this project you need to install the DHT Sensor library and the Adafruit Unified Sensor Driver. Follow the next steps to install the libraries.

Installing the DHT Sensor library

The DHT sensor library provides an easy way of using any DHT sensor to read temperature and humidity with your ESP8266 or Arduino boards.

1) Click here to download the DHT sensor library. You should have a .zip folder in your Downloads

zip

2) Unzip the .zip folder and you should get DHT-sensor-library-master folder

folder

3) Rename your folder from DHT-sensor-library-master to DHT

rename

4) Move the DHT folder to your Arduino IDE installation libraries folder

moving

5) Then re-open your Arduino IDE

Installing the Adafruit Unified Sensor library

You also need to install the Adafruit Unified Sensor Driver Library. Follow the next steps to install the library.

  1. Click here to download the Adafruit Unified Sensor library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get Adafruit_sensor-master folder
  3. Rename your folder from Adafruit_sensor-master to Adafruit_sensor
  4. Move the DHT_sensor folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Uploading your ESP8266 code

Before uploading any code to the ESP8266, you need to have the ESP8266 add-on for the Arduino IDE installed (How to Install the ESP8266 Board in Arduino IDE).

Go to Tools and select “Generic ESP8266 Module”.

Copy the sketch below to your Arduino IDE. Replace the SSID and password with your own credentials.

After modifying my sketch upload it to your ESP8266 (if you can’t upload code to your ESP8266, read this troubleshooting guide).

Note: if you’re using the DHT22 sensor comment the DHT11 line and uncomment the DHT22, as shown below:

// Uncomment one of the lines below for whatever DHT sensor type you're using!
//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321

Schematics

To complete this project you need the following components:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Note: Other DHT sensor types will also work with a small change in the code (as described in the sketch comments).

Here’s the schematic you need to follow to wire the DHT sensor to your ESP8266 board:

ESP8266_schematics_bb

Important: the DHT sensor requires 5V to operate properly, so make sure you use the Vin pin from your ESP8266 that outputs 5V.

ESP8266 IP Address

Open the Arduino IDE serial monitor at a baud rate of 115200. After a few seconds your IP address should appear. In my case it’s 192.168.1.95.

Arduino IDE

Demonstration

For the final demonstration open any browser from a device that is connected to the same router that your ESP is. Then type the IP address and click Enter!

web server

Now you can easily see the current temperature in Celsius and Fahrenheit and the humidity.

Wrapping Up

In this project we’ve shown you how to build a simple web server to display the temperature and humidity. You can easily modify this project to display other sensor readings. If you like the ESP8266 and IoT projects take a look at some of our most popular projects:

Do you have any questions? Leave a comment down below!

Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing our blog and our Facebook Page.

P.S. If you got stuck during this tutorial make sure you read “ESP8266 Troubleshooting Guide


Learn ESP32 with Arduino IDE

This our complete guide to program the ESP32 with Arduino IDE, including projects, tips, and tricks! The registration closes soon, so SIGN UP NOW »

Leave a Comment:

Add Your Reply

⏰ Registrations are open ⏰
Sign up for "Learn ESP32 with Arduino IDE" Course