Introduction

In this ESP32 tutorial we will check how to develop a HTTP web server that will expose an API for clients to retrieve measurements of temperaturehumidity and CO2. These measurements will be gathered from the surrounding environment using two distinct sensors.

Regarding the web server, we will use the async HTTP web server library that can be found here. As we have been covering in previous tutorials, this library allows us to set up an asynchronous HTTP web server, which means we don’t need to be periodically polling some object to handle incoming clients, like we had to in the original ESP8266 web server implementation.

This library is very versatile and offers a lot of functionalities. Some of them have been covered in previous posts and you can check a list of them in the “Related Posts” section at the end of this article.

If you haven’t yet installed and tested the library, then my recommendation is to check this introductory tutorial, which explains how to install the library and its dependencies and how to get started.

As CO2 sensor, we will use an Analog Infrared CO2 Sensor from DFRobot, which we have been covering in the previous tutorial. This sensor is based on the NDIRtechnology and you can consult its Wiki here.

As detailed in the previous post, the sensor outputs an analog voltage that is proportional to the concentration of CO2 in the air, in the range of 0.4 V to 2.0 V. These voltages correspond to a concentration of 0 ppm (parts per million) and 5000 ppm, respectively.

Note that to obtain the analog voltage outputted by the sensor, we will need to use the ESP32 Analog to Digital Converter (ADC). As discussed in this open issue of the Arduino core, the analogRead function we will use is returning inconsistent values. This is caused by the non linearity of the ADC, which is currently not being compensated in the analogRead function.

Nonetheless, for the sake of simplicity, we will assume a linear behavior of the ADC in the 0 V to 3.3 V range, like we did in the previous tutorial about interacting with the CO2 sensor. Naturally, this will introduce some imprecision in the CO2 measurements, which means that this code should not be used if you need very accurate measurements.

Finally, the temperature and humidity measurements will be obtained using a DFRobot DHT22 module. The DHT22 is a sensor that allows to get both humidity and temperature measurements from the surrounding environment and has a single wire interface to exchange data with a microcontroller.

In order to interact with the sensor using a higher level API that abstracts from us the lower level details of the single wire interface of the sensor, we will use this Arduino library, which is compatible with the ESP32. Note that you can install it from the Arduino IDE libraries manager.

For a detailed tutorial on how to get temperature measurements from the sensor, please consult this post. For humidity measurements, check this one.

The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.

The electronic schematic

The electronic schematic needed to connect the ESP32 to the sensors is very simple since each of them only need to be connected to a single pin of the microcontroller. Figure 1 illustrates the diagram.

ESP32 CO2 DHT22 sensors connection

Figure 1 – Electronic schematic.

As mentioned before, the CO2 sensor needs to be connected to an analog pin of the ESP32, since it outputs an analog voltage. On the other hand, the DHT22 sensor has a digital interface, so we can connect it to any digital pin of the ESP32.

The pins from each sensor to be connected to the ESP32 are generically labeled as data in the previous diagram, but in the physical devices those pins don’t have any labels. So we need to take into account the colors of the wires attached...

Read more »