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 with any device that has a browser on your local network.

ESP8266 DHT11 DHT22 Temperature and Humidity Web Server with Arduino IDE

Throughout this tutorial we’ll show how to build two different web servers:

  • Web Server #1: Asynchronous web server that updates the temperature and humidity automatically without the need to refresh the web page and with custom CSS to style the web page.
  • Web Server #2: Simple HTTP web server that displays the latest sensor readings when the page is updated in a raw HTML page.

Recommended resources:

Learn more about the ESP8266 with our course: Home Automation using ESP8266.

Parts Required

To build this project, you need the following parts:

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!

ESP8266 and DHT11/DHT22 Schematic Diagram

Before proceeding with the tutorial, wire the DHT11 or DHT22 temperature and humidity sensor to the ESP8266 as shown in the following schematic diagram.

ESP8266 DHT11 DHT22 Schematic Diagram Circuit

In this example, we’re wiring the DHT data pin to GPIO5 (D1), but you can use any other suitable GPIO. Read our ESP8266 GPIO Reference Guide to learn more about the ESP8266 GPIOs.

If you’re using an ESP-01, GPIO 2 is the most suitable pin to connect to the DHT data pin, as shown in the next diagram.

ESP-01 DHT11 DHT22 Schematic Diagram Circuit

Installing the DHT Library for ESP8266

To read from the DHT sensor, we’ll use the DHT library from Adafruit. To use this library you also need to install the Adafruit Unified Sensor library. Follow the next steps to install those libraries.

1. Open your Arduino IDE and go to Sketch Include Library > Manage Libraries. The Library Manager should open.

2. Search for “DHT” on the Search box and install the DHT library from Adafruit.

Installing Adafruit DHT library

3. After installing the DHT library from Adafruit, type “Adafruit Unified Sensor” in the search box. Scroll all the way down to find the library and install it.

Installing Adafruit Unified Sensor driver library

After installing the libraries, restart your Arduino IDE.


1. ESP8266 Asynchronous Web Server

To build the web server we’ll use the ESPAsyncWebServer library that provides an easy way to build an asynchronous web server. Building an asynchronous web server has several advantages. We recommend taking a quick look at the library documentation on its GitHub page.

Installing the ESPAsyncWebServer library

The ESPAsyncWebServer library is not available to install in the Arduino IDE Library Manager. So, you need to install it manually.

Follow the next steps to install the ESPAsyncWebServer library:

  1. Click here to download the ESPAsyncWebServer library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get ESPAsyncWebServer-master folder
  3. Rename your folder from ESPAsyncWebServer-master to ESPAsyncWebServer
  4. Move the ESPAsyncWebServer folder to your Arduino IDE installation libraries folder

Installing the ESPAsync TCP Library

The ESPAsyncWebServer library requires the ESPAsyncTCP library to work. Follow the next steps to install that library:

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

Code

We’ll program the ESP8266 using Arduino IDE, so you must have the ESP8266 add-on installed in your Arduino IDE. If you haven’t, follow the next tutorial first:

Open your Arduino IDE and copy the following code.

/*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com/esp8266-dht11dht22-temperature-and-humidity-web-server-with-arduino-ide/
*********/

// Import required libraries
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Hash.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>

// Replace with your network credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

#define DHTPIN 5     // Digital pin connected to the DHT sensor

// Uncomment the type of sensor in use:
//#define DHTTYPE    DHT11     // DHT 11
#define DHTTYPE    DHT22     // DHT 22 (AM2302)
//#define DHTTYPE    DHT21     // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

// current temperature & humidity, updated in loop()
float t = 0.0;
float h = 0.0;

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;    // will store last time DHT was updated

// Updates DHT readings every 10 seconds
const long interval = 10000;  

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
  <style>
    html {
     font-family: Arial;
     display: inline-block;
     margin: 0px auto;
     text-align: center;
    }
    h2 { font-size: 3.0rem; }
    p { font-size: 3.0rem; }
    .units { font-size: 1.2rem; }
    .dht-labels{
      font-size: 1.5rem;
      vertical-align:middle;
      padding-bottom: 15px;
    }
  </style>
</head>
<body>
  <h2>ESP8266 DHT Server</h2>
  <p>
    <i class="fas fa-thermometer-half" style="color:#059e8a;"></i> 
    <span class="dht-labels">Temperature</span> 
    <span id="temperature">%TEMPERATURE%</span>
    <sup class="units">&deg;C</sup>
  </p>
  <p>
    <i class="fas fa-tint" style="color:#00add6;"></i> 
    <span class="dht-labels">Humidity</span>
    <span id="humidity">%HUMIDITY%</span>
    <sup class="units">%</sup>
  </p>
</body>
<script>
setInterval(function ( ) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("temperature").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/temperature", true);
  xhttp.send();
}, 10000 ) ;

setInterval(function ( ) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("humidity").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/humidity", true);
  xhttp.send();
}, 10000 ) ;
</script>
</html>)rawliteral";

// Replaces placeholder with DHT values
String processor(const String& var){
  //Serial.println(var);
  if(var == "TEMPERATURE"){
    return String(t);
  }
  else if(var == "HUMIDITY"){
    return String(h);
  }
  return String();
}

void setup(){
  // Serial port for debugging purposes
  Serial.begin(115200);
  dht.begin();
  
  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  Serial.println("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println(".");
  }

  // Print ESP8266 Local IP Address
  Serial.println(WiFi.localIP());

  // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/html", index_html, processor);
  });
  server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", String(t).c_str());
  });
  server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", String(h).c_str());
  });

  // Start server
  server.begin();
}
 
