Environmental Monitoring with DHT22, ESP32 and InfluxDB

by Müller | Nov 15, 2025 | Hardware & IoT | 0 comments

The goal here is to build a system that collects temperature and humidity data using the DHT22 sensor and sends it to an InfluxDB server for storage and analysis, together with a Grafana server. The ESP32 acts as the bridge, connecting the sensor to Wi-Fi and managing communication with InfluxDB.

Required Hardware

  • ESP32 — Microcontroller with built-in Wi-Fi and Bluetooth.
  • DHT22 — Low-cost temperature and humidity sensor.
  • Wi-Fi connection — Required for sending the data.
20240505 190842 1 1024x768.jpg

Pinout

DHT22 sensor:

  • VCC: 3.3V from the ESP32
  • GND: ESP32 GND
  • DHTPIN: GPIO configured in code (set to 18)

Project Setup in VSCode with PlatformIO

[env:upesy_wroom]
platform = espressif32
board = upesy_wroom
framework = arduino
lib_deps = 
    adafruit/DHT sensor library@^1.4.6
    arduino-libraries/NTPClient@3.1.0
    tobiasschuerg/ESP8266 Influxdb@^3.13.1

Source Code Analysis

DHT22 Declaration

#include <Adafruit_Sensor.h>
#include <DHT.h>

#define DHTPIN 18
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

Wi-Fi Connection

#include <WiFi.h>
#include <WiFiUdp.h>

const char* ssid = "MY_WIFI";
const char* password = "MY_PASSWORD";

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
  delay(2500);
  Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");

InfluxDB Declaration and Initialization

#include <InfluxDbClient.h>

#define INFLUXDB_URL "http://influx.mfs.eng.br:8086"
#define INFLUXDB_DB_NAME "clima"
#define INFLUXDB_USER "esp32"
#define INFLUXDB_PASSWORD "esp32_password"

InfluxDBClient client(INFLUXDB_URL, INFLUXDB_DB_NAME);
Point sensor("clima");

client.setConnectionParamsV1(INFLUXDB_URL, INFLUXDB_DB_NAME,
                             INFLUXDB_USER, INFLUXDB_PASSWORD);

Collecting Data from the DHT22

float tempValue = dht.readTemperature();
float humidityValue = dht.readHumidity();

if (!isnan(tempValue) && !isnan(humidityValue)) {
  Serial.print("Temperature: ");
  Serial.println(tempValue);
  Serial.print("Humidity: ");
  Serial.println(humidityValue);
}

Sending Data to InfluxDB

sensor.clearFields();
sensor.addField("temperature", tempValue);
sensor.addField("humidity", humidityValue);

if (!client.writePoint(sensor)) {
  Serial.print("InfluxDB write failed: ");
  Serial.println(client.getLastErrorMessage());
}

InfluxDB Advantages

  • Time-series database — ideal for continuous data.
  • Scalability — handles large data volumes well.
  • Seamless Grafana integration.

Visualization in Grafana

Example dashboard:
Link to the Grafana dashboard

Image 1 1024x600.png

Complete Code

Project repository:
https://gitlab.com/mfs.eng.br/hardware-quirks/temperatura_esp32

#include <Arduino.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <InfluxDbClient.h>
#include "config.h"

// Full code omitted here — included in the repository

References

Code Repository

Table of Contents