
in this tutorial we will build Humidity and temperature webserver DHT11 humdity and temperature sensor with esp32.
The DHT11 is a basic, low-cost digital temperature and humidity sensor, remains a popular choice for projects where a simple and affordable temperature and humidity sensor is sufficient for the task at hand.
Component You Need:
Features of DHT11 sensor:
The DHT11 sensor is a basic and affordable digital temperature and humidity sensor. Here are the key features of the DHT11 sensor:
1.Combined Temperature and Humidity Measurement:
- The DHT11 sensor provides both temperature and humidity measurements in a single device.
2.Digital Output:
- The sensor produces a digital output, making it easy to interface with microcontrollers without the need for analog-to-digital conversion.
3.Low Cost:
- One of the main advantages of the DHT11 is its low cost, making it accessible for a wide range of applications, especially in educational and hobbyist projects.
4.Simple Interfacing:
- The sensor requires minimal external components and has a straightforward interface, typically involving just three wires (power, ground, and signal).
5.Wide Operating Voltage Range:
- The DHT11 operates within a wide voltage range, typically from 3.3V to 5V, making it compatible with common microcontroller platforms like Arduino.
6.Moderate Accuracy:
- While not as accurate as some higher-end sensors, the DHT11 provides reasonably accurate temperature and humidity measurements for many applications. The temperature accuracy is within ±2°C, and humidity accuracy is within ±5%.
7.Resistive Humidity Element:
- The DHT11 uses a resistive humidity element to measure humidity levels. This element changes its resistance based on the moisture in the air.
8.Thermistor for Temperature Measurement:
- Temperature measurement in the DHT11 is achieved using a thermistor, which changes its resistance with temperature.
9.Slow Sampling Rate:
- The DHT11 typically has a slow sampling rate, around 1 Hz, meaning it provides a new set of data approximately once every second.
10.Compact Design:
- The sensor is housed in a small, black plastic casing with four pins for easy connection.
11.Limited Communication Protocol:
- The DHT11 uses a simple, proprietary communication protocol to transmit data to microcontrollers.
12.Suitability for Basic Applications:
- The DHT11 is well-suited for basic applications such as home automation, weather monitoring, and educational projects where moderate accuracy is acceptable.
13.Community Support:
- Due to its popularity, the DHT11 is well-supported in the maker community, and libraries are available for various microcontroller platforms.
While the DHT11 may not be suitable for applications requiring high precision, its simplicity and affordability make it a popular choice for introductory projects and applications where basic temperature and humidity measurements are sufficient.
Shematics:

Program:
#include <WiFi.h> // Wifi library
#include <ESPAsyncWebServer.h>// Wifi library
#include <Adafruit_Sensor.h>// Adafruit sensor library (DH11 library)
#include <DHT.h>//DH11 library
const char* ssid = "ssid"; //Put your ssid wifi
const char* password = "password";//put your password wifi
const int dhtPin = 4; // Pin connected to the DHT sensor
// webserver html code
const char* htmlPage = R"(
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DHT11 Sensor Data</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
margin: 0;
padding: 20px;
}
#sensorData {
background-color: #ffffff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.wifi-icon, .temperature-icon, .humidity-icon {
font-size: 40px;
}
.comfort-level {
font-size: 24px;
margin-top: 10px;
}
.comfort-icon {
font-size: 40px;
margin-top: 10px;
}
.comfort-good {
color: green;
}
.comfort-moderate {
color: orange;
}
.comfort-poor {
color: red;
}
</style>
</head>
<body>
<div id="sensorData">
<h1>DHT11 Sensor Data</h1>
<p>Temperature: <span id="temperature">Loading...</span> °C <span class="temperature-icon">????️</span></p>
<p>Humidity: <span id="humidity">Loading...</span> % <span class="humidity-icon">????</span></p>
<div class="comfort-level" id="comfortLevel">Comfort Level: Loading...</div>
<div class="comfort-icon" id="comfortIcon"></div>
</div>
<script>
function updateData(data) {
document.getElementById('temperature').innerText = data.temperature;
document.getElementById('humidity').innerText = data.humidity;
// Update comfort level text
const comfortLevelElement = document.getElementById('comfortLevel');
const comfortIconElement = document.getElementById('comfortIcon');
if (data.temperature >= 20 && data.temperature <= 25 && data.humidity >= 30 && data.humidity <= 60) {
comfortLevelElement.innerText = 'Comfort Level: Good';
comfortLevelElement.className = 'comfort-good';
comfortIconElement.innerText = '????';
} else if (data.temperature >= 18 && data.temperature <= 28 && data.humidity >= 20 && data.humidity <= 70) {
comfortLevelElement.innerText = 'Comfort Level: Moderate';
comfortLevelElement.className = 'comfort-moderate';
comfortIconElement.innerText = '????';
} else {
comfortLevelElement.innerText = 'Comfort Level: Poor';
comfortLevelElement.className = 'comfort-poor';
comfortIconElement.innerText = '????';
}
}
function fetchData() {
fetch('/data')
.then(response => response.json())
.then(data => updateData(data))
.catch(error => console.error('Error fetching data:', error));
}
setInterval(fetchData, 2000);
fetchData(); // Initial fetch when the page loads
</script>
</body>
</html>
)";
const int DHT_TYPE = DHT11;
DHT dht(dhtPin, DHT_TYPE);
AsyncWebServer server(80);//the default port for HTTP communication
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Initialize DHT sensor
dht.begin();
// Serve HTML page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.println("Serving HTML page");
request->send(200, "text/html", htmlPage);
});
// Serve sensor data as JSON
server.on("/data", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.println("Serving sensor data");
String temperature = String(dht.readTemperature());
String humidity = String(dht.readHumidity());
String json = "{\"temperature\":\"" + temperature + "\", \"humidity\":\"" + humidity + "\"}";
request->send(200, "application/json", json);
});
server.begin();
}
void loop() {
// Nothing to do here
}
Result:
After the connexion with the wifi we will get this ip adress

