Project Overview

In this tutorial, you'll build a wireless environmental monitor using the ESP32 and a DHT22 temperature/humidity sensor. The ESP32 will host a small web server, so you can view live readings from any device on your local network — no app required, just a browser.

What You'll Need

  • ESP32 development board (any DevKit variant)
  • DHT22 (AM2302) temperature and humidity sensor
  • 10kΩ resistor (pull-up for data line)
  • Breadboard and jumper wires
  • Arduino IDE with ESP32 board package installed
  • DHT sensor library by Adafruit (install via Library Manager)

Wiring the DHT22 to the ESP32

The DHT22 has three active pins (some modules have 4 pins with one unused):

DHT22 PinESP32 PinNotes
VCC3.3VUse 3.3V — DHT22 is 3.3/5V compatible
DATAGPIO 4Add 10kΩ pull-up to 3.3V
GNDGND

The Code

Install the Adafruit DHT Unified and Adafruit Unified Sensor libraries before uploading. Here's the full sketch:

#include <WiFi.h>
#include <DHT.h>

#define DHTPIN 4
#define DHTTYPE DHT22

const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

DHT dht(DHTPIN, DHTTYPE);
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  dht.begin();
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) delay(500);
  Serial.println(WiFi.localIP());
  server.begin();
}

void loop() {
  WiFiClient client = server.available();
  if (!client) return;
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  String html = "<html><body><h1>ESP32 Sensor</h1>";
  html += "<p>Temperature: " + String(t) + " C</p>";
  html += "<p>Humidity: " + String(h) + " %</p>";
  html += "</body></html>";
  client.println("HTTP/1.1 200 OK");
  client.println("Content-type:text/html");
  client.println();
  client.print(html);
  client.stop();
}

Accessing Your Monitor

  1. Open the Serial Monitor at 115200 baud after uploading.
  2. Wait for the ESP32 to connect to Wi-Fi — it will print its IP address.
  3. Open a browser on any device on the same network and navigate to that IP address.
  4. You'll see the current temperature and humidity readings.

Going Further

Once the basics are working, consider these upgrades:

  • Auto-refresh the page using a <meta http-equiv="refresh"> tag
  • Log readings to Google Sheets or an MQTT broker
  • Add a small OLED display for local readout without Wi-Fi
  • Use deep sleep between readings to run the project on battery power

This project is a perfect foundation for building more complex environmental monitoring systems, home automation dashboards, and data logging applications.