{"id":21652,"date":"2019-05-07T08:46:29","date_gmt":"2019-05-07T08:46:29","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=21652"},"modified":"2025-04-14T08:53:07","modified_gmt":"2025-04-14T08:53:07","slug":"esp8266-dht11dht22-temperature-and-humidity-web-server-with-arduino-ide","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp8266-dht11dht22-temperature-and-humidity-web-server-with-arduino-ide\/","title":{"rendered":"ESP8266 DHT11\/DHT22 Temperature and Humidity Web Server with Arduino IDE"},"content":{"rendered":"\n<p>In this project you\u2019ll 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&#8217;ll build can be accessed with any device that has a browser on your local network.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP8266-DHT11-DHT22-Web-Server.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 DHT11 DHT22 Temperature and Humidity Web Server with Arduino IDE\" class=\"wp-image-84914\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP8266-DHT11-DHT22-Web-Server.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP8266-DHT11-DHT22-Web-Server.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP8266-DHT11-DHT22-Web-Server.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP8266-DHT11-DHT22-Web-Server.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p>Throughout this tutorial we&#8217;ll show how to build two different web servers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>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.<\/li>\n\n\n\n<li>Web Server #2: Simple HTTP web server that displays the latest sensor readings when the page is updated in a raw HTML page.<\/li>\n<\/ul>\n\n\n\n<p><strong>Recommended resources:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/randomnerdtutorials.com\/how-to-install-esp8266-board-arduino-ide\/\" target=\"_blank\">How to Install the ESP8266 Board in Arduino IDE<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/randomnerdtutorials.com\/esp8266-web-server\/\" target=\"_blank\">ESP8266 Web Server<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/best-esp8266-wi-fi-development-board\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">ESP8266 Boards Comparison<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/complete-guide-for-dht11dht22-humidity-and-temperature-sensor-with-arduino\/\">DHT11\/DHT22 Humidity and Temperature Sensor with Arduino<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-pinout-reference-gpios\/\">ESP8266 Pinout: Ultimate GPIOs Guide<\/a><\/li>\n<\/ul>\n\n\n\n<p class=\"rntbox rntclblue\">Learn more about the ESP8266 with our course: <a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\">Home Automation using ESP8266<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required <\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP8266-DHT11-DHT22-Web-Server-Circuit-Schematic.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-84917\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP8266-DHT11-DHT22-Web-Server-Circuit-Schematic.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP8266-DHT11-DHT22-Web-Server-Circuit-Schematic.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<p>To build this project, you need the following parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\">ESP8266 development board<\/a> (read <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/best-esp8266-wi-fi-development-board\/\" target=\"_blank\">ESP8266 development boards comparison<\/a>)<\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/dht22-temperature-humidity-sensor\/\" target=\"_blank\">DHT22<\/a>&nbsp;or&nbsp;<a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/dht11-temperature-humidity-sensor\/\" target=\"_blank\">DHT11<\/a>&nbsp;Temperature and Humidity Sensor<\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\">4.7k Ohm Resistor<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Jumper wires<\/a><\/li>\n<\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ESP8266 and DHT11\/DHT22 Schematic Diagram<\/h2>\n\n\n\n<p>Before proceeding with the tutorial, wire the DHT11 or DHT22 temperature and humidity sensor to the ESP8266 as shown in the following schematic diagram.<\/p>\n\n\n<div class=\"wp-block-image is-resized\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"642\" height=\"826\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/esp8266-dht.png?resize=642%2C826&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 DHT11 DHT22 Schematic Diagram Circuit\" class=\"wp-image-86097\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/esp8266-dht.png?w=642&amp;quality=100&amp;strip=all&amp;ssl=1 642w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/06\/esp8266-dht.png?resize=233%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 233w\" sizes=\"(max-width: 642px) 100vw, 642px\" \/><\/figure><\/div>\n\n\n<p>In this example, we&#8217;re wiring the DHT data pin to <span class=\"rnthl rntcblue\">GPIO5<\/span> (D1), but you can use any other suitable GPIO. Read our <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-pinout-reference-gpios\/\">ESP8266 GPIO Reference Guide<\/a> to learn more about the ESP8266 GPIOs. <\/p>\n\n\n\n<p>If you&#8217;re using an ESP-01, <span class=\"rnthl rntcblue\">GPIO 2<\/span> is the most suitable pin to connect to the DHT data pin, as shown in the next diagram.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/dht22-esp-01.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"964\" height=\"918\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/dht22-esp-01.png?resize=964%2C918&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP-01 DHT11 DHT22 Schematic Diagram Circuit\" class=\"wp-image-84950\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/dht22-esp-01.png?w=964&amp;quality=100&amp;strip=all&amp;ssl=1 964w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/dht22-esp-01.png?resize=300%2C286&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/dht22-esp-01.png?resize=768%2C731&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 964px) 100vw, 964px\" \/><\/a><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Installing the DHT Library for ESP8266<\/h2>\n\n\n\n<p>To read from the DHT sensor, we\u2019ll use the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/adafruit\/DHT-sensor-library\" target=\"_blank\">DHT library from Adafruit<\/a>. To use this library you also need to install the&nbsp;<a href=\"https:\/\/github.com\/adafruit\/Adafruit_Sensor\" target=\"_blank\" rel=\"noreferrer noopener\">Adafruit Unified Sensor library<\/a>. Follow the next steps to install those libraries.<\/p>\n\n\n\n<p>1. Open your Arduino IDE and go to <strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong>. The Library Manager should open.<\/p>\n\n\n\n<p>2. Search for \u201c<strong>DHT<\/strong>\u201d on the Search box and install the DHT library from Adafruit.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_dht_library.png?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing Adafruit DHT library\" class=\"wp-image-84293\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_dht_library.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_dht_library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<p>3. After installing the DHT library from Adafruit, type \u201c<strong>Adafruit Unified Sensor<\/strong>\u201d in the search box. Scroll all the way down to find the library and install it.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing Adafruit Unified Sensor driver library\" class=\"wp-image-84295\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<p>After installing the libraries, restart your Arduino IDE.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. ESP8266 Asynchronous Web Server<\/h2>\n\n\n\n<p>To build the web server we\u2019ll use the&nbsp;<a href=\"https:\/\/github.com\/ESP32Async\/ESPAsyncWebServer\" target=\"_blank\" rel=\"noopener\" title=\"\">ESPAsyncWebServer<\/a> 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 <a href=\"https:\/\/github.com\/ESP32Async\/ESPAsyncWebServer\/wiki\" target=\"_blank\" rel=\"noopener\" title=\"\">library documentation on its GitHub page<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installing the Web Server Libraries<\/h3>\n\n\n\n<p>We&#8217;ll build the web server using the following libraries:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/ESP32Async\/ESPAsyncWebServer\" target=\"_blank\" rel=\"noopener\" title=\"\">ESPAsyncWebServer by ESP32Async<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/ESP32Async\/ESPAsyncTCP\" target=\"_blank\" rel=\"noopener\" title=\"\">ESPAsyncTCP&nbsp;by ESP32Async<\/a><\/li>\n<\/ul>\n\n\n\n<p>You can install these libraries using the Arduino Library Manager. Go to&nbsp;<strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong>&nbsp;and search for the libraries&#8217; names.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code<\/h3>\n\n\n\n<p>We\u2019ll program the ESP8266 using Arduino IDE, so you must have the ESP8266 add-on installed in your Arduino IDE. If you haven&#8217;t, follow the next tutorial first:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-esp8266-nodemcu-arduino-ide-2-0\/\" title=\"\">Install the ESP8266 Board in Arduino IDE<\/a><\/li>\n<\/ul>\n\n\n\n<p>Open your Arduino IDE and copy the following code.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at https:\/\/randomnerdtutorials.com\/esp8266-dht11dht22-temperature-and-humidity-web-server-with-arduino-ide\/\n*********\/\n\n\/\/ Import required libraries\n#include &lt;Arduino.h&gt;\n#include &lt;ESP8266WiFi.h&gt;\n#include &lt;Hash.h&gt;\n#include &lt;ESPAsyncTCP.h&gt;\n#include &lt;ESPAsyncWebServer.h&gt;\n#include &lt;Adafruit_Sensor.h&gt;\n#include &lt;DHT.h&gt;\n\n\/\/ Replace with your network credentials\nconst char* ssid = &quot;REPLACE_WITH_YOUR_SSID&quot;;\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\n\n#define DHTPIN 5     \/\/ Digital pin connected to the DHT sensor\n\n\/\/ Uncomment the type of sensor in use:\n\/\/#define DHTTYPE    DHT11     \/\/ DHT 11\n#define DHTTYPE    DHT22     \/\/ DHT 22 (AM2302)\n\/\/#define DHTTYPE    DHT21     \/\/ DHT 21 (AM2301)\n\nDHT dht(DHTPIN, DHTTYPE);\n\n\/\/ current temperature &amp; humidity, updated in loop()\nfloat t = 0.0;\nfloat h = 0.0;\n\n\/\/ Create AsyncWebServer object on port 80\nAsyncWebServer server(80);\n\n\/\/ Generally, you should use &quot;unsigned long&quot; for variables that hold time\n\/\/ The value will quickly become too large for an int to store\nunsigned long previousMillis = 0;    \/\/ will store last time DHT was updated\n\n\/\/ Updates DHT readings every 10 seconds\nconst long interval = 10000;  \n\nconst char index_html[] PROGMEM = R&quot;rawliteral(\n&lt;!DOCTYPE HTML&gt;&lt;html&gt;\n&lt;head&gt;\n  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;\n  &lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/use.fontawesome.com\/releases\/v5.7.2\/css\/all.css&quot; integrity=&quot;sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr&quot; crossorigin=&quot;anonymous&quot;&gt;\n  &lt;style&gt;\n    html {\n     font-family: Arial;\n     display: inline-block;\n     margin: 0px auto;\n     text-align: center;\n    }\n    h2 { font-size: 3.0rem; }\n    p { font-size: 3.0rem; }\n    .units { font-size: 1.2rem; }\n    .dht-labels{\n      font-size: 1.5rem;\n      vertical-align:middle;\n      padding-bottom: 15px;\n    }\n  &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;h2&gt;ESP8266 DHT Server&lt;\/h2&gt;\n  &lt;p&gt;\n    &lt;i class=&quot;fas fa-thermometer-half&quot; style=&quot;color:#059e8a;&quot;&gt;&lt;\/i&gt; \n    &lt;span class=&quot;dht-labels&quot;&gt;Temperature&lt;\/span&gt; \n    &lt;span id=&quot;temperature&quot;&gt;%TEMPERATURE%&lt;\/span&gt;\n    &lt;sup class=&quot;units&quot;&gt;&amp;deg;C&lt;\/sup&gt;\n  &lt;\/p&gt;\n  &lt;p&gt;\n    &lt;i class=&quot;fas fa-tint&quot; style=&quot;color:#00add6;&quot;&gt;&lt;\/i&gt; \n    &lt;span class=&quot;dht-labels&quot;&gt;Humidity&lt;\/span&gt;\n    &lt;span id=&quot;humidity&quot;&gt;%HUMIDITY%&lt;\/span&gt;\n    &lt;sup class=&quot;units&quot;&gt;%&lt;\/sup&gt;\n  &lt;\/p&gt;\n&lt;\/body&gt;\n&lt;script&gt;\nsetInterval(function ( ) {\n  var xhttp = new XMLHttpRequest();\n  xhttp.onreadystatechange = function() {\n    if (this.readyState == 4 &amp;&amp; this.status == 200) {\n      document.getElementById(&quot;temperature&quot;).innerHTML = this.responseText;\n    }\n  };\n  xhttp.open(&quot;GET&quot;, &quot;\/temperature&quot;, true);\n  xhttp.send();\n}, 10000 ) ;\n\nsetInterval(function ( ) {\n  var xhttp = new XMLHttpRequest();\n  xhttp.onreadystatechange = function() {\n    if (this.readyState == 4 &amp;&amp; this.status == 200) {\n      document.getElementById(&quot;humidity&quot;).innerHTML = this.responseText;\n    }\n  };\n  xhttp.open(&quot;GET&quot;, &quot;\/humidity&quot;, true);\n  xhttp.send();\n}, 10000 ) ;\n&lt;\/script&gt;\n&lt;\/html&gt;)rawliteral&quot;;\n\n\/\/ Replaces placeholder with DHT values\nString processor(const String&amp; var){\n  \/\/Serial.println(var);\n  if(var == &quot;TEMPERATURE&quot;){\n    return String(t);\n  }\n  else if(var == &quot;HUMIDITY&quot;){\n    return String(h);\n  }\n  return String();\n}\n\nvoid setup(){\n  \/\/ Serial port for debugging purposes\n  Serial.begin(115200);\n  dht.begin();\n  \n  \/\/ Connect to Wi-Fi\n  WiFi.begin(ssid, password);\n  Serial.println(&quot;Connecting to WiFi&quot;);\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(1000);\n    Serial.println(&quot;.&quot;);\n  }\n\n  \/\/ Print ESP8266 Local IP Address\n  Serial.println(WiFi.localIP());\n\n  \/\/ Route for root \/ web page\n  server.on(&quot;\/&quot;, HTTP_GET, [](AsyncWebServerRequest *request){\n    request-&gt;send(200, &quot;text\/html&quot;, index_html, processor);\n  });\n  server.on(&quot;\/temperature&quot;, HTTP_GET, [](AsyncWebServerRequest *request){\n    request-&gt;send(200, &quot;text\/plain&quot;, String(t).c_str());\n  });\n  server.on(&quot;\/humidity&quot;, HTTP_GET, [](AsyncWebServerRequest *request){\n    request-&gt;send(200, &quot;text\/plain&quot;, String(h).c_str());\n  });\n\n  \/\/ Start server\n  server.begin();\n}\n \nvoid loop(){  \n  unsigned long currentMillis = millis();\n  if (currentMillis - previousMillis &gt;= interval) {\n    \/\/ save the last time you updated the DHT values\n    previousMillis = currentMillis;\n    \/\/ Read temperature as Celsius (the default)\n    float newT = dht.readTemperature();\n    \/\/ Read temperature as Fahrenheit (isFahrenheit = true)\n    \/\/float newT = dht.readTemperature(true);\n    \/\/ if temperature read failed, don't change t value\n    if (isnan(newT)) {\n      Serial.println(&quot;Failed to read from DHT sensor!&quot;);\n    }\n    else {\n      t = newT;\n      Serial.println(t);\n    }\n    \/\/ Read Humidity\n    float newH = dht.readHumidity();\n    \/\/ if humidity read failed, don't change h value \n    if (isnan(newH)) {\n      Serial.println(&quot;Failed to read from DHT sensor!&quot;);\n    }\n    else {\n      h = newH;\n      Serial.println(h);\n    }\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266\/ESP8266_WiFi_DHT_Web_Server.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Insert your network credentials in the following variables and the code will work straight away.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* ssid = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>In the following paragraphs we&#8217;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. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Importing libraries<\/h4>\n\n\n\n<p>First, import the required libraries.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Arduino.h&gt;\n#include &lt;ESP8266WiFi.h&gt;\n#include &lt;Hash.h&gt;\n#include &lt;ESPAsyncTCP.h&gt;\n#include &lt;ESPAsyncWebServer.h&gt;\n#include &lt;Adafruit_Sensor.h&gt;\n#include &lt;DHT.h&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Setting your network credentials<\/h4>\n\n\n\n<p>Insert your network credentials in the following variables, so that the ESP8266 can connect to your local network.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* ssid = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Variables definition<\/h4>\n\n\n\n<p>Define the GPIO that the DHT data pin is connected to. In this case, it\u2019s connected to <span class=\"rnthl rntcblue\">GPIO5<\/span> (D1).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define DHTPIN 5     \/\/ Digital pin connected to the DHT sensor<\/code><\/pre>\n\n\n\n<p>Then, select the DHT sensor type you\u2019re using. In our example, we\u2019re using the DHT22. If you\u2019re using another type, you just need to uncomment your sensor and comment all the others.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define DHTTYPE DHT22   \/\/ DHT 22 (AM2302)<\/code><\/pre>\n\n\n\n<p>Instantiate a <span class=\"rnthl rntliteral\">DHT<\/span>object with the type and pin defined earlier.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>DHT dht(DHTPIN, DHTTYPE);<\/code><\/pre>\n\n\n\n<p>Create an <span class=\"rnthl rntliteral\">AsyncWebServer<\/span>object on port 80.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>AsyncWebServer server(80);<\/code><\/pre>\n\n\n\n<p>Create float variables to hold the current temperature and humidity values. The temperature and humidity are updated in the <span class=\"rnthl rntliteral\">loop()<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float t = 0.0;\nfloat h = 0.0;<\/code><\/pre>\n\n\n\n<p>Create timer variables needed to update the temperature readings every 10 seconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>unsigned long previousMillis = 0;    \/\/ will store last time DHT was updated\n\n\/\/ Updates DHT readings every 10 seconds\nconst long interval = 10000; <\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Building the Web Page<\/h4>\n\n\n\n<p>Proceeding to the web server page.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"633\" height=\"561\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/web-server-labeled.png?resize=633%2C561&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-84921\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/web-server-labeled.png?w=633&amp;quality=100&amp;strip=all&amp;ssl=1 633w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/web-server-labeled.png?resize=300%2C266&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 633px) 100vw, 633px\" \/><\/figure><\/div>\n\n\n<p>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. <\/p>\n\n\n\n<p>Let\u2019s see how this web page is created. <\/p>\n\n\n\n<p>All the HTML text with styles included is stored in the <span class=\"rnthl rntliteral\">index_html<\/span> variable. Now we\u2019ll go through the HTML text and see what each part does.<\/p>\n\n\n\n<p>The following <span style=\"color: #333399;\">&lt;meta&gt;<\/span> tag makes your web page responsive in any browser.<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"&gt;<\/code><\/pre>\n\n\n\n<p>The <span style=\"color: #333399;\">&lt;link&gt;<\/span> tag is needed to load the icons from the fontawesome website.<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;link rel=\"stylesheet\" href=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/font-awesome\/6.5.1\/css\/all.min.css\"&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Styles<\/h4>\n\n\n\n<p>Between the <span style=\"color: #333399;\">&lt;style&gt;&lt;\/style&gt;<\/span> tags, we add some CSS to style the web page.<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;style&gt;\n html {\n   font-family: Arial;\n   display: inline-block;\n   margin: 0px auto;\n   text-align: center;\n  }\n  h2 { font-size: 3.0rem; }\n  p { font-size: 3.0rem; }\n  .units { font-size: 1.2rem; }\n  .dht-labels{\n    font-size: 1.5rem;\n    vertical-align:middle;\n    padding-bottom: 15px;\n  }\n&lt;\/style&gt;<\/code><\/pre>\n\n\n\n<p>Basically, we&#8217;re setting the HTML page to display the text with Arial font in block without margin, and aligned at the center.<\/p>\n\n\n\n<pre class=\"wp-block-code language-css\"><code>html {\n  font-family: Arial;\n  display: inline-block;\n  margin: 0px auto;\n  text-align: center;\n}<\/code><\/pre>\n\n\n\n<p>We set the font size for the heading (<span class=\"rnthl rntliteral\">h2<\/span>), paragraph (<span class=\"rnthl rntliteral\">p<\/span>) and the units(<span class=\"rnthl rntliteral\">.units<\/span>) of the readings.<\/p>\n\n\n\n<pre class=\"wp-block-code language-css\"><code>h2 { font-size: 3.0rem; }\np { font-size: 3.0rem; }\n.units { font-size: 1.2rem; }<\/code><\/pre>\n\n\n\n<p>The labels for the readings are styled as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code language-css\"><code>dht-labels{\n  font-size: 1.5rem;\n  vertical-align:middle;\n  padding-bottom: 15px;\n}<\/code><\/pre>\n\n\n\n<p>All of the previous tags should go between the <span style=\"color: #333399;\">&lt;head&gt;<\/span> and <span style=\"color: #333399;\">&lt;\/head&gt;<\/span> tags. These tags are used to include content that is not directly visible to the user, like the <span style=\"color: #333399;\">&lt;meta&gt;<\/span> , the <span style=\"color: #333399;\">&lt;link&gt;<\/span> tags, and the styles.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">HTML Body<\/h4>\n\n\n\n<p>Inside the <span style=\"color: #333399;\">&lt;body&gt;&lt;\/body&gt;<\/span> tags is where we add the web page content.<br><\/p>\n\n\n\n<p>The <span style=\"color: #333399;\">&lt;h2&gt;&lt;\/h2&gt;<\/span> tags add a heading to the web page. In this case, the \u201cESP8266 DHT server\u201d text, but you can add any other text.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;h2&gt;ESP8266 DHT Server&lt;\/h2&gt;<\/code><\/pre>\n\n\n\n<p>Then, there are two paragraphs. One to display the temperature and the other to display the humidity. The paragraphs are delimited by the <span style=\"color: #333399;\">&lt;p&gt;<\/span> and <span style=\"color: #333399;\">&lt;\/p&gt;<\/span> tags. The paragraph for the temperature is the following:<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;p&gt;\n  &lt;i class=\"fas fa-thermometer-half\" style=\"color:#059e8a;\"&lt;\/i&gt; \n  &lt;span class=\"dht-labels\"&gt;Temperature&lt;\/span&gt; \n  &lt;span id=\"temperature\"&gt;%TEMPERATURE%&lt;\/span&gt;\n  &lt;sup class=\"units\"&gt;\u00b0C&lt;\/sup&gt;\n&lt;\/p&gt;<\/code><\/pre>\n\n\n\n<p>And the paragraph for the humidity is on the following snipet:<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;p&gt;\n  &lt;i class=\"fas fa-tint\" style=\"color:#00add6;\"&gt;&lt;\/i&gt; \n  &lt;span class=\"dht-labels\"&gt;Humidity&lt;\/span&gt;\n  &lt;span id=\"humidity\"&gt;%HUMIDITY%&lt;\/span&gt;\n  &lt;sup class=\"units\"&gt;%&lt;\/sup&gt;\n&lt;\/p&gt;<\/code><\/pre>\n\n\n\n<p>The <span style=\"color: #333399;\">&lt;i&gt;<\/span> tags display the fontawesome icons. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How to display icons<\/h4>\n\n\n\n<p>To chose the icons, go to the <a href=\"https:\/\/fontawesome.com\/icons?d=gallery\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Font Awesome Icons website<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"476\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/fontawesomepage.png?resize=750%2C476&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-83530\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/fontawesomepage.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/fontawesomepage.png?resize=300%2C190&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<p>Search the icon you&#8217;re looking for. For example, &#8220;thermometer&#8221;.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"476\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/fontawesome-thermometer.png?resize=750%2C476&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-83531\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/fontawesome-thermometer.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/fontawesome-thermometer.png?resize=300%2C190&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<p>Click the desired icon. Then, you just need to copy the HTML text provided.<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;i class=\"fas fa-thermometer-half\"&gt;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"476\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/fontawesome-thermometer-html.png?resize=750%2C476&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-83532\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/fontawesome-thermometer-html.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/03\/fontawesome-thermometer-html.png?resize=300%2C190&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure>\n\n\n\n<p>To chose the color, you just need to pass the style parameter with the color in hexadecimal, as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;i class=\"fas fa-tint\" style=\"color:#00add6;\"&gt;&lt;\/i&gt; <\/code><\/pre>\n\n\n\n<p>Proceeding with the HTML text&#8230;<\/p>\n\n\n\n<p>The next line writes the word &#8220;Temperature&#8221; into the web page. <\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;span class=\"dht-labels\"&gt;Temperature&lt;\/span&gt;<\/code><\/pre>\n\n\n\n<p>The TEMPERATURE text between % signs is a placeholder for the temperature value. <br><\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;span id=\"temperature\"&gt;%TEMPERATURE%&lt;\/span&gt;<\/code><\/pre>\n\n\n\n<p>This means that this <strong>%TEMPERATURE%<\/strong> 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. <\/p>\n\n\n\n<p>Finally, we add the degree symbol.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;sup class=\"units\"&gt;\u00b0C&lt;\/sup&gt;<\/code><\/pre>\n\n\n\n<p>The <span style=\"color: #333399;\">&lt;sup&gt;&lt;<\/span><span style=\"color: #333399;\">\/sup&gt;<\/span> tags make the text superscript. <\/p>\n\n\n\n<p class=\"language-c\">We use the same approach for the humidity paragraph, but it uses a different icon and the <strong>%HUMIDITY%<\/strong> placeholder.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;p&gt;\n  &lt;i class=\"fas fa-tint\" style=\"color:#00add6;\"&gt;&lt;\/i&gt; \n  &lt;span class=\"dht-labels\"&gt;Humidity&lt;\/span&gt;\n  &lt;span id=\"humidity\"&gt;%HUMIDITY%&lt;\/span&gt;\n  &lt;sup class=\"units\"&gt;%&lt;\/sup&gt;\n&lt;\/p&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Automatic Updates<\/h4>\n\n\n\n<p>Finally, there\u2019s some JavaScript code in our web page that updates the temperature and humidity automatically, every 10 seconds. <\/p>\n\n\n\n<p>Scripts in HTML text should go between the <span style=\"color: #333399;\">&lt;script&gt;&lt;\/script&gt;<\/span> tags.<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;script&gt;\nsetInterval(function ( ) {\n  var xhttp = new XMLHttpRequest();\n  xhttp.onreadystatechange = function() {\n    if (this.readyState == 4 &amp;&amp; this.status == 200) {\n      document.getElementById(\"temperature\").innerHTML = this.responseText;\n    }\n  };\n  xhttp.open(\"GET\", \"\/temperature\", true);\n  xhttp.send();\n}, 10000 ) ;\n\nsetInterval(function ( ) {\n  var xhttp = new XMLHttpRequest();\n  xhttp.onreadystatechange = function() {\n    if (this.readyState == 4 &amp;&amp; this.status == 200) {\n      document.getElementById(\"humidity\").innerHTML = this.responseText;\n    }\n  };\n  xhttp.open(\"GET\", \"\/humidity\", true);\n  xhttp.send();\n}, 10000 ) ;\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>To update the temperature on the background, we have a <span class=\"rnthl rntliteral\">setInterval()<\/span> function that runs every 10 seconds. <\/p>\n\n\n\n<p>Basically, it makes a request in the <em>\/temperature<\/em> URL to get the latest temperature reading. <\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>  xhttp.open(\"GET\", \"\/temperature\", true);\n  xhttp.send();\n}, 10000 ) ;<\/code><\/pre>\n\n\n\n<p>When it receives that value, it updates the HTML element whose id is <span class=\"rnthl rntliteral\">temperature<\/span>. <\/p>\n\n\n\n<pre class=\"wp-block-code language-javascript\"><code>if (this.readyState == 4 &amp;&amp; this.status == 200) {\n  document.getElementById(\"temperature\").innerHTML = this.responseText;\n}<\/code><\/pre>\n\n\n\n<p>In summary, this previous section is responsible for updating the temperature asynchronously. The same process is repeated for the humidity readings.<br><\/p>\n\n\n\n<p class=\"rntbox rntcred\"><strong>Important: <\/strong> 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.<br><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Processor<\/h4>\n\n\n\n<p>Now, we need to create the <span class=\"rnthl rntliteral\">processor()<\/span> function, that will replace the placeholders in our HTML text with the actual temperature and humidity values.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String processor(const String&amp; var){\n  \/\/Serial.println(var);\n  if(var == \"TEMPERATURE\"){\n    return String(t);\n  }\n  else if(var == \"HUMIDITY\"){\n    return String(h);\n  }\n  return String();\n}<\/code><\/pre>\n\n\n\n<p>When the web page is requested, we check if the HTML has any placeholders. If it finds the <strong>%TEMPERATURE%<\/strong> placeholder, we return the temperature that is stored on the  <span class=\"rnthl rntliteral\">t<\/span> variable.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if(var == \"TEMPERATURE\"){\n  return String(t);\n}<\/code><\/pre>\n\n\n\n<p>If the placeholder is <strong>%HUMIDITY%<\/strong>, we return the humidity value.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>else if(var == \"HUMIDITY\"){\n  return String(h);\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">setup()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the Serial Monitor for debugging purposes.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>Initialize the DHT sensor.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>dht.begin();<\/code><\/pre>\n\n\n\n<p>Connect to your local network and print the ESP8266 IP address.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>WiFi.begin(ssid, password);\nwhile (WiFi.status() != WL_CONNECTED) {\n  delay(1000);\n  Serial.println(\"Connecting to WiFi..\");\n}<\/code><\/pre>\n\n\n\n<p>Finally, add the next lines of code to handle the web server.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request->send(200, \"text\/html\", index_html, processor);\n});\nserver.on(\"\/temperature\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request->send(200, \"text\/plain\", String(t).c_str());\n});\nserver.on(\"\/humidity\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request->send(200, \"text\/plain\", String(h).c_str());\n});<\/code><\/pre>\n\n\n\n<p>When we make a request on the root URL, we send the HTML text that is stored on the <span class=\"rnthl rntliteral\">index_html<\/span> variable. We also need to pass the <span class=\"rnthl rntliteral\">processor<\/span>function, that will replace all the placeholders with the right values.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request->send(200, \"text\/html\", index_html, processor);\n});<\/code><\/pre>\n\n\n\n<p>We need to add two additional handlers to update the temperature and humidity readings. When we receive a request on the <em>\/temperature<\/em> 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 <span class=\"rnthl rntliteral\">c_str()<\/span> method.<br><\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/temperature\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request->send(200, \"text\/plain\", String(t).c_str());\n});<\/code><\/pre>\n\n\n\n<p>The same process is repeated for the humidity.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.on(\"\/humidity\", HTTP_GET, &#091;](AsyncWebServerRequest *request){\n  request->send(200, \"text\/plain\", String(h).c_str());\n});<\/code><\/pre>\n\n\n\n<p>Lastly, we can start the server.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>server.begin();<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span> is where we get new temperature readings from the sensor every 10 seconds.<\/p>\n\n\n\n<p>Basically, we check if it is time to get new sensor readings:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (currentMillis - previousMillis &gt;= interval) {<\/code><\/pre>\n\n\n\n<p>If it is, we store a new temperature reading on the <span class=\"rnthl rntliteral\">newT<\/span> variable<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float newT = dht.readTemperature();<\/code><\/pre>\n\n\n\n<p>If the <span class=\"rnthl rntliteral\">newT<\/span> variable is a valid temperature readings, we update the <span class=\"rnthl rntliteral\">t<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>else {\n  t = newT;\n  Serial.println(t);\n}<\/code><\/pre>\n\n\n\n<p>The same process is repeated for the humidity.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Read Humidity\nfloat newH = dht.readHumidity();\n\/\/ if humidity read failed, don't change h value \nif (isnan(newH)) {\n   Serial.println(\"Failed to read from DHT sensor!\");\n}\nelse {\n  h = newH;\n  Serial.println(h);\n}<\/code><\/pre>\n\n\n\n<p>That\u2019s pretty much how the code works.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Uploading the code<\/h3>\n\n\n\n<p>After modifying the sketch with the necessary changes, if needed, upload the code to your ESP8266 (if you can\u2019t upload code to your ESP8266, <a rel=\"noreferrer noopener\" href=\"https:\/\/randomnerdtutorials.com\/esp8266-troubleshooting-guide\/\" target=\"_blank\">read this troubleshooting guide<\/a>). <\/p>\n\n\n\n<p>Make sure you have the right board and COM port select. Go to <strong>Tools&gt; Board <\/strong>and select the ESP8266 model you&#8217;re using. In our case, we&#8217;re using the ESP8266 12-E NodeMCU Kit.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"672\" height=\"596\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/select-esp8266-board-arduino-ide.png?resize=672%2C596&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-84953\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/select-esp8266-board-arduino-ide.png?w=672&amp;quality=100&amp;strip=all&amp;ssl=1 672w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/select-esp8266-board-arduino-ide.png?resize=300%2C266&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 672px) 100vw, 672px\" \/><\/figure><\/div>\n\n\n<p>Go to <strong>Tools <\/strong>&gt; <strong>Port <\/strong>and select the COM port the ESP8266 is connected to.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"548\" height=\"595\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/select-esp8266-port-arduino-ide.png?resize=548%2C595&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-84954\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/select-esp8266-port-arduino-ide.png?w=548&amp;quality=100&amp;strip=all&amp;ssl=1 548w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/select-esp8266-port-arduino-ide.png?resize=276%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 276w\" sizes=\"(max-width: 548px) 100vw, 548px\" \/><\/figure><\/div>\n\n\n<p>Press the Arduino IDE upload button. <\/p>\n\n\n\n<p class=\"rntbox rntclgreen\"><strong>Note: <\/strong> if you&#8217;re using an ESP-01, you need a <a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp8266-01-serial-adapter\/\" target=\"_blank\">serial adapter<\/a>&nbsp;or an&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/ftdi-programmer-board\/\" target=\"_blank\">FTDI programmer<\/a> to upload code. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ESP8266 IP Address<\/h3>\n\n\n\n<p>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.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"744\" height=\"331\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP8266-IP-Address.png?resize=744%2C331&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-84957\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP8266-IP-Address.png?w=744&amp;quality=100&amp;strip=all&amp;ssl=1 744w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP8266-IP-Address.png?resize=300%2C133&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 744px) 100vw, 744px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>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.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"547\" height=\"449\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP8266-DHT11-DHT22-Web-Server-Demonstration.png?resize=547%2C449&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-84956\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP8266-DHT11-DHT22-Web-Server-Demonstration.png?w=547&amp;quality=100&amp;strip=all&amp;ssl=1 547w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP8266-DHT11-DHT22-Web-Server-Demonstration.png?resize=300%2C246&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 547px) 100vw, 547px\" \/><\/figure><\/div>\n\n\n<p>The temperature and humidity readings update automatically every 10 seconds without the need to refresh the web page.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. ESP8266 Simple HTTP Web Server<\/h2>\n\n\n\n<p>In this section, we&#8217;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. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"791\" height=\"555\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/DSC08855.jpg?resize=791%2C555&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-23749\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/DSC08855.jpg?w=791&amp;quality=100&amp;strip=all&amp;ssl=1 791w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/DSC08855.jpg?resize=150%2C105&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/DSC08855.jpg?resize=300%2C210&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/DSC08855.jpg?resize=768%2C539&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/DSC08855.jpg?resize=80%2C56&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/DSC08855.jpg?resize=220%2C154&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/DSC08855.jpg?resize=143%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 143w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/DSC08855.jpg?resize=214%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 214w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/DSC08855.jpg?resize=339%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 339w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/DSC08855.jpg?resize=591%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 591w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/DSC08855.jpg?resize=694%2C487&amp;quality=100&amp;strip=all&amp;ssl=1 694w\" sizes=\"(max-width: 791px) 100vw, 791px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Video Demonstration<\/h3>\n\n\n\n<p>First, you can watch the ESP8266 web server project video demonstration below.<\/p>\n\n\n<p style=\"text-align:center\"><iframe width=\"720\" height=\"405\" src=\"https:\/\/www.youtube.com\/embed\/hXhYLd4O8cU?rel=0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code<\/h3>\n\n\n\n<p>We\u2019ll program the ESP8266 using Arduino IDE, so you must have the ESP8266 add-on installed in your Arduino IDE. If you haven&#8217;t follow the next tutorial first.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-esp8266-board-arduino-ide\/\">Install the ESP8266 Board in Arduino IDE<\/a><\/li>\n<\/ul>\n\n\n\n<p>Open your Arduino IDE and copy the following code.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at http:\/\/randomnerdtutorials.com  \n*********\/\n\n\/\/ Including the ESP8266 WiFi library\n#include &lt;ESP8266WiFi.h&gt;\n#include &quot;DHT.h&quot;\n\n\/\/ Uncomment one of the lines below for whatever DHT sensor type you're using!\n#define DHTTYPE DHT11   \/\/ DHT 11\n\/\/#define DHTTYPE DHT21   \/\/ DHT 21 (AM2301)\n\/\/#define DHTTYPE DHT22   \/\/ DHT 22  (AM2302), AM2321\n\n\/\/ Replace with your network details\nconst char* ssid = &quot;YOUR_NETWORK_NAME&quot;;\nconst char* password = &quot;YOUR_NETWORK_PASSWORD&quot;;\n\n\/\/ Web Server on port 80\nWiFiServer server(80);\n\n\/\/ DHT Sensor\nconst int DHTPin = 5;\n\/\/ Initialize DHT sensor.\nDHT dht(DHTPin, DHTTYPE);\n\n\/\/ Temporary variables\nstatic char celsiusTemp[7];\nstatic char fahrenheitTemp[7];\nstatic char humidityTemp[7];\n\n\/\/ only runs once on boot\nvoid setup() {\n  \/\/ Initializing serial port for debugging purposes\n  Serial.begin(115200);\n  delay(10);\n\n  dht.begin();\n  \n  \/\/ Connecting to WiFi network\n  Serial.println();\n  Serial.print(&quot;Connecting to &quot;);\n  Serial.println(ssid);\n  \n  WiFi.begin(ssid, password);\n  \n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(&quot;.&quot;);\n  }\n  Serial.println(&quot;&quot;);\n  Serial.println(&quot;WiFi connected&quot;);\n  \n  \/\/ Starting the web server\n  server.begin();\n  Serial.println(&quot;Web server running. Waiting for the ESP IP...&quot;);\n  delay(10000);\n  \n  \/\/ Printing the ESP IP address\n  Serial.println(WiFi.localIP());\n}\n\n\/\/ runs over and over again\nvoid loop() {\n  \/\/ Listenning for new clients\n  WiFiClient client = server.available();\n  \n  if (client) {\n    Serial.println(&quot;New client&quot;);\n    \/\/ bolean to locate when the http request ends\n    boolean blank_line = true;\n    while (client.connected()) {\n      if (client.available()) {\n        char c = client.read();\n        \n        if (c == '\\n' &amp;&amp; blank_line) {\n            \/\/ Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)\n            float h = dht.readHumidity();\n            \/\/ Read temperature as Celsius (the default)\n            float t = dht.readTemperature();\n            \/\/ Read temperature as Fahrenheit (isFahrenheit = true)\n            float f = dht.readTemperature(true);\n            \/\/ Check if any reads failed and exit early (to try again).\n            if (isnan(h) || isnan(t) || isnan(f)) {\n              Serial.println(&quot;Failed to read from DHT sensor!&quot;);\n              strcpy(celsiusTemp,&quot;Failed&quot;);\n              strcpy(fahrenheitTemp, &quot;Failed&quot;);\n              strcpy(humidityTemp, &quot;Failed&quot;);         \n            }\n            else{\n              \/\/ Computes temperature values in Celsius + Fahrenheit and Humidity\n              float hic = dht.computeHeatIndex(t, h, false);       \n              dtostrf(hic, 6, 2, celsiusTemp);             \n              float hif = dht.computeHeatIndex(f, h);\n              dtostrf(hif, 6, 2, fahrenheitTemp);         \n              dtostrf(h, 6, 2, humidityTemp);\n              \/\/ You can delete the following Serial.print's, it's just for debugging purposes\n              Serial.print(&quot;Humidity: &quot;);\n              Serial.print(h);\n              Serial.print(&quot; %\\t Temperature: &quot;);\n              Serial.print(t);\n              Serial.print(&quot; *C &quot;);\n              Serial.print(f);\n              Serial.print(&quot; *F\\t Heat index: &quot;);\n              Serial.print(hic);\n              Serial.print(&quot; *C &quot;);\n              Serial.print(hif);\n              Serial.print(&quot; *F&quot;);\n              Serial.print(&quot;Humidity: &quot;);\n              Serial.print(h);\n              Serial.print(&quot; %\\t Temperature: &quot;);\n              Serial.print(t);\n              Serial.print(&quot; *C &quot;);\n              Serial.print(f);\n              Serial.print(&quot; *F\\t Heat index: &quot;);\n              Serial.print(hic);\n              Serial.print(&quot; *C &quot;);\n              Serial.print(hif);\n              Serial.println(&quot; *F&quot;);\n            }\n            client.println(&quot;HTTP\/1.1 200 OK&quot;);\n            client.println(&quot;Content-Type: text\/html&quot;);\n            client.println(&quot;Connection: close&quot;);\n            client.println();\n            \/\/ your actual web page that displays temperature and humidity\n            client.println(&quot;&lt;!DOCTYPE HTML&gt;&quot;);\n            client.println(&quot;&lt;html&gt;&quot;);\n            client.println(&quot;&lt;head&gt;&lt;\/head&gt;&lt;body&gt;&lt;h1&gt;ESP8266 - Temperature and Humidity&lt;\/h1&gt;&lt;h3&gt;Temperature in Celsius: &quot;);\n            client.println(celsiusTemp);\n            client.println(&quot;*C&lt;\/h3&gt;&lt;h3&gt;Temperature in Fahrenheit: &quot;);\n            client.println(fahrenheitTemp);\n            client.println(&quot;*F&lt;\/h3&gt;&lt;h3&gt;Humidity: &quot;);\n            client.println(humidityTemp);\n            client.println(&quot;%&lt;\/h3&gt;&lt;h3&gt;&quot;);\n            client.println(&quot;&lt;\/body&gt;&lt;\/html&gt;&quot;);     \n            break;\n        }\n        if (c == '\\n') {\n          \/\/ when starts reading a new line\n          blank_line = true;\n        }\n        else if (c != '\\r') {\n          \/\/ when finds a character on the current line\n          blank_line = false;\n        }\n      }\n    }  \n    \/\/ closing the client connection\n    delay(1);\n    client.stop();\n    Serial.println(&quot;Client disconnected.&quot;);\n  }\n}   \n\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266_dht_arduino_ide.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>Insert your network credentials in the following variables and the code will work straight away.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* ssid = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>We&#8217;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: <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-web-server\/\">Build an ESP8266 Web Server<\/a>.<\/p>\n\n\n\n<p>In this section, we&#8217;ll just take a look at the relevant parts for this example.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Importing libraries<\/h4>\n\n\n\n<p>Import the DHT libraries to read from the <span class=\"rnthl rntliteral\">DHT<\/span> sensor and the <span class=\"rnthl rntliteral\">ESP8266WiFi<\/span> library to build the web server:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Adafruit_Sensor.h&gt;\n#include &lt;DHT.h&gt;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">DHT Sensor Type<\/h4>\n\n\n\n<p>Uncomment one of the following lines for the sensor type you&#8217;re using. If you&#8217;re using a DHT22 sensor, you don&#8217;t need to modify anything.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/#define DHTTYPE DHT11   \/\/ DHT 11\n\/\/#define DHTTYPE DHT21   \/\/ DHT 21 (AM2301)\n#define DHTTYPE DHT22     \/\/ DHT 22  (AM2302), AM2321<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Setting your network credentials<\/h4>\n\n\n\n<p>Insert your network credentials in the following variables, so that the ESP8266 can connect to your local network.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* ssid = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Initialize DHT Sensor<\/h4>\n\n\n\n<p>Define the GPIO that the DHT data pin is connected to. In this case, it\u2019s connected to <span class=\"rnthl rntcblue\">GPIO5<\/span> (D1).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define DHTPIN 5     \/\/ Digital pin connected to the DHT sensor<\/code><\/pre>\n\n\n\n<p>Instantiate a <span class=\"rnthl rntliteral\">DHT<\/span> object with the type and pin defined earlier.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>DHT dht(DHTPIN, DHTTYPE);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">setup()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the DHT sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>dht.begin();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">loop()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we check whether there&#8217;s an new client making a request:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (client) {\n    Serial.println(\"New client\");\n    \/\/ bolean to locate when the http request ends\n    boolean blank_line = true;\n    while (client.connected()) {\n      if (client.available()) {\n        char c = client.read();<\/code><\/pre>\n\n\n\n<p>When a new client makes a request, we read the humidity, temperature in Celsius and Fahrenheit, and save them in the <span class=\"rnthl rntliteral\">h<\/span>, <span class=\"rnthl rntliteral\">t<\/span> and <span class=\"rnthl rntliteral\">f<\/span> variables:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float h = dht.readHumidity();\n\/\/ Read temperature as Celsius (the default)\nfloat t = dht.readTemperature();\n\/\/ Read temperature as Fahrenheit (isFahrenheit = true)\nfloat f = dht.readTemperature(true);<\/code><\/pre>\n\n\n\n<p>Finally, you send the response to the client with the HTML text to build the page as well as the temperature and humidity:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>client.println(\"&lt;!DOCTYPE HTML&gt;\");\nclient.println(\"&lt;html&gt;\");\nclient.println(\"&lt;head&gt;&lt;\/head&gt;&lt;body&gt;&lt;h1&gt;ESP8266 - Temperature and Humidity&lt;\/h1&gt;&lt;h3&gt;Temperature in Celsius: \");\nclient.println(celsiusTemp);\nclient.println(\"*C&lt;\/h3&gt;&lt;h3&gt;Temperature in Fahrenheit: \");\nclient.println(fahrenheitTemp);\nclient.println(\"*F&lt;\/h3&gt;&lt;h3&gt;Humidity: \");\nclient.println(humidityTemp);\nclient.println(\"%&lt;\/h3&gt;&lt;h3&gt;\");\nclient.println(\"&lt;\/body&gt;&lt;\/html&gt;\");  <\/code><\/pre>\n\n\n\n<p>The temperature and humidity are sent to the client in these variables: <span class=\"rnthl rntliteral\">celsiusTemp<\/span>, <span class=\"rnthl rntliteral\">fahrenheitTemp<\/span> and <span class=\"rnthl rntliteral\">humidityTemp<\/span>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>After making any necessary changes, upload the code to the ESP8266 as described in an earlier section.<\/p>\n\n\n\n<p>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\u2019s <strong>192.168.1.95.<\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"856\" height=\"452\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/Arduino-IDE.png?resize=856%2C452&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino IDE\" class=\"wp-image-21659\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/Arduino-IDE.png?w=856&amp;quality=100&amp;strip=all&amp;ssl=1 856w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/Arduino-IDE.png?resize=150%2C79&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/Arduino-IDE.png?resize=300%2C158&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/Arduino-IDE.png?resize=768%2C406&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/Arduino-IDE.png?resize=80%2C42&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/Arduino-IDE.png?resize=220%2C116&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/Arduino-IDE.png?resize=189%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 189w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/Arduino-IDE.png?resize=280%2C148&amp;quality=100&amp;strip=all&amp;ssl=1 280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/Arduino-IDE.png?resize=451%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 451w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/Arduino-IDE.png?resize=750%2C396&amp;quality=100&amp;strip=all&amp;ssl=1 750w\" sizes=\"(max-width: 856px) 100vw, 856px\" \/><\/figure><\/div>\n\n\n<p>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!<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"668\" height=\"353\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/web-server.png?resize=668%2C353&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 Simple DHT Web Server\" class=\"wp-image-21660\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/web-server.png?w=668&amp;quality=100&amp;strip=all&amp;ssl=1 668w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/web-server.png?resize=150%2C79&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/web-server.png?resize=300%2C159&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/web-server.png?resize=80%2C42&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/web-server.png?resize=220%2C116&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/web-server.png?resize=189%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 189w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/web-server.png?resize=280%2C148&amp;quality=100&amp;strip=all&amp;ssl=1 280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/05\/web-server.png?resize=450%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 450w\" sizes=\"(max-width: 668px) 100vw, 668px\" \/><\/figure><\/div>\n\n\n<p>Now you should see the latest temperature and humidity readings. To update the readings, you just need to refresh the web page.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshooting<\/h2>\n\n\n\n<p>If your DHT sensor fails to get the readings, read our&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/solved-dht11-dht22-failed-to-read-from-dht-sensor\/\"><strong>DHT Troubleshooting Guide<\/strong><\/a>&nbsp;to help you fix the issue.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this project we&#8217;ve shown you how to display sensor readings from a DHT sensor on a web page. We&#8217;ve provided two examples: a simple web server and an asynchronous web server with auto-updates.<\/p>\n\n\n\n<p>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:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\">Home Automation with ESP8266 (Course)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE (Course)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming with ESP32 and ESP8266 (ebook)<\/a><\/li>\n<\/ul>\n\n\n\n<p>You may also like some of our most popular projects with the ESP8266:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/hack-pir-motion-sensor-esp8266-hlk-pm03\/\">Hack a PIR Motion Sensor with an ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-multisensor-shield\/\">Build a Multisensor Shield for ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-wi-fi-button-diy-amazon-dash-button-clone\/\">ESP8266 Wi-Fi Button \u2013 DIY Amazon Dash Button Clone<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp8266\/\">30+ ESP8266 Projects <\/a><\/li>\n<\/ul>\n\n\n\n<p>Do you have any questions? Leave a comment below!<\/p>\n\n\n\n<p>Thanks for reading. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this project you\u2019ll 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 &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP8266 DHT11\/DHT22 Temperature and Humidity Web Server with Arduino IDE\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp8266-dht11dht22-temperature-and-humidity-web-server-with-arduino-ide\/#more-21652\" aria-label=\"Read more about ESP8266 DHT11\/DHT22 Temperature and Humidity Web Server with Arduino IDE\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":84914,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[214,265,246,300,240,225,220,264,218],"tags":[],"class_list":["post-21652","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp8266","category-esp8266-project","category-esp8266-arduino-ide","category-0-esp8266","category-esp8266-projects","category-home-automation","category-http-client","category-project","category-web-server"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP8266-DHT11-DHT22-Web-Server.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/21652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=21652"}],"version-history":[{"count":3,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/21652\/revisions"}],"predecessor-version":[{"id":168942,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/21652\/revisions\/168942"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/84914"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=21652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=21652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=21652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}