void loop(){  
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // save the last time you updated the DHT values
    previousMillis = currentMillis;
    // Read temperature as Celsius (the default)
    float newT = dht.readTemperature();
    // Read temperature as Fahrenheit (isFahrenheit = true)
    //float newT = dht.readTemperature(true);
    // if temperature read failed, don't change t value
    if (isnan(newT)) {
      Serial.println("Failed to read from DHT sensor!");
    }
    else {
      t = newT;
      Serial.println(t);
    }
    // Read Humidity
    float newH = dht.readHumidity();
    // if humidity read failed, don't change h value 
    if (isnan(newH)) {
      Serial.println("Failed to read from DHT sensor!");
    }
    else {
      h = newH;
      Serial.println(h);
    }
  }
}

View raw code

Insert your network credentials in the following variables and the code will work straight away.

const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

How the Code Works

In the following paragraphs we’ll explain how the code works. Keep reading if you want to learn more or jump to the Demonstration section to see the final result.

Importing libraries

First, import the required libraries.

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Hash.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>

Setting your network credentials

Insert your network credentials in the following variables, so that the ESP8266 can connect to your local network.

const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

Variables definition

Define the GPIO that the DHT data pin is connected to. In this case, it’s connected to GPIO5 (D1).

#define DHTPIN 5     // Digital pin connected to the DHT sensor

Then, select the DHT sensor type you’re using. In our example, we’re using the DHT22. If you’re using another type, you just need to uncomment your sensor and comment all the others.

#define DHTTYPE DHT22   // DHT 22 (AM2302)

Instantiate a DHTobject with the type and pin defined earlier.

DHT dht(DHTPIN, DHTTYPE);

Create an AsyncWebServerobject on port 80.

AsyncWebServer server(80);

Create float variables to hold the current temperature and humidity values. The temperature and humidity are updated in the loop().

float t = 0.0;
float h = 0.0;

Create timer variables needed to update the temperature readings every 10 seconds.

unsigned long previousMillis = 0;    // will store last time DHT was updated

// Updates DHT readings every 10 seconds
const long interval = 10000; 

Building the Web Page

Proceeding to the web server page.

As you can see in the above figure, the web page shows one heading and two paragraphs. There is a paragraph to display the temperature and another to display the humidity. There are also two icons to style the page.

Let’s see how this web page is created.

All the HTML text with styles included is stored in the index_html variable. Now we’ll go through the HTML text and see what each part does.

The following <meta> tag makes your web page responsive in any browser.

<meta name="viewport" content="width=device-width, initial-scale=1">

The <link> tag is needed to load the icons from the fontawesome website.

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">

Styles

Between the <style></style> tags, we add some CSS to style the web page.

<style>
 html {
   font-family: Arial;
   display: inline-block;
   margin: 0px auto;
   text-align: center;
  }
  h2 { font-size: 3.0rem; }
  p { font-size: 3.0rem; }
  .units { font-size: 1.2rem; }
  .dht-labels{
    font-size: 1.5rem;
    vertical-align:middle;
    padding-bottom: 15px;
  }
</style>

Basically, we’re setting the HTML page to display the text with Arial font in block without margin, and aligned at the center.

html {
  font-family: Arial;
  display: inline-block;
  margin: 0px auto;
  text-align: center;
}

We set the font size for the heading (h2), paragraph (p) and the units(.units) of the readings.

h2 { font-size: 3.0rem; }
p { font-size: 3.0rem; }
.units { font-size: 1.2rem; }

The labels for the readings are styled as shown below:

dht-labels{
  font-size: 1.5rem;
  vertical-align:middle;
  padding-bottom: 15px;
}

All of the previous tags should go between the <head> and </head> tags. These tags are used to include content that is not directly visible to the user, like the <meta> , the <link> tags, and the styles.

HTML Body

Inside the <body></body> tags is where we add the web page content.

The <h2></h2> tags add a heading to the web page. In this case, the “ESP8266 DHT server” text, but you can add any other text.

<h2>ESP8266 DHT Server</h2>

Then, there are two paragraphs. One to display the temperature and the other to display the humidity. The paragraphs are delimited by the <p> and </p> tags. The paragraph for the temperature is the following:

<p>
  <i class="fas fa-thermometer-half" style="color:#059e8a;"</i> 
  <span class="dht-labels">Temperature</span> 
  <span id="temperature">%TEMPERATURE%</span>
  <sup class="units">°C</sup>
</p>

And the paragraph for the humidity is on the following snipet:

<p>
  <i class="fas fa-tint" style="color:#00add6;"></i> 
  <span class="dht-labels">Humidity</span>
  <span id="humidity">%HUMIDITY%</span>
  <sup class="units">%</sup>
</p>

The <i> tags display the fontawesome icons.

How to display icons

To chose the icons, go to the Font Awesome Icons website.

Search the icon you’re looking for. For example, “thermometer”.

Click the desired icon. Then, you just need to copy the HTML text provided.

<i class="fas fa-thermometer-half">

To chose the color, you just need to pass the style parameter with the color in hexadecimal, as follows:

<i class="fas fa-tint" style="color:#00add6;"></i> 

Proceeding with the HTML text…

The next line writes the word “Temperature” into the web page.

<span class="dht-labels">Temperature</span>

The TEMPERATURE text between % signs is a placeholder for the temperature value.

<span id="temperature">%TEMPERATURE%</span>

This means that this %TEMPERATURE% text is like a variable that will be replaced by the actual temperature value from the DHT sensor. The placeholders on the HTML text should go between % signs.

Finally, we add the degree symbol.

<sup class="units">°C</sup>

The <sup></sup> tags make the text superscript.

We use the same approach for the humidity paragraph, but it uses a different icon and the %HUMIDITY% placeholder.

<p>
  <i class="fas fa-tint" style="color:#00add6;"></i> 
  <span class="dht-labels">Humidity</span>
  <span id="humidity">%HUMIDITY%</span>
  <sup class="units">%</sup>
</p>

Automatic Updates

Finally, there’s some JavaScript code in our web page that updates the temperature and humidity automatically, every 10 seconds.