Put it in the URL of webbrowser (googlechrome,mozilafirefox,opera....etc) and you can acess to this page


31 Comment(s)
bonjour\r\n n\'y connaissant en création de pages web j\'ai télécharge le programme qui fonctionne très bien pour l\'affichage des données du capteur dht11 par contre je me retrouve avec cet affichage \r\n c\'est fade par rapport au votre je n\'est pas le graphisme\r\n merci d\'avance pour votre analyse\r\n\r\n Température : 25.80 °C ????️\r\n Humidité : 31.00 % ????\r\n Niveau de confort : Modéré
What a material of un-ambiguity and preserveness of valuable experience on the topic of unpredicted feelings.
Ahaa, its pleasant conversation concerning this paragraph at this place at this webpage, I have read all that, so at this time me also commenting at this place.
Wow, this paragraph is nice, my younger sister is analyzing these kinds of things, so I am going to tell her.
Hello there! Do you know if they make any plugins to protect against hackers? I\'m kinda paranoid about losing everything I\'ve worked hard on. Any suggestions?
I really like it when folks come together and share views. Great website, keep it up!
I rattling glad to find this internet site on bing, just what I was looking for :D besides saved to fav.
I simply couldn\'t depart your website prior to suggesting that I extremely enjoyed the standard info an individual provide to your visitors? Is gonna be back frequently in order to investigate cross-check new posts
Hi! I\'ve been following your blog for a long time now and finally got the bravery to go ahead and give you a shout out from Austin Tx! Just wanted to say keep up the fantastic job!
So if you have the budget, present him a notebook or possibly a plasma Television as his birthday gifts.
Wow, this article is good, my younger sister is analyzing such things, therefore I am going to tell her.
Ahaa, its good dialogue about this article at this place at this blog, I have read all that, so at this time me also commenting here.
I visited various sites however the audio quality for audio songs present at this web page is really wonderful.
Ahaa, its pleasant conversation regarding this piece of writing here at this web site, I have read all that, so now me also commenting at this place.
It\'s very simple to find out any matter on net as compared to books, as I found this piece of writing at this web site.
Awesome! Its actually amazing piece of writing, I have got much clear idea concerning from this piece of writing.
I visited a lot of website but I conceive this one contains something special in it.
Hi, I do believe this is a great blog. I stumbledupon it ;) I am going to come back once again since i have saved as a favorite it. Money and freedom is the best way to change, may you be rich and continue to help others.
It\'s an remarkable piece of writing for all the internet users; they will get advantage from it I am sure.
Way cool! Some very valid points! I appreciate you writing this write-up and the rest of the website is really good.
I am sure this paragraph has touched all the internet users, its really really fastidious article on building up new webpage.
Greetings! Very helpful advice within this post! It is the little changes that make the biggest changes. Thanks a lot for sharing!
I’ll immediately clutch your rss as I can’t in finding your email subscription link or e-newsletter service. Do you have any? Kindly allow me understand so that I may subscribe. Thanks.
Enjoyed looking through this, very good stuff, thanks.
I’ll immediately take hold of your rss feed as I can not find your email subscription link or e-newsletter service. Do you’ve any? Kindly allow me realize so that I could subscribe. Thanks.
Ahaa, its pleasant dialogue about this post at this place at this website, I have read all that, so now me also commenting at this place.
I am sure this piece of writing has touched all the internet people, its really really pleasant post on building up new webpage.
Hi, i feel that i noticed you visited my website thus i came to “return the desire”.I\'m attempting to in finding issues to enhance my web site!I suppose its adequate to use some of your concepts!!
Hi, I do think this is a great web site. I stumbledupon it ;) I may return once again since i have book marked it. Money and freedom is the best way to change, may you be rich and continue to guide others.
Ahaa, its pleasant dialogue on the topic of this article here at this weblog, I have read all that, so at this time me also commenting here.
I am sure this paragraph has touched all the internet users, its really really pleasant paragraph on building up new weblog.
Leave a Comment