This tutorial demonstrates how to build a simple sensor and publish its values online (on a web server) using an ESP32 module. We will build an Arduino sketch which will read the sensor values, connect to WiFi, connect to a website and publish the values on the website. The published values can then be used to view them online (view them on a web page) or in an App (this is a separate tutorial).
This tutorial assumes you already have basic knowledge on Arduino and building simple circuits.
Build circuit: connect DHT11 temperature sensor to ESP32 module
- ESP32 Module, in this case a Lolin32 Lite
- DHT11 temperature & humidity sensor
- Breadboard power module
- 10K resistor
- Breadboard wires
- Connect 3.3V of ESP module to 3.3V power line of breadboard (red line)
- Connect GND of ESP module to GND line of breadboard (blue line)
- The temperature sensor’s Vcc and GND pins are connected to the 5V + GND power lines on the breadboard.
- The data pin of the temperature sensor is connected to pin 5 of the ESP module
- There is a 10K resistor between the data pin of the temperature sensor and the 5V power line.
Make ESP module ready for use in Arduino IDE
Start the Arduino IDE. Choose File > Preferences. At “Additional Boards Managers URLs” enter
"https://dl.espressif.com/dl/package_esp32_index.json" (without the quotes).
Now choose Tools > Boards > Boards Manager. Enter “esp” in the search field.
Click on “esp32 by Espressif Systems” and click the Install button.
Install ESP version of DHT library
To be able to use the DHT sensor, we must install a library. For this example we use an ESP-version of the DHT library “DHTesp”. You can add it into the Arduino IDE via Sketch > Include Library > Manage Libraries, search for “DHT”, pick “DHT sensor library for ESPx”.
Or do a manual install: goto github.com/beegee-tokyo/DHTesp to download this library (click the “Clone or download” button and choose “Download ZIP”). Extract the zip-file to
Documents\Arduino\libraries and rename the folder to “DHTesp”.
First test: check if DHT sensor works
Connect a mini USB cable to the power module on the breadboard and connect it to your computer.
Connect a micro USB cable to the ESP module and connect it to your computer.
Download the first example
"DHTesp_basic_example.ino" and open it in the Arduino IDE.
Set the Board to “WEMOS LOLIN32” and Upload Speed to 115200:
Now, Upload it and start the Serial Monitor when it is uploaded. Make sure the speed of the Serial Monitor is set to 115200. If it works, the temperature and humidity are shown:
Complete code: use example code which publishes the values on the web server
Download the complete example
"esp32_web_client_dht11_v2.ino" (and study it). It uses my web server home.et.utwente.nl to store values.
Find the lines with the settings for the WiFi connection (variables
password) and adjust them to reflect your WiFi settings. If you attend one of my workshops, the preset values might work.
If you want to upload values to a different web server, adjust the variable
test_root_ca (this contains the certificate, more on this below). And check the request settings in the function
doRequest(). The request is made like this:
GET /val/variable/value where variable is the name of the variable to use for the value.
Something you must change are the variable names in the function call of function
doRequest(). These must become unique names for your use case, as otherwise your variables will be overwritten by values of others. So find this in the code:
And change the
"humidity" (red underlined) to something unique, eg.
Now, Upload it and start the Serial Monitor when it is uploaded.
If everything works, you will see no errors and output will be similar to:
Also, if you goto the URL you can find the values online. If you choose
"temp953276" the latest value can be found here:
- “Getting started with the ESP32 Wifi module”, includes info on how to connect it to the Blynk App
- Sparkfun’s “ESP32 Thing Hookup Guide”
- If you do not want to use my webservice but for example the Blynk cloud service, check out this post. You can also connect to the ThingSpeak service to log and analyse data from sensors.
To get the proper certificate code, I use the command below on my web server (shell-login via ssh/putty):
openssl s_client -showcerts -connect home.et.utwente.nl:443 > cert.output
In the example above, the certificate will be written to the file “cert.output”. If there are multiple certificates, get the one labelled “Root CA”.