Illustration: an ESP32 and a DHT22 sensor by a rainy window, and a phone in hand showing temperature and humidity gauges.Home and IoT
Illustration (made with AI): roughly what the finished project looks like.

A Weather Station on Your Phone

An ESP32 measures the room's temperature and humidity and serves a small web page you open in your phone's browser on the home Wi-Fi.

Difficulty
Intermediate
Time
1 h 30 min
Age
12+
Parts
4, 4 on the site

Written by the RoboHub team. A question? Write to us.

Variations

Choose a variation and the parts list updates.

Instructions

  1. Connect the sensor

    Power the DHT22 from the 3V3 pin, connect its “−” to GND and its data pin to GPIO 4. You need a 10 kΩ resistor between data and power (modules often have one already).

    If your DHT22 sits on a small board (a module), it already has the 10 kΩ resistor and you don't need another. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • DHT22 sensor + → ESP32 3V3
    • DHT22 sensor DATA → ESP32 GPIO4
    • DHT22 sensor − → ESP32 GND
    • 10 kΩ resistor 1 → ESP32 GPIO4
    • 10 kΩ resistor 2 → ESP32 3V3
  2. Libraries

    Install the ESP32 boards package (esp32 by Espressif) and the “DHT sensor library” in the Arduino IDE.

  3. Wi-Fi and the web page

    Choose “ESP32 Dev Module” as the board. Put your network name and password in the code and upload it. On start the ESP32 prints its IP address to the Serial Monitor (115200). This code is for the ESP32: an Uno R4 WiFi needs a different Wi-Fi library (WiFiS3), with the sensor on 5V and a digital pin.

    Arduino C++
    // ESP32 weather station: a DHT22 on GPIO 4 and a small web page
    // that shows the temperature and humidity and refreshes itself every 10 seconds.
    #include <WiFi.h>
    #include <WebServer.h>
    #include <DHT.h>
    
    const char* WIFI_NAME = "your-network";       // your Wi-Fi network's name
    const char* WIFI_PASSWORD = "your-password";  // and its password
    
    const int DHT_PIN = 4;   // the DHT22's data pin
    DHT dht(DHT_PIN, DHT22);
    WebServer server(80);
    
    float temperature = NAN;   // the last good reading, in °C
    float humidity = NAN;      // the last good reading, in %
    unsigned long lastRead = 0;
    
    // Keeps the old values when a reading fails (the DHT22 misses one now and then)
    void readSensor() {
      float t = dht.readTemperature();
      float h = dht.readHumidity();
      if (!isnan(t) && !isnan(h)) {
        temperature = t;
        humidity = h;
      }
    }
    
    void handleRoot() {
      String html = "<!DOCTYPE html><html><head><meta charset='utf-8'>";
      html += "<meta name='viewport' content='width=device-width,initial-scale=1'>";
      html += "<meta http-equiv='refresh' content='10'>";   // the phone reloads the page every 10 s
      html += "<title>Weather station</title>";
      html += "<style>body{font-family:sans-serif;text-align:center;margin-top:15vh}p{font-size:2.5em;margin:0.3em}</style>";
      html += "</head><body>";
      if (isnan(temperature)) {
        html += "<p>No reading yet</p>";
      } else {
        html += "<p>";
        html += String(temperature, 1);
        html += " &deg;C</p><p>";
        html += String(humidity, 0);
        html += " %</p>";
      }
      html += "</body></html>";
      server.send(200, "text/html", html);
    }
    
    void setup() {
      Serial.begin(115200);
      dht.begin();
    
      WiFi.mode(WIFI_STA);
      WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
      Serial.print("Connecting to Wi-Fi");
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
      Serial.println();
      Serial.print("Open this address on your phone: http://");
      Serial.println(WiFi.localIP());
    
      server.on("/", handleRoot);
      server.begin();
      readSensor();
    }
    
    void loop() {
      server.handleClient();
      if (millis() - lastRead >= 2000) {   // the DHT22 needs 2 seconds between readings
        lastRead = millis();
        readSensor();
      }
    }
  4. Open it on your phone

    Type that address into the browser of a phone on the same Wi-Fi. The readings refresh every 10 seconds.

    Illustration: an ESP32 and a DHT22 sensor on a breadboard, beside a phone showing temperature and humidity gauges.

    Illustration, made with AI

  • Home and IoT
    Beginner40 min10+

    A Room Thermometer on an LCD

    A DHT11 sensor measures the temperature and humidity, and a 16×2 display shows both, updating every two seconds.

    5/5 parts on the site
  • Home and IoT
    Intermediate1 h12+

    A Code Lock with a Keypad

    Type the secret code and press #: with the right code the green light comes on and a servo slides the bolt open, with the wrong one the buzzer beeps three times.

    7/7 parts on the site
  • Sensors
    Beginner1 h10+

    Parking Sensor with Sound and Lights

    An ultrasonic sensor measures the distance. Three LEDs show how close the obstacle is, and the buzzer beeps faster the closer you get.

    6/6 parts on the site