Scripts in HTML text should go between the <script></script> tags.

<script>
setInterval(function ( ) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("temperature").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/temperature", true);
  xhttp.send();
}, 10000 ) ;

setInterval(function ( ) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("humidity").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/humidity", true);
  xhttp.send();
}, 10000 ) ;
</script>

To update the temperature on the background, we have a setInterval() function that runs every 10 seconds.

Basically, it makes a request in the /temperature URL to get the latest temperature reading.

  xhttp.open("GET", "/temperature", true);
  xhttp.send();
}, 10000 ) ;

When it receives that value, it updates the HTML element whose id is temperature.

if (this.readyState == 4 && this.status == 200) {
  document.getElementById("temperature").innerHTML = this.responseText;
}

In summary, this previous section is responsible for updating the temperature asynchronously. The same process is repeated for the humidity readings.

Important: since the DHT sensor is quite slow getting the readings, if you plan to have multiple clients connected to an ESP8266 at the same time, we recommend increasing the request interval or remove the automatic updates.

Processor

Now, we need to create the processor() function, that will replace the placeholders in our HTML text with the actual temperature and humidity values.

String processor(const String& var){
  //Serial.println(var);
  if(var == "TEMPERATURE"){
    return String(t);
  }
  else if(var == "HUMIDITY"){
    return String(h);
  }
  return String();
}

When the web page is requested, we check if the HTML has any placeholders. If it finds the %TEMPERATURE% placeholder, we return the temperature that is stored on the t variable.

if(var == "TEMPERATURE"){
  return String(t);
}

If the placeholder is %HUMIDITY%, we return the humidity value.

else if(var == "HUMIDITY"){
  return String(h);
}

setup()

In the setup(), initialize the Serial Monitor for debugging purposes.

Serial.begin(115200);

Initialize the DHT sensor.

dht.begin();

Connect to your local network and print the ESP8266 IP address.

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
  Serial.println("Connecting to WiFi..");
}

Finally, add the next lines of code to handle the web server.

server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  request->send_P(200, "text/html", index_html, processor);
});
server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
  request->send_P(200, "text/plain", String(t).c_str());
});
server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
  request->send_P(200, "text/plain", String(h).c_str());
});

When we make a request on the root URL, we send the HTML text that is stored on the index_html variable. We also need to pass the processorfunction, that will replace all the placeholders with the right values.

server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  request->send_P(200, "text/html", index_html, processor);
});

We need to add two additional handlers to update the temperature and humidity readings. When we receive a request on the /temperature URL, we simply need to send the updated temperature value. It is plain text, and it should be sent as a char, so, we use the c_str() method.

server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
  request->send_P(200, "text/plain", String(t).c_str());
});

The same process is repeated for the humidity.

server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
  request->send_P(200, "text/plain", String(h).c_str());
});

Lastly, we can start the server.

server.begin();

In the loop() is where we get new temperature readings from the sensor every 10 seconds.

Basically, we check if it is time to get new sensor readings:

if (currentMillis - previousMillis >= interval) {

If it is, we store a new temperature reading on the newT variable

float newT = dht.readTemperature();

If the newT variable is a valid temperature readings, we update the t variable.

else {
  t = newT;
  Serial.println(t);
}

The same process is repeated for the humidity.

// Read Humidity
float newH = dht.readHumidity();
// if humidity read failed, don't change h value 
if (isnan(newH)) {
   Serial.println("Failed to read from DHT sensor!");
}
else {
  h = newH;
  Serial.println(h);
}

That’s pretty much how the code works.

Uploading the code

After modifying the sketch with the necessary changes, if needed, upload the code to your ESP8266 (if you can’t upload code to your ESP8266, read this troubleshooting guide).

Make sure you have the right board and COM port select. Go to Tools> Board and select the ESP8266 model you’re using. In our case, we’re using the ESP8266 12-E NodeMCU Kit.

Go to Tools > Port and select the COM port the ESP8266 is connected to.

Press the Arduino IDE upload button.

Note: if you’re using an ESP-01, you need a serial adapter or an FTDI programmer to upload code.

ESP8266 IP Address

After uploading the code, open the Serial Monitor at a baud rate of 115200. Press the ESP8266 reset button. The ESP8266 IP address will be printed in the serial monitor as shown in the following figure.

Demonstration

In your local network, go to a browser and type the ESP8266 IP address. It should display the following web page with the latest sensor readings.

The temperature and humidity readings update automatically every 10 seconds without the need to refresh the web page.


2. ESP8266 Simple HTTP Web Server

In this section, we’ll show you how to build a simple HTTP web server that displays the temperature and humidity in a raw HTML page. This web server sends an HTTP response when your browser makes a request on the ESP8266 IP address.

Video Demonstration

First, you can watch the ESP8266 web server project video demonstration below.

Code

We’ll program the ESP8266 using Arduino IDE, so you must have the ESP8266 add-on installed in your Arduino IDE. If you haven’t follow the next tutorial first.

Open your Arduino IDE and copy the following code.

/*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com  
*********/

// Including the ESP8266 WiFi library
#include <ESP8266WiFi.h>
#include "DHT.h"

// 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

// Replace with your network details
const char* ssid = "YOUR_NETWORK_NAME";
const char* password = "YOUR_NETWORK_PASSWORD";

// Web Server on port 80
WiFiServer server(80);

// DHT Sensor
const int DHTPin = 5;
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);

// Temporary variables
static char celsiusTemp[7];
static char fahrenheitTemp[7];
static char humidityTemp[7];

// only runs once on boot
void setup() {
  // Initializing serial port for debugging purposes
  Serial.begin(115200);
  delay(10);

  dht.begin();
  
  // Connecting to WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  // Starting the web server
  server.begin();
  Serial.println("Web server running. Waiting for the ESP IP...");
  delay(10000);
  
  // Printing the ESP IP address
  Serial.println(WiFi.localIP());
}

