Key Takeaways
- ESP32 is the ideal smart home controller – built-in Wi-Fi + Bluetooth, 34 GPIO pins, dual-core processor, under Rs 400
- MQTT protocol enables reliable decoupled communication between sensors, relays, and dashboards
- 3-room setup costs Rs 2,800-7,500 depending on whether you use a Raspberry Pi hub or cloud MQTT
- Home Assistant integration adds voice control, automation rules, and mobile push notifications
- AUTO/MANUAL modes let the system handle routine tasks while you retain manual override
Why ESP32 for Smart Home Automation?
The ESP32 is the go-to microcontroller for DIY home automation in 2026. It integrates both 2.4 GHz Wi-Fi and Bluetooth 5 on a single chip, eliminating the need for separate wireless modules. With 34 programmable GPIOs, dual-core Xtensa LX6 processors at up to 240 MHz, and hardware support for SPI, I2C, UART, PWM, and ADC, the ESP32 can manage multiple sensors and actuators simultaneously. At under Rs 400 for most variants, it is dramatically more cost-effective than platforms like Raspberry Pi for embedded control tasks.
System Architecture Overview
A practical ESP32 smart home system divides into three layers:
- Edge Layer: ESP32 nodes placed in each room, each controlling relays and reading local sensors. A single ESP32 can handle 2-4 rooms with multiple relays; one per room is cleaner and more fault-tolerant.
- Communication Layer: MQTT broker (Mosquitto on a Raspberry Pi, or a cloud service like HiveMQ) that routes messages between nodes and the dashboard.
- Application Layer: A web dashboard (Node-RED or custom HTML served by the ESP32) or Home Assistant for a polished UI and automation rules.
Components You Need
Per room/node:
- 1x ESP32 development board (ESP32-C6 Mini or standard ESP32) – Rs 380
- 1x 4-channel relay module (5V, optocoupler-isolated) – Rs 150
- 1x DHT11 or DHT22 temperature and humidity sensor – Rs 80-150
- 1x HC-SR501 PIR motion sensor – Rs 60
- 1x 5V/2A USB power supply – Rs 120
- Jumper wires, terminal blocks, enclosure – Rs 200
For the hub (optional): Raspberry Pi 4 (2GB) running Mosquitto MQTT broker and Home Assistant OS – Rs 4,500
Total Estimated Cost: 3-Room Setup
- With Raspberry Pi hub: Rs 7,470
- Without hub (cloud MQTT): Rs 2,800
Wiring the System
Relay Module Connection
Relays allow the 3.3V ESP32 GPIO to safely switch 230V AC loads. Connect the relay IN pins to ESP32 GPIOs (e.g., GPIO 26, 27, 14, 12 for four channels), VCC to 5V, and GND to GND. Wire the 230V load through the relay NO (normally open) and COM terminals. Always use optocoupler-isolated relay modules to protect the ESP32 from voltage spikes, and proper insulated wiring for the mains side.
DHT11/DHT22 Temperature and Humidity Sensor
The DHT11 provides temperature (0-50 C, +/-2 C) and humidity (20-80% RH, +/-5%) readings over a single-wire protocol. For better accuracy, upgrade to DHT22 (-40 to 80 C, +/-0.5 C). Wire DATA to GPIO 4, VCC to 3.3V, and GND to GND with a 10k ohm pull-up resistor on the data line.
PIR Motion Sensor
The HC-SR501 PIR sensor detects human movement using infrared radiation changes. Wire OUT to an ESP32 GPIO, VCC to 5V, and GND to GND. Install at 2-2.5 meters height in room corners for optimal coverage. The two onboard potentiometers adjust sensitivity (up to 7 meters) and time delay.
Complete Arduino Code
Here is a full working sketch for an ESP32 smart home node with DHT22, PIR motion, relay control, OLED display, and a web server:
#include <WiFi.h>
#include <WebServer.h>
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define DHT_PIN 15
#define PIR_PIN 14
#define LIGHT_PIN 26
#define FAN_PIN 27
#define BUZZER_PIN 25
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);
Adafruit_SSD1306 display(128, 64, &Wire, -1);
WebServer server(80);
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
float temperature = 0;
float humidity = 0;
bool lightStatus = false;
bool fanStatus = false;
bool motionDetected = false;
bool autoMode = true;
void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT);
pinMode(LIGHT_PIN, OUTPUT);
pinMode(FAN_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
dht.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.println("Smart Home");
display.display();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
server.on("/", handleRoot);
server.on("/light/on", [](){ lightStatus=true; digitalWrite(LIGHT_PIN,HIGH); server.send(200,"text/plain","OK"); });
server.on("/light/off", [](){ lightStatus=false; digitalWrite(LIGHT_PIN,LOW); server.send(200,"text/plain","OK"); });
server.on("/fan/on", [](){ fanStatus=true; digitalWrite(FAN_PIN,HIGH); server.send(200,"text/plain","OK"); });
server.on("/fan/off", [](){ fanStatus=false; digitalWrite(FAN_PIN,LOW); server.send(200,"text/plain","OK"); });
server.on("/mode/auto", [](){ autoMode=true; server.send(200,"text/plain","OK"); });
server.on("/mode/manual", [](){ autoMode=false; server.send(200,"text/plain","OK"); });
server.on("/status", handleStatus);
server.begin();
}
void loop() {
server.handleClient();
static unsigned long lastRead = 0;
if (millis() - lastRead >= 2000) {
lastRead = millis();
readSensors();
if (autoMode) autoControl();
updateDisplay();
}
}
void readSensors() {
temperature = dht.readTemperature();
humidity = dht.readHumidity();
motionDetected = digitalRead(PIR_PIN);
}
void autoControl() {
if (motionDetected && !lightStatus) {
lightStatus = true;
digitalWrite(LIGHT_PIN, HIGH);
} else if (!motionDetected && lightStatus) {
lightStatus = false;
digitalWrite(LIGHT_PIN, LOW);
}
if (temperature > 28 && !fanStatus) {
fanStatus = true;
digitalWrite(FAN_PIN, HIGH);
tone(BUZZER_PIN, 1000, 200);
} else if (temperature <= 26 && fanStatus) {
fanStatus = false;
digitalWrite(FAN_PIN, LOW);
}
}
void updateDisplay() {
display.clearDisplay();
display.setCursor(0, 0);
display.print("Temp: "); display.print(temperature, 1); display.println(" C");
display.print("Humid: "); display.print(humidity, 1); display.println(" %");
display.print("Light: "); display.println(lightStatus ? "ON" : "OFF");
display.print("Fan: "); display.println(fanStatus ? "ON" : "OFF");
display.display();
}
void handleRoot() {
String html = "<html><head><meta name='viewport' content='width=device-width'>";
html += "<style>body{font-family:sans-serif;text-align:center;padding:20px;background:#1a1a2e;color:white}</style></head><body>";
html += "<h1>ESP32 Smart Home</h1>";
html += "<p>Temp: " + String(temperature,1) + "C | Humidity: " + String(humidity,1) + "%</p>";
html += "<p>Motion: " + String(motionDetected ? "Yes" : "No") + "</p>";
html += "<p><a href='/light/on'><button>Light ON</button></a> ";
html += "<a href='/light/off'><button>Light OFF</button></a></p>";
html += "<p><a href='/fan/on'><button>Fan ON</button></a> ";
html += "<a href='/fan/off'><button>Fan OFF</button></a></p>";
html += "<p>Mode: " + String(autoMode ? "AUTO" : "MANUAL") + " ";
html += "<a href='/mode/" + String(autoMode ? "manual" : "auto") + "'><button>Toggle</button></a></p>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void handleStatus() {
String json = "{\"temp\":" + String(temperature,1) + ",\"humidity\":" + String(humidity,1);
json += ",\"light\":" + String(lightStatus) + ",\"fan\":" + String(fanStatus);
json += ",\"motion\":" + String(motionDetected) + ",\"auto\":" + String(autoMode) + "}";
server.send(200, "application/json", json);
}
MQTT Integration for Multi-Room Setup
For homes with multiple ESP32 nodes, MQTT is essential. Install Mosquitto on a Raspberry Pi: sudo apt install mosquitto mosquitto-clients. On the ESP32 side, use the PubSubClient library. Publish sensor readings to topics like home/bedroom1/temperature every 30 seconds. Home Assistant can auto-discover sensors using MQTT Discovery topics.
Home Assistant Integration
Once you have MQTT running, configure Home Assistant to connect to your Mosquitto broker (Settings > Integrations > MQTT). HA uses MQTT Discovery – if your ESP32 publishes to topics in the format homeassistant/sensor/bedroom1_temp/config with a JSON payload describing the sensor, HA automatically creates entities without manual YAML configuration. With HA you get: mobile push notifications, automation rules (“turn on AC if temperature > 28C”), Google Assistant/Alexa voice control, and the Lovelace dashboard.
Automation Logic: AUTO vs MANUAL Modes
The system supports two operating modes:
- AUTO mode: Firmware makes all decisions based on sensor inputs. Motion triggers lights, temperature triggers fans, with hysteresis to prevent rapid toggling.
- MANUAL mode: You control everything via the web dashboard or Home Assistant. Sensors still log data but do not trigger actions.
This dual-mode approach balances convenience with control – the system handles routine tasks while you retain override capability.
Safety Considerations
- Use optocoupler-isolated relay modules to protect ESP32 from mains voltage spikes
- Always include a fuse on the mains input side
- House all mains wiring in proper enclosures
- For rental apartments, use plug-in smart switches instead of hardwired relays
- Test all automation logic with low-voltage LEDs before connecting actual appliances
Frequently Asked Questions
Can one ESP32 control multiple rooms?
Yes, a single ESP32 can handle 2-4 rooms by using multiple relay channels. For larger homes, one ESP32 per room is more reliable and easier to debug.
Do I need a Raspberry Pi for MQTT?
No. You can use free cloud MQTT brokers like HiveMQ Cloud (free tier) or connect ESP32 nodes directly to nodrix or Adafruit IO. A Raspberry Pi is optional but gives you local control without internet dependency.
Can I control the system remotely?
Yes. Options include: cloud MQTT broker (HiveMQ, EMQX Cloud), Home Assistant Cloud (Nabu Casa), or a VPN into your home network (Tailscale, ZeroTier).
What is the total cost for a complete 3-room setup in India?
Approximately Rs 2,800 without a hub (using cloud MQTT) or Rs 7,500 with a Raspberry Pi 4 hub running Home Assistant. This is significantly cheaper than commercial smart home systems.
Related Reading
- OPC UA vs MQTT in 2026: Choosing the Right Industrial IoT Protocol
- Edge Computing vs Cloud Computing in Manufacturing 2026
Sources
- Zbotic – How to Build a Smart Home with ESP32 & Sensors
- CircuitDigest – Smart Home Automation with ESP32
- MakeMindz – ESP32 Smart Home Automation System
- Nodrix – DIY Smart Home with ESP32
Disclosure: Some links in this article are affiliate links. We may earn a commission at no extra cost to you. All component prices are approximate and based on Indian market rates.

