Skip to content

lab: iot

Virtual ESP32 sensor board

Wire a DHT22 temperature sensor and an LED to an ESP32, power it on, and watch the MQTT messages fly. Then cut the Wi-Fi and see why edge computing matters.

ESP32 DevKitoff
3V3free
GNDfree
GPIO2free
GPIO4free
GPIO5free
GPIO34free
DHT22

—

LED off

Wiring

Board is off. Wire it up, then press Power on.

Who decides the LED?
Temperature, last 60 sdashed: LED threshold
Dashboard:

MQTT broker

  1. Messages appear here once the board is on.
The firmware this board is running (Arduino C++)
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>

DHT dht(4, DHT22);        // DATA on GPIO4
const int LED = 2;        // LED on GPIO2
const float LIMIT = 32;  // °C
const bool EDGE = true;  // decide on the board?

void loop() {
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  if (isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; }

  if (EDGE) digitalWrite(LED, t > LIMIT);   // works without Wi-Fi

  char msg[64];
  snprintf(msg, sizeof msg, "{\"temp\":%.1f,\"hum\":%.1f}", t, h);
  mqtt.publish("shubh/esp32/telemetry", msg);
  mqtt.loop();                              // cloud mode: "shubh/esp32/led/set" callback sets the LED
  delay(1000);
}