// runs over and over again
void loop() {
  // Listenning for new clients
  WiFiClient client = server.available();
  
  if (client) {
    Serial.println("New client");
    // bolean to locate when the http request ends
    boolean blank_line = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        
        if (c == '\n' && blank_line) {
            // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
            float h = dht.readHumidity();
            // Read temperature as Celsius (the default)
            float t = dht.readTemperature();
            // Read temperature as Fahrenheit (isFahrenheit = true)
            float f = dht.readTemperature(true);
            // Check if any reads failed and exit early (to try again).
            if (isnan(h) || isnan(t) || isnan(f)) {
              Serial.println("Failed to read from DHT sensor!");
              strcpy(celsiusTemp,"Failed");
              strcpy(fahrenheitTemp, "Failed");
              strcpy(humidityTemp, "Failed");         
            }
            else{
              // Computes temperature values in Celsius + Fahrenheit and Humidity
              float hic = dht.computeHeatIndex(t, h, false);       
              dtostrf(hic, 6, 2, celsiusTemp);             
              float hif = dht.computeHeatIndex(f, h);
              dtostrf(hif, 6, 2, fahrenheitTemp);         
              dtostrf(h, 6, 2, humidityTemp);
              // You can delete the following Serial.print's, it's just for debugging purposes
              Serial.print("Humidity: ");
              Serial.print(h);
              Serial.print(" %\t Temperature: ");
              Serial.print(t);
              Serial.print(" *C ");
              Serial.print(f);
              Serial.print(" *F\t Heat index: ");
              Serial.print(hic);
              Serial.print(" *C ");
              Serial.print(hif);
              Serial.print(" *F");
              Serial.print("Humidity: ");
              Serial.print(h);
              Serial.print(" %\t Temperature: ");
              Serial.print(t);
              Serial.print(" *C ");
              Serial.print(f);
              Serial.print(" *F\t Heat index: ");
              Serial.print(hic);
              Serial.print(" *C ");
              Serial.print(hif);
              Serial.println(" *F");
            }
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connection: close");
            client.println();
            // your actual web page that displays temperature and humidity
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
            client.println("<head></head><body><h1>ESP8266 - Temperature and Humidity</h1><h3>Temperature in Celsius: ");
            client.println(celsiusTemp);
            client.println("*C</h3><h3>Temperature in Fahrenheit: ");
            client.println(fahrenheitTemp);
            client.println("*F</h3><h3>Humidity: ");
            client.println(humidityTemp);
            client.println("%</h3><h3>");
            client.println("</body></html>");     
            break;
        }
        if (c == '\n') {
          // when starts reading a new line
          blank_line = true;
        }
        else if (c != '\r') {
          // when finds a character on the current line
          blank_line = false;
        }
      }
    }  
    // closing the client connection
    delay(1);
    client.stop();
    Serial.println("Client disconnected.");
  }
}   

View raw code

Insert your network credentials in the following variables and the code will work straight away.

const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

How the Code Works

We’ve explained in great detail how a very similar web server works in a previous tutorial. Take a look at the folowing tutorial for an in-depth explanation of each line of code: Build an ESP8266 Web Server.

In this section, we’ll just take a look at the relevant parts for this example.

Importing libraries

Import the DHT libraries to read from the DHT sensor and the ESP8266WiFi library to build the web server:

#include <Adafruit_Sensor.h>
#include <DHT.h>

DHT Sensor Type

Uncomment one of the following lines for the sensor type you’re using. If you’re using a DHT22 sensor, you don’t need to modify anything.

//#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
#define DHTTYPE DHT22     // DHT 22  (AM2302), AM2321

Setting your network credentials

Insert your network credentials in the following variables, so that the ESP8266 can connect to your local network.

const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

Initialize DHT Sensor

Define the GPIO that the DHT data pin is connected to. In this case, it’s connected to GPIO5 (D1).

#define DHTPIN 5     // Digital pin connected to the DHT sensor

Instantiate a DHT object with the type and pin defined earlier.

DHT dht(DHTPIN, DHTTYPE);

setup()

In the setup(), initialize the DHT sensor.

dht.begin();

loop()

In the loop(), we check whether there’s an new client making a request:

