Designing and building the web page
Last updated
Last updated
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.
The <link> tag is needed to load the icons from the fontawesome website.
Styles
Between the <style></style> tags, we add some CSS to style the web page.
Basically, we’re setting the HTML page to display the text with Arial font in block without margin, and aligned at the center.
We set the font size for the heading (h2), paragraph (p) and the units(.units) of the readings.
The labels for the readings are styled as shown below:
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.
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:
And the paragraph for the humidity is on the following snipet:
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.
To chose the color, you just need to pass the style parameter with the color in hexadecimal, as follows:
Proceeding with the HTML text…
The next line writes the word “Temperature” into the web page.
The TEMPERATURE text between % signs is a placeholder for the temperature value.
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.
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.
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.
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.
When it receives that value, it updates the HTML element whose id is temperature.
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.
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 the placeholder is %HUMIDITY%, we return the humidity value.
setup()
In the setup(), initialize the Serial Monitor for debugging purposes.
Initialize the DHT sensor.
Connect to your local network and print the ESP8266 IP address.
Finally, add the next lines of code to handle the web server.
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.
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.
The same process is repeated for the humidity.
Lastly, we can start the server.
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 it is, we store a new temperature reading on the newT variable
If the newT variable is a valid temperature readings, we update the t variable.
The same process is repeated for the humidity.
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.
Now click the Arduino IDE upload button.
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.
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.