# Designing and building the web page

![](https://3073865318-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ly3RLHyAwTi7Hml5lZN%2F-LyJkg2hLiocWr0Q0X16%2F-LyJkoIXrqLL7ha8LlFd%2Fweb-server-labeled.png?alt=media\&token=fc639a90-3046-4e64-baf3-75c762f8dc77)

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

**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](https://fontawesome.com/icons?d=gallery).\\

![FontAwesome Home Page (randomnerdtutorial.com)](https://3073865318-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ly3RLHyAwTi7Hml5lZN%2F-LyJHnTHd3eKSVU7Gj6A%2F-LyJHyMVDB0ke5oQ_rBi%2Ffontawesomepage.png?alt=media\&token=5ef716aa-5d55-4869-9254-80e6ea998b60)

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

![](https://3073865318-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ly3RLHyAwTi7Hml5lZN%2F-LyJI8J_MELbsF9FwWe0%2F-LyJILRsjhmuUu8Lhdgf%2Ffontawesome-thermometer.png?alt=media\&token=49610a55-5a7c-45a5-86ea-57ffc9a57ff9)

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

![(randomnerdtutorial.com)](https://3073865318-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ly3RLHyAwTi7Hml5lZN%2F-LyJpkQC0SlftRF7guWi%2F-LyJq3RP1ioZEXuLjrEY%2Ffontawesome-thermometer-html%20\(1\).png?alt=media\&token=4d06c0f3-7132-4955-9a87-2ff43ebdfe70)

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.

**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);
}
```

**Uploading the code**

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.

![Selecting an ESP8266 board (randomnerdtutorial.com)](https://3073865318-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ly3RLHyAwTi7Hml5lZN%2F-LyJrRCIPhFcUecav4RR%2F-LyJr_X_xrjhEKp6xOwW%2Fselect-esp8266-board-arduino-ide.png?alt=media\&token=e3b27fdb-3d15-45aa-a2e3-a14e74ada92c)

![Selecting the ESP8266 port (randomnerdtutorial.com)](https://3073865318-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ly3RLHyAwTi7Hml5lZN%2F-LyJsw7_l3ywml74Sz5_%2F-LyJteBLDW4Jlrdc3JJZ%2Fselect-esp8266-port-arduino-ide.png?alt=media\&token=bee4f4bc-3efb-44de-acc7-211fec619dbc)

Now click the Arduino IDE upload button.

#### 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.

![The IP address for the NodeMCU ](https://3073865318-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ly3RLHyAwTi7Hml5lZN%2F-LyJuLT_DPFFq9miNQ2z%2F-LyJuS9Ifcl4onhOKd8Q%2FESP8266-IP-Address.png?alt=media\&token=40b6aa9a-9a0f-4cd7-bc1c-fd5b008033e6)

#### 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.\\

![](https://3073865318-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Ly3RLHyAwTi7Hml5lZN%2F-LyJv0nap90e__iFofyF%2F-LyJv8h8zyskKsaKRlI2%2FESP8266-DHT11-DHT22-Web-Server-Demo.png?alt=media\&token=56250f02-e4c0-4108-9c08-b663eaa2659f)