if (client) {
    Serial.println("New client");
    // bolean to locate when the http request ends
    boolean blank_line = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

When a new client makes a request, we read the humidity, temperature in Celsius and Fahrenheit, and save them in the h, t and f variables:

float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

Finally, you send the response to the client with the HTML text to build the page as well as the temperature and humidity:

client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head></head><body><h1>ESP8266 - Temperature and Humidity</h1><h3>Temperature in Celsius: ");
client.println(celsiusTemp);
client.println("*C</h3><h3>Temperature in Fahrenheit: ");
client.println(fahrenheitTemp);
client.println("*F</h3><h3>Humidity: ");
client.println(humidityTemp);
client.println("%</h3><h3>");
client.println("</body></html>");  

The temperature and humidity are sent to the client in these variables: celsiusTemp, fahrenheitTemp and humidityTemp.

Demonstration

After making any necessary changes, upload the code to the ESP8266 as described in an earlier section.

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

Arduino IDE

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!

ESP8266 Simple DHT Web Server

Now you should see the latest temperature and humidity readings. To update the readings, you just need to refresh the web page.

Troubleshooting

If your DHT sensor fails to get the readings, read our DHT Troubleshooting Guide to help you fix the issue.

Wrapping Up

In this project we’ve shown you how to display sensor readings from a DHT sensor on a web page. We’ve provided two examples: a simple web server and an asynchronous web server with auto-updates.

Now, you can easily modify the examples provided to display readings from other sensors. If you like the ESP8266 and IoT projects take a look at some of our resources:

You may also like some of our most popular projects with the ESP8266:

Do you have any questions? Leave a comment below!

Thanks for reading.


Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

97 thoughts on “ESP8266 DHT11/DHT22 Temperature and Humidity Web Server with Arduino IDE”

      • Hello Rui, first: thanks so much, the best explained tutorial about it I’ve ever saw!
        I noticed it does use an internal ip address (LAN WiFi) not a real IP we can access from outside…I mean: did you try accessing it over internet outside same wifi network? Do you know how to do that?
        Thanks

  1. First, I wanted to thank you for your tutorials , already a real plaisur .
    However, I have a question about mounting. How can we provide power to the system for a long time . I believe a single AA battery couple is not enough for more than two or three days. I’ve heard of a solution about hibernation, but is applicable to an ESP- 01 ? And finally, what other kind of battery can be used (regardless of model lipo ? A link to Ebay to provide ? ) . Thank you for all your answers.

  2. Nice work here 🙂

    Any chance of doing more demos with Node MCU based ESP8266 projects. Running a Lua Script on the ESP8266 allows the ESp8266 to run standalone… Just a thought 🙂

  3. Rui
    If you change this line in your code, the web page will auto update every 5 seconds! Change the “5” to how many seconds you want to wait for the next update!!

    client.println(“ESP8266 – Temperature and HumidityTemperature in Celsius: “);

  4. Are you sure about the schematics? Vcc and Gnd connections to 4.7 kohm, tx pin connected to gnd rail?, nothing connected to gnd pin?

    Regards

      • Hi Rui, I am not sure if this reply will be duplicate or not, first draft was accidentally disappeared, so I write again.

        First of all thanks for your great postings, which help me a lot.

        This circuit schematics still has a mistake. 4.7 K should be a pull up resistor, whereas your sketch shows it as a pull down to GND. It does not work like this, I tried it.

        BR

  5. Great sketch!
    I need to refresh my browser to get the updated data.
    How to made the sketch to update my browser data automatically say every 5 minutes?
    Thanks.

  6. what is the problem if my esp isn’t react with the serial monitor?
    i use esp8266 as in the link below:
    encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQwg3ohvTuCUk2iTloQKOVM0-tB3lT_7Ps_j3rrURKmV1_DMXIESg
    best regards and thanks for good tutorial !

  7. Hi Rui, I try your tutorial with this esp8266 banggood.com/ESP8266-Web-Server-Port-WiFi-Expansion-Board-ESP-13-Compatible-With-Arduino-p-1008124.html?rmmds=myorder

    but I have this error loading your script on esp8266 board:

    Arduino:1.6.12 (Windows 7), Scheda:”Generic ESP8266 Module, 80 MHz, 40MHz, DIO, 115200, 512K (64K SPIFFS), ck, Disabled, None”

    Lo sketch usa 236.389 byte (54%) dello spazio disponibile per i programmi. Il massimo è 434.160 byte.
    Le variabili globali usano 32.568 byte (39%) di memoria dinamica, lasciando altri 49.352 byte liberi per le variabili locali. Il massimo è 81.920 byte.
    C:\Users\user\AppData\Local\Arduino15\packages\esp8266\tools\esptool\0.4.9/esptool.exe -vv -cd ck -cb 115200 -cp COM13 -ca 0x00000 -cf C:\Users\user\AppData\Local\Temp\arduino_build_921673/remoteTemp.ino.bin
    esptool v0.4.9 – (c) 2014 Ch. Klippel
    setting board to ck
    setting baudrate from 115200 to 115200
    setting port from COM1 to COM13
    setting address from 0x00000000 to 0x00000000
    espcomm_upload_file
    espcomm_upload_mem
    setting serial port timeouts to 1000 ms
    opening bootloader
    resetting board

    trying to connect
    flush start
    setting serial port timeouts to 1 ms
    setting serial port timeouts to 1000 ms
    flush complete
    espcomm_send_command: sending command header
    espcomm_send_command: sending command payload
    serialport_receive_C0: 00 instead of C0
    warning: espcomm_sync failed
    error: espcomm_open failed
    error: espcomm_upload_mem failed
    error: espcomm_upload_mem failed

    Can you help me to understand what is the problem and to solve it? Thanks.

    • Hi,
      I’ve never used that board, but it sounds like your Arduino IDE is not establishing a serial communication with your ESP8266 board.
      To upload a new firmware/sketch to your ESP8266 GPIO 0 must be connected to GND on boot

      • Hi yes i connect the 2 GPIO on off and i think it means GND.
        Trying with esptool i have the error A fatal error occurred: Failed to connect to ESP8266..
        Any idea?
        Thanks.

  8. iam doing a small project with arduino uno with help of ultrasonic sensor and buzzer(home alaram system ) i need help to add esp8266 to my project so that i can get message if the buzzer goes on any idea how to movie on or anything u have did already

  9. Hi Rui. When trying to compile using IDE 1.6.8 I get the following error.

    WARNING: Spurious .github folder in ‘DHT sensor library’ library
    In file included from C:\Users\user\Documents\Arduino\libraries\DHT\DHT_U.cpp:22:0:

    C:\Users\user\Documents\Arduino\libraries\DHT\DHT_U.h:25:29: fatal error: Adafruit_Sensor.h: No such file or directory

    #include

    The only change I made was to Rem out the DHT11 define and replace it with DHT21.

    Thanks for any help

    ^

    compilation terminated.

    exit status 1
    Error compiling for board Generic ESP8266 Module.

  10. Hi Rui, I can connect to the IP address of my ESP8266 using the Arduino Serial Monitor but I keep getting the message:

    New client
    Failed to read from DHT sensor!
    Client disconnected.

    Any thoughts on why this might be?
    I’m using the NodeMCU V3 with the DH11 (3pin) connecting directly.

    • I’m using the V3 also. the V3’s Vin pin is not supplying power or enough power to the dht sensor. Mine is the AM2302 and I had to power it separately but it worked fine after that

    • I think on the V3 the Vin pin is at 5volts only if you power the board via that pin. if you power it via usb it wont power the dht sensor through that pin. when I applied an external 5 volts there it began to work.

  11. Hello, Thanks for a great tutorial. What do we need to do to make the web page automatically refresh or only refresh if there is a change in the temperature?

    • I added this line in the html code.

      client.println(“”);

      I dont know if its exactly the correct way to do it. I borrowed it from some of the example code that comes with the IDE. It works on my V3

  12. Great job!! I have a question about the deep sleep mode, if you already used it in an ESP project.
    What instructions you did use for deepsleep and to wake the esp up?
    Thanks.

  13. Hi Rui,

    Thanks for this tutorial.
    Despite it’s age 😉 I am now using it as a starting point and move on to programming the Arduino, ESP8266 and possibly Raspberry Pi using (Micro-)Python.

    I made a few personal modifications to your Code to better satisfy my “needs”, but one in particular might be useful for others as well.
    This one is NOT directly related to the ESP8266, the DHT11 etc, but it might be useful if people want to “monitor” the temperature.
    If you add (be aware of the twin double-quotes) in between the tags, the resulting WebPage will refresh every 5 seconds showing different readings of the temperature sensor 😉

    Thanks for providing these great tutorials!

    Regards,
    Rob.

  14. I keep getting error messages:

    Arduino: 1.8.2 (Mac OS X), Board: “Generic ESP8266 Module, 80 MHz, 40MHz, DIO, 115200, 512K (64K SPIFFS), ck, Disabled, None”

    Build options changed, rebuilding all
    Archiving built core (caching) in: /var/folders/m_/s61mbj4s2qx7k7y9xxntjvzr0000gn/T/arduino_cache_671719/core/core_esp8266_esp8266_generic_CpuFrequency_80,FlashFreq_40,FlashMode_dio,UploadSpeed_115200,FlashSize_512K64,ResetMethod_ck,Debug_Disabled,DebugLevel_None_____d36531614ebe222a45d35a594f73c3ee.a
    Sketch uses 236301 bytes (54%) of program storage space. Maximum is 434160 bytes.
    Global variables use 32472 bytes (39%) of dynamic memory, leaving 49448 bytes for local variables. Maximum is 81920 bytes.
    warning: espcomm_sync failed
    error: espcomm_open failed
    error: espcomm_upload_mem failed
    error: espcomm_upload_mem failed

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

    I’ve tried this on a MAC and LINUX machine, same error. What am I doing wrong?

    • Hi Christopher.
      The following lines:
      warning: espcomm_sync failed
      error: espcomm_open failed
      error: espcomm_upload_mem failed
      error: espcomm_upload_mem failed
      Mean that your ESP8266 is not establishing a serial communication with your computer, or it’s not in flashing mode.
      If you’re using an ESP-01 make sure GPIO0 is connected to GND on power up.
      If you’re using another board, make sure you hold down the BOOT/FLASH button when uploading.
      I hope this helps,
      Regards,
      Sara

  15. First of all a big thanks to you. I almost completed. Nodemcu was connected to my mobile hotspot. But data is not displayed on the browser. Please help

  16. Hi!

    This is a great manual. But I faced some problem when I disconnect TLL converter form PC my esp8266 doesn’t connect to wifi, with converter everything works like a charm. Do you have an idea what it can be?

  17. “In file included from C:\Users\COMPUTER\Documents\Arduino\libraries\DHT_sensor_library\DHT_U.cpp:22:0:

    C:\Users\COMPUTER\Documents\Arduino\libraries\DHT_sensor_library\DHT_U.h:25:29: fatal error: Adafruit_Sensor.h: No such file or directory

    #include

    ^

    compilation terminated.”
    I use the esp8266-12 and faulty as above. Can you help me fix it?

    • Hi Phú.
      You need to install the Adafruit Sensor library.
      You can find the Adafruit Sensor library here: github.com/adafruit/Adafruit_Sensor
      Regards,
      Sara

  18. Hi, I have done everything that was written on this tutorial but when I enter in my IP address into a browser it says

    Temperature in Celsius: Failed *C
    Temperature in Fahrenheit: Failed *F
    Humidity: Failed %

    Is there any way to know the reason that it’s not working?

  19. HI,

    I found that if we place the sensor at a sudden change of temperature/humidity environment, we will require to refresh the webpage twice in order to receive the correct data.
    The first refresh data is not correct as the data shown did not update according to the environment change. It is still at old environment.
    Any suggestion?

    • Hi Mario.
      I think you have a DHT sensor module.
      In that case, you just need to take a look at the labels on the part. It should have VCC, OUT and GND.
      Connect VCC to the Vin pin, GND to GND, and out to D1. I think that with these modules you don’t need to add the 4.7kOhm resistor.
      I hope this helps,
      Regards,
      Sara 🙂

  20. I’m getting -999 in values (Failed to read from DHT sensor). Works great connected to my Arduino Uno, but the ESP8266 won’t read the DHT22. Any ideas? I’ve tried multiple libraries, all the same thing. I know the ESP8266 is working because it sends the -999 to Thingspeak.com. Thank you for any help.

    • Hi Al.
      Please check that the sensor is being properly powered (5V to the Vin pin of the sensor).
      Regards,
      Sara

  21. Hi Rui & Sara – this is a great tutorial, I usually find I have to tweak Arduino tutorials but this one just works! 🙂

    Best

    Andrew

  22. Very good post, even me I understood 🙂
    How to store values to make a chart (for example) by days, weeks or months?
    Thank you!
    Excuse my English, it is not my native language!

  23. sadly many compilation errors on compiling the first example. The 2nd one works.
    These are my errors they mainly seem to have to do with conversion errors:

    C:\Users\ik\Documents\Arduino\DHTWebserver2\DHTWebserver2.ino: In lambda function:
    DHTWebserver2:184: error: invalid conversion from ‘const char*’ to ‘const uint8_t* {aka const unsigned char*}’ [-fpermissive]

    In file included from C:\Users\ik\Documents\Arduino\DHTWebserver2\DHTWebserver2.ino:12:0:

    C:\Users\ik\Documents\Arduino\libraries\ESPAsyncWebServer\src/ESPAsyncWebServer.h:397:10: error: initializing argument 3 of ‘void AsyncWebServerRequest::send_P(int, const String&, const uint8_t*, size_t)’ [-fpermissive]

    void onNotFound(ArRequestHandlerFunction fn); //called when handler is not assigned

    ^

    DHTWebserver2:184: error: invalid conversion from ‘String (*)(const String&)’ to ‘size_t {aka unsigned int}’ [-fpermissive]

    In file included from C:\Users\ik\Documents\Arduino\DHTWebserver2\DHTWebserver2.ino:12:0:

    C:\Users\ik\Documents\Arduino\libraries\ESPAsyncWebServer\src/ESPAsyncWebServer.h:397:10: error: initializing argument 4 of ‘void AsyncWebServerRequest::send_P(int, const String&, const uint8_t*, size_t)’ [-fpermissive]

    void onNotFound(ArRequestHandlerFunction fn); //called when handler is not assigned
    ^
    exit status 1
    invalid conversion from ‘const char*’ to ‘const uint8_t* {aka const unsigned char*}’ [-fpermissive]

    Could it be that maybe some libraries have changed since you designed this program

  24. OK, sorry for all the previous comments, problem seems solved.
    What happened is that I had an old copy of the ESPAsyncWebServer and although my compilation monitor said it was using the new one, I guess it got confused and may have picked up a file from the old one. Deleted the old one and compiles fine now

  25. When I try and go to the web page in my browser, the program seems to crash. I’ve redownloaded the latest TCP and WebServer packages as well as the code.
    Here’s the serial com output when it happens:
    Connecting to WiFi
    .
    .
    .
    .
    192.168.1.18

    Panic C:\Users\ClifS\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.1\cores\esp8266\core_esp8266_main.cpp:103 __yield

    >>>stack>>>

    ets Jan 8 2013,rst cause:2, boot mode:(3,6)

    load 0x4010f000, len 1384, room 16
    tail 8
    chksum 0x2d
    csum 0x2d
    vac02aff5
    ~ld

    • Hi.
      Please follow this topic: github.com/esp8266/Arduino/issues/2414
      They provide some solutions that may help, including erasing the ESP8266 flash.
      I hope this helps.
      Regards,
      Sara

    • Hi, I have had exactly the same problem. Finally I have discovered what’s wrong there and how to solve it but it took me whole day 🙂 . The problem is with Adafruit’s DHT library. It calls yield() function in dht.cpp, which is prohibited inside a AsynchronousWebserver request handler. The solution is just to comment-out the yield() call in dht.cpp library source (in my version on line 159). Then it will work just fine.

      • Hey Radek,
        Thank you so much for working through that. As you said, once I commented out the yield() in the dht.cpp library, I stopped getting the stack errors.
        Best regards,
        Clif

  26. I using Nodemcu as AP Mode, It shows on serial monitor “Failed to read from DHT sensor”. I have checked with only DHT code it shows the readings.

    /*********
    Rui Santos
    Complete project details at https://randomnerdtutorials.com
    *********/

    // Import required libraries
    #include
    #include
    #include
    #include
    #include
    #include
    #include

    // Replace with your network credentials
    const char* ssid = “Weather”;
    const char* password = “12345678”;

    #define DHTPIN D1 // Digital pin connected to the DHT sensor

    // Uncomment the type of sensor in use:
    #define DHTTYPE DHT11 // DHT 11
    //#define DHTTYPE DHT22 // DHT 22 (AM2302)
    //#define DHTTYPE DHT21 // DHT 21 (AM2301)

    DHT dht(DHTPIN, DHTTYPE);

    // Create AsyncWebServer object on port 80
    AsyncWebServer server(80);

    String readDHTTemperature() {
    // Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
    // Read temperature as Celsius (the default)
    float t = dht.readTemperature();
    delay(1000);
    // Read temperature as Fahrenheit (isFahrenheit = true)
    //float t = dht.readTemperature(true);
    // Check if any reads failed and exit early (to try again).
    if (isnan(t)) {
    Serial.println(“Failed to read from DHT sensor!”);
    return “–“;
    }
    else {
    Serial.println(t);
    return String(t);
    }
    }

    String readDHTHumidity() {
    // Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
    float h = dht.readHumidity();
    if (isnan(h)) {
    Serial.println(“Failed to read from DHT sensor!”);
    return “–“;
    }
    else {
    Serial.println(h);
    return String(h);
    }
    }

    const char index_html[] PROGMEM = R”rawliteral(

    html {
    font-family: Arial;
    display: inline-block;
    margin: 0px auto;
    text-align: center;
    }
    h2 { font-size: 3.0rem; }
    p { font-size: 3.0rem; }
    .units { font-size: 1.2rem; }
    .dht-labels{
    font-size: 1.5rem;
    vertical-align:middle;
    padding-bottom: 15px;
    }

    ESP8266 DHT Server


    Temperature
    %TEMPERATURE%
    °C


    Humidity
    %HUMIDITY%
    %

    setInterval(function ( ) {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
    document.getElementById(“temperature”).innerHTML = this.responseText;
    }
    };
    xhttp.open(“GET”, “/temperature”, true);
    xhttp.send();
    }, 10000 ) ;

    setInterval(function ( ) {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
    document.getElementById(“humidity”).innerHTML = this.responseText;
    }
    };
    xhttp.open(“GET”, “/humidity”, true);
    xhttp.send();
    }, 10000 ) ;

    )rawliteral”;

    // Replaces placeholder with DHT values
    String processor(const String& var){
    //Serial.println(var);
    if(var == “TEMPERATURE”){
    return readDHTTemperature();
    }
    else if(var == “HUMIDITY”){
    return readDHTHumidity();
    }
    return String();
    }

    void setup(){
    // Serial port for debugging purposes
    Serial.begin(115200);
    dht.begin();
    // Connect to Wi-Fi
    WiFi.softAP(ssid, password);
    IPAddress myIP = WiFi.softAPIP();
    Serial.println(“AP IP address: “);
    Serial.println(myIP);
    delay(1000);
    // Print ESP8266 Local IP Address

    // Route for root / web page
    server.on(“/”, HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, “text/html”, index_html, processor);
    });
    server.on(“/temperature”, HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, “text/plain”, readDHTTemperature().c_str());
    });
    server.on(“/humidity”, HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, “text/plain”, readDHTHumidity().c_str());
    });

    // Start server
    server.begin();
    }

    void loop(){

    }

  27. Hi Rui / Sara

    Your ESP8266 and DHT11/DHT22 Schematic Diagram seems to be incorrectly connected – the other side of the 4k7 resistor connected to the data pin should be connected to +V. not Gnd.
    Regards,
    Leon

    • Hi Leon.
      You are right. Thanks for noticing.
      I’ve replaced the schematic with the correct one.
      Regards,
      Sara

  28. Hey Sara, Thank u very much for the tutorial.
    A question: What should I change if I must use a DHT11 sensor?

    • Hi Luis.
      You just need to uncomment the sensor type you’re using in the code.
      So, replace the next lines:
      //#define DHTTYPE DHT11 // DHT 11
      #define DHTTYPE DHT22 // DHT 22 (AM2302)
      //#define DHTTYPE DHT21 // DHT 21 (AM2301)
      With this ones:
      #define DHTTYPE DHT11 // DHT 11
      //#define DHTTYPE DHT22 // DHT 22 (AM2302)
      //#define DHTTYPE DHT21 // DHT 21 (AM2301)

      I hope this helps.
      Regards,
      Sara

  29. My main interest was with the Async website as I have made regular straight http websites sort of like your webserver nr 2 already.
    There is one thing I would like to add though regarding your temp&humidity webserver nr 2:
    Normally, when one opens a regular webpage on a mobile, the font is pretty small and you have to manually zoom in to be able to read it. Sadly though, on the next refresh, you have to zoom in again and again and again.
    You seem to have solved it by putting the text in between header tags, so the font becomes bigger.

    There might however be a more elegant method, by adding ‘viewport’ to the metadata one sends in the http page.

    In my program I send the following in the header:

    As this has some “fish hooks” in it, I am not sure if this page will leave it ungarbled as is
    So I will also print it here as well without the enclosing “fish hooks”
    meta name=\”viewport\” content=\”width=device-width, initial-scale=1\” /
    , but just in case it is explained here as well: https://www.w3schools.com/html/html_responsive.asp
    mind you that in the w3school example wherever you come across a quote sigh, you probably need to ‘escape’ it so ” becomes \”

    Anyway, what it does is to already set an initial scale for the text.

  30. Hi there,

    Instead of the ESP8266 12-E NodeMCU Kit, I am using the ESP8266 12-F NodeMCU Kit, and I cannot get it to work. I read that the only difference between the two was the wifi aerial layout, and that the 12-E sketch should work – am I mistaken?

  31. Fantastic project. Thank you.

    Having an issues that have not been able to resolve yet. Any suggestions appreciated. The error message is “xtensa-lx106-elf-g++: error: CreateProcess: No such file or directory”

    Have turned AV off, run as admin, confirmed permissions for the IDE but still get the same error. Would love to use this project so any help very appreciated.

  32. Awesome awsomeness! This works great. For some reason my IDE has to have the 8266 boards deleted and then reinstalled to avoid the “xtensa-lx106-elf-g++: error: CreateProcess: No such file or directory”. But now that I know that it works perfect.

    An observation and feedback if any ideas: the same DHT11 reads locally with an attiny 3 degrees lower than the web server.

    Also, is there a way to permanently assign the ip address within the 8266 sketch? Or query the IP address of the 8266 from a browser?

    Thanks again. Great project!

  33. Hi Sara, Hi Rui,

    what should be changed to host the web page on a web site instead of on ESP8266 web server?

  34. Hi Rui, Hi Sara, please let me know what I have to change in order to add another further variable on the web server, for exaple I would add “Heat Index”, I modified the sketch but, I see regularly shown on the serial monitor my heat index, but not on the server.

    I added the function:

    String readDHTHeatindex() {

    //Calcolo dell’indice di calore in gradi Celsius (Fahreheit = false)

    float h = dht.readHumidity();
    float t = dht.readTemperature();
    float hic = dht.computeHeatIndex(t, h, false);

    //Verifica se la lettura è andata a buon fine.

    if (isnan(hic)) {
    Serial.println(“Errore lettura sensore DHT!”);
    return “–“;
    }
    else {
    Serial.println(hic);
    return String(hic);
    }

    And I made some modification on the web server code:

    (…)

    All the rest is the same made from you.

    PS: please be so kind tell me whats does mean this row

    if (this.readyState == 4 && this.status == 200)

    what is 4 and what is 200?

    • Hi.
      From what I see, you seem to be doing the right procedures.
      Do you get any errors on the Serial Monitor?

      About 4 and 200 you can read more here: stackoverflow.com/questions/17561463/readystate-vs-status-200
      Regards,
      Sara

  35. Rui,

    Super job and thanks for all the responses from other contributors. The DHT yield() comments really helped.

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.