Random Nerd Tutorials

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


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


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


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


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



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


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:


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

Arduino IDE


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

MicroPython Programming with ESP32/ESP8266 eBook

Learn how to program the ESP boards with MicroPython, including projects, tips, and tricks! The registrations are open for a limited time, so SIGN UP NOW »

Recommended Resources

Home Automation using ESP8266 »
Build IoT projects and home automation gadgets with the ESP8266 Wi-Fi module.

Build a Home Automation System »
Learn how to build a automation system using open-source hardware and software from scratch.

Arduino Step-by-Step Projects »
Build 25 cool Arduino projects with our course even with no prior experience!

Leave a Comment:

Add Your Reply

⏰ Registrations are open for "MicroPython Programming with ESP32 and ESP8266" eBook