beginnerTutorialSensorsESP32-S320 min

Put a DHT22 on the internet in 20 minutes

Wire a temperature and humidity sensor to an ESP32-S3, and watch the readings land on a live dashboard you can open from your phone. No backend, no server, no port forwarding.

Most "IoT sensor" tutorials stop at the serial monitor. You get a number scrolling past in a terminal window, and then you're on your own for the part you actually wanted — seeing that number from somewhere else.

This one goes all the way. By the end you'll have a DHT22 reporting temperature and humidity to a dashboard you can open on your phone, from anywhere, with no server to run and nothing to forward through your router.

What you're building

Three pieces, and only the middle one is yours to write:

  • A DHT22 measures temperature and humidity, and hands the numbers to the board over a single data wire.
  • An ESP32-S3 reads the sensor and publishes the values to the cloud over your WiFi.
  • Fluxgrid receives them and draws them — a gauge for the current reading, a chart for the history.

Wire it up

The DHT22 module has three pins. Two are power, one is data.

DHT22 moduleESP32-S3
+ or VCC3V3
- or GNDGND
out or DATAGPIO 4

The three-pin module already has the pull-up resistor on the board. If you bought the bare four-pin IC instead, you need a 10kΩ resistor between DATA and 3V3 or the readings will come back as nan.

Power it from 3V3, not 5V. The ESP32's GPIO pins are 3.3V, and feeding a 5V-powered sensor's data line into them is how boards die.

Install the two libraries

In the Arduino IDE, open Sketch → Include Library → Manage Libraries, then install:

  • Fluxgrid — the cloud connection
  • DHT sensor library by Adafruit — reading the sensor (it will offer to install Adafruit Unified Sensor alongside it; say yes)

Create the dashboard first

The sketch needs to know where to send each number, so make the destinations before you write it.

  1. Open Fluxgrid and create a dashboard.
  2. Drag a Gauge onto the canvas. It creates a Number datastream — rename its handle to temp.
  3. Drag a second Gauge, and set its handle to humidity.
  4. Add the device, and copy the device token it gives you.

Those two handles — temp and humidity — are the whole contract between the sketch and the dashboard. Get them matching and everything else follows.

The sketch

#define WIFI_SSID  "your-wifi"
#define WIFI_PASS  "your-password"
#define FG_TOKEN   "paste-device-token"
#include <Fluxgrid.h>          // ← credentials must be #defined ABOVE this line

#include <DHT.h>

#define DHT_PIN   4
#define DHT_TYPE  DHT22

DHT dht(DHT_PIN, DHT_TYPE);

unsigned long lastRead = 0;

void setup() {
  Serial.begin(115200);
  dht.begin();
  Fluxgrid.begin();            // server, port and TLS are built in
}

void loop() {
  Fluxgrid.run();              // keep the connection alive

  // The DHT22 samples every 2 seconds. Reading it faster just returns
  // the previous value, so there is nothing to gain by hurrying it.
  if (millis() - lastRead < 2000) return;
  lastRead = millis();

  float t = dht.readTemperature();
  float h = dht.readHumidity();

  // A failed read returns nan. Publishing that would draw a hole in the
  // chart, so skip the cycle instead.
  if (isnan(t) || isnan(h)) {
    Serial.println("DHT read failed");
    return;
  }

  Fluxgrid.write("temp", t);
  Fluxgrid.write("humidity", h);

  Serial.printf("%.1f C  %.1f %%\n", t, h);
}

Three things worth pointing out, because they're the ones people get wrong:

The #defines go above the #include. The Fluxgrid header reads those three names while it's being compiled. Put them underneath and you'll get a compile error that doesn't obviously say so.

Fluxgrid.run() goes at the top of loop(), before the early return. If it sits below the return, it only runs every 2 seconds — and the connection needs servicing far more often than that.

write() paces itself. You don't need a timer around it. The 2-second gate here is for the sensor, which physically can't sample faster.

Watch it arrive

Flash the board and open the serial monitor at 115200. You should see the readings printed every two seconds — and the same numbers appearing on the two gauges in Fluxgrid within a second of each other.

Now add a Chart widget and bind it to temp. It backfills from the history already stored, so the line appears immediately rather than starting from scratch.

When it doesn't work

Gauges say "waiting" and never move. The handles in the sketch don't match the handles on the widgets. They're case-sensitive.

Serial prints DHT read failed every time. Data pin isn't on GPIO 4, or the sensor is on 5V, or you have the bare IC without a pull-up resistor.

Serial prints good numbers but nothing reaches the dashboard. The board is reading the sensor but not connecting — check the WiFi credentials, and note that the ESP32 only joins 2.4 GHz networks. A 5 GHz-only SSID will fail silently.

Readings are 2–3 °C high. The board warms the air around it. Move the sensor a few centimetres away from the ESP32 on longer jumper wires.

Where to take it next

The sketch publishes two numbers. Everything else is a dashboard decision, not a firmware one — you can add a Chart, an Alarm on the temperature, or a published view you send to someone, without touching the code again.

The natural next step is a second sensor on the same board. Add its reading, give it a third handle, and both arrive over the one connection.

Edit this page — content/tutorials/dht22-esp32-fluxgrid-dashboard.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

This page covers several products. Choose yours to see the right discussions.

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Projects and guides on the forum