🔌

ESP32 Sensor Node

An ESP32-based IoT sensor node that measures temperature and humidity, publishes data via MQTT, receives LED control commands, and supports OTA firmware updates over WiFi.

ESP32 C++ / Arduino MQTT DHT22 OTA Update IoT
ESP32 · MQTT · DHT22
Photos
Hardware photos and setup
Hardware Overview
Hardware Overview
ESP32 Board
ESP32 Board
DHT22 Sensor
DHT22 Sensor
Wiring Setup
Wiring Setup
LED Circuit
LED Circuit
Assembled Unit
Assembled Unit
Features
What the sensor node does
🌡️
Temperature & Humidity
Reads DHT22 sensor every 5 seconds and publishes JSON payload to MQTT topic iot/devices/{id}/telemetry.
📡
MQTT Publishing
Connects to Mosquitto broker and publishes telemetry with device_id, value, unit, type, and timestamp fields.
💡
LED Control
Subscribes to iot/devices/{id}/command. On receiving {command:led, state:on/off} it controls the onboard LED.
🔄
OTA Firmware Update
Supports over-the-air firmware updates via Arduino OTA. No USB cable needed to update firmware in the field.
📶
WiFi Auto-reconnect
Automatically reconnects to WiFi and MQTT broker if connection is lost. Retries every 5 seconds.
🔁
Status Feedback
Publishes LED state and device status back to iot/devices/{id}/status so the dashboard stays in sync.
MQTT Topics
Topics the device publishes and subscribes to
DirectionTopicPayload Example
📤 Publish iot/devices/DEV-001/telemetry {"device_id":"DEV-001","value":28.5,"unit":"°C","type":"Temperature","timestamp":1234567890}
📤 Publish iot/devices/DEV-001/status {"device_id":"DEV-001","led":"on","status":"online"}
📥 Subscribe iot/devices/DEV-001/command {"command":"led","state":"on","timestamp":1234567890}
Hardware Components
Parts used in this project
 
ESP32 DevKit v1
Main microcontroller with built-in WiFi and Bluetooth
 
DHT22 Sensor
Temperature and humidity sensor, ±0.5°C accuracy
 
LED (5mm)
Status indicator LED controlled via MQTT command
 
220Ω Resistor
Current limiting resistor for LED
 
Breadboard / PCB
Prototyping board for component connections
 
USB Power / 5V
Powered via USB or 5V DC power supply
Firmware Snippet
Core MQTT publish loop
// Publish telemetry every 5 seconds
void loop() {
  if (!mqttClient.connected()) reconnect();
  mqttClient.loop();

  if (millis() - lastPublish > 5000) {
    float temp = dht.readTemperature();
    float hum  = dht.readHumidity();

    String payload = "{";
    payload += "\"device_id\":\"DEV-001\",";
    payload += "\"value\":"  + String(temp) + ",";
    payload += "\"unit\":\"°C\",";
    payload += "\"type\":\"Temperature\",";
    payload += "\"timestamp\":" + String(millis());
    payload += "}";

    mqttClient.publish("iot/devices/DEV-001/telemetry",
                       payload.c_str());
    lastPublish = millis();
  }
}