Building a smart home with ESP32 is one of the most rewarding IoT projects you can tackle. Whether you want to automate lights, monitor temperature, or control appliances remotely, the ESP32 microcontroller gives you everything you need: Wi-Fi, Bluetooth, plenty of GPIOs, and a thriving ecosystem of libraries. In this guide we walk through the architecture, components, wiring, code, and estimated cost for a 3-room smart home setup in India — all using affordable hardware available on Zbotic.in.
Table of Contents
- Why ESP32 for Smart Home?
- Smart Home Architecture Overview
- Components You Need
- Relay Control for Lights and Fans
- Temperature & Humidity Monitoring with DHT11
- Motion-Triggered Lighting with PIR Sensor
- Web Dashboard for Remote Control
- MQTT Communication
- Home Assistant Integration Basics
- Estimated Cost for a 3-Room Setup (INR)
- FAQ
Why ESP32 for Smart Home?
The ESP32 is the go-to microcontroller for home automation in India for several compelling reasons. 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 running at up to 240 MHz, and hardware support for SPI, I2C, UART, PWM, and ADC, the ESP32 can comfortably manage multiple sensors and actuators simultaneously. Its low price — under ₹400 for most variants — makes it far more accessible than platforms like Raspberry Pi for embedded control tasks.
Newer variants like the ESP32-C6 bring Wi-Fi 6 (802.11ax) and Bluetooth 5.3 support, which significantly improves range and reduces congestion in apartments where dozens of wireless networks overlap. For a whole-home deployment, this matters.
Smart Home Architecture Overview
A practical ESP32 smart home system consists of three layers:
- Edge layer: ESP32 nodes placed in each room, each controlling relays and reading local sensors.
- Communication layer: MQTT broker (running on a Raspberry Pi, a local server, or a cloud service like HiveMQ) that routes messages between nodes and the dashboard.
- Application layer: A web dashboard (built with Node-RED or a custom HTML page served by the ESP32 itself) or a Home Assistant instance for a polished UI and automation rules.
For a small apartment, a single ESP32 can handle 2–4 rooms by managing multiple relays. For larger homes or for reliability, one ESP32 node per room is cleaner and more fault-tolerant. All nodes connect to your home Wi-Fi router and communicate via the MQTT broker.
Components You Need
Here is a practical bill of materials per room node:
- 1× ESP32 development board (ESP32-C6 Mini or standard ESP32)
- 1–4 channel relay module (5V, optocoupler-isolated)
- 1× DHT11 or DHT22 temperature & humidity sensor
- 1× HC-SR501 PIR motion sensor (for motion-triggered lighting)
- 1–2 magnetic door/window reed switches
- 5V/2A USB power supply or Hi-Link AC-DC module for mains power
- Jumper wires, terminal blocks, and a small enclosure
For the central hub you will need a Raspberry Pi 4 (or even a Raspberry Pi Zero 2W) running Mosquitto MQTT broker and optionally Home Assistant OS. Alternatively, use a free cloud MQTT broker for a zero-hardware-hub setup.
Relay Control for Lights and Fans
Relays are electromechanical switches that let a 3.3V/5V GPIO signal switch 230V AC loads safely. Use optocoupler-isolated relay modules to protect the ESP32 from voltage spikes. A 4-channel relay board can independently control ceiling light, fan, bedside lamp, and a power socket in a single room.
Wiring is straightforward: connect the relay IN pin to an ESP32 GPIO, VCC to 5V, and GND to GND. Then wire the 230V load through the relay NO (normally open) and COM terminals. Always use proper insulated wiring for the mains side and house everything in a plastic enclosure.
Sample Arduino IDE code to toggle a relay via a simple HTTP endpoint:
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "YourWiFi";
const char* password = "YourPassword";
#define RELAY_PIN 26
WebServer server(80);
void handleRelay() {
String state = server.arg("state");
if (state == "on") {
digitalWrite(RELAY_PIN, LOW); // LOW = relay ON (active-low modules)
server.send(200, "text/plain", "Relay ON");
} else {
digitalWrite(RELAY_PIN, HIGH);
server.send(200, "text/plain", "Relay OFF");
}
}
void setup() {
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // start OFF
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
server.on("/relay", handleRelay);
server.begin();
}
void loop() {
server.handleClient();
}
Access http://<ESP32-IP>/relay?state=on from any browser on your local network to toggle the relay. For fan speed control, pair with a TRIAC module and PWM output from the ESP32.
Temperature & Humidity Monitoring with DHT11
The DHT11 is the most popular beginner-friendly climate sensor. It provides temperature (0–50°C, ±2°C accuracy) and humidity (20–80% RH, ±5% accuracy) readings over a single-wire protocol. For better accuracy, upgrade to a DHT22 (−40–80°C, ±0.5°C). Wire DATA to GPIO 4, VCC to 3.3V, and GND to GND with a 10kΩ pull-up resistor on the data line.
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (!isnan(h) && !isnan(t)) {
Serial.printf("Temp: %.1f°C Humidity: %.1f%%n", t, h);
}
delay(2000);
}
Integrate this data with your MQTT broker by publishing to a topic like home/bedroom1/temperature every 30 seconds. Home Assistant will auto-discover the sensor if you follow the HA MQTT discovery topic format.
Motion-Triggered Lighting with PIR Sensor
The HC-SR501 PIR (Passive Infrared) sensor detects human movement by sensing changes in infrared radiation. It has two onboard potentiometers: one for sensitivity (detection range up to 7 metres) and one for time delay (how long the output stays HIGH after detection). Wire OUT to an ESP32 GPIO, VCC to 5V, and GND to GND.
Logic for motion-triggered light automation:
#define PIR_PIN 14
#define RELAY_PIN 26
unsigned long lastMotion = 0;
const unsigned long LIGHT_ON_TIME = 120000; // 2 minutes
void setup() {
pinMode(PIR_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // OFF
}
void loop() {
if (digitalRead(PIR_PIN) == HIGH) {
lastMotion = millis();
digitalWrite(RELAY_PIN, LOW); // Turn light ON
}
if (millis() - lastMotion > LIGHT_ON_TIME) {
digitalWrite(RELAY_PIN, HIGH); // Turn light OFF
}
delay(100);
}
Install the PIR sensor in a corner of the room or corridor at 2–2.5 metres height for optimal coverage. The bracket accessory lets you angle the sensor precisely without adhesive tape hacks.
Web Dashboard for Remote Control
The ESP32 can serve a small web dashboard directly from its flash memory using the SPIFFS or LittleFS filesystem. Upload an index.html file with toggle buttons styled with CSS, and use JavaScript fetch() calls to hit the ESP32 REST endpoints. This approach works with zero cloud dependency — perfect when your internet is down.
For a more polished dashboard, install Node-RED on a Raspberry Pi. Drag-and-drop MQTT input nodes, dashboard button nodes, and gauge nodes to create a fully interactive home control panel accessible from your phone browser on the local network. Add a Tailscale or ZeroTier VPN for secure remote access without port-forwarding.
MQTT Communication
MQTT (Message Queuing Telemetry Transport) is the de facto protocol for IoT messaging. It uses a lightweight publish/subscribe model — devices publish sensor readings to a topic, and other devices or the dashboard subscribe to that topic to receive updates. This decouples your sensors from your actuators and dashboard, making the system highly modular.
Install Mosquitto on a Raspberry Pi with sudo apt install mosquitto mosquitto-clients. Configure authentication with a username/password and enable TLS if exposing it beyond your local network. On the ESP32 side, use the popular PubSubClient library. See our dedicated MQTT tutorial (Post 58 in this series) for a full code walkthrough.
Home Assistant Integration Basics
Home Assistant (HA) is an open-source home automation platform that runs beautifully on a Raspberry Pi 4. Once installed, go to Settings → Integrations → MQTT and point it to your Mosquitto broker IP and port (default 1883). 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 will automatically create entities without any manual YAML configuration.
With HA you get: a mobile app with push notifications, automation rules (“turn on AC if temperature > 28°C”), energy monitoring, Google Assistant / Amazon Alexa voice control, and a beautiful Lovelace dashboard. The combination of ESP32 + MQTT + Home Assistant is arguably the most powerful and cost-effective smart home stack available today.
Estimated Cost for a 3-Room Setup (INR)
Here is a realistic budget for a 3-bedroom Indian home:
| Component | Qty | Unit Price (₹) | Total (₹) |
|---|---|---|---|
| ESP32 Dev Board | 3 | 380 | 1,140 |
| 4-Channel Relay Module | 3 | 150 | 450 |
| DHT11 Sensor Module | 3 | 80 | 240 |
| HC-SR501 PIR Sensor | 3 | 60 | 180 |
| 5V/2A USB Power Supply | 3 | 120 | 360 |
| Raspberry Pi 4 (2GB) Hub | 1 | 4,500 | 4,500 |
| Enclosures, wires, misc | — | — | 600 |
| Total | ₹7,470 |
Skip the Raspberry Pi by using a free cloud MQTT broker (HiveMQ Cloud free tier) and Node-RED on your PC for the dashboard — that brings the total down to around ₹2,800 for the three-room hardware. You can also expand the system over time by adding more nodes for the kitchen, bathroom, or balcony without changing the hub.
Frequently Asked Questions
Q: Can I control my smart home remotely from outside my Wi-Fi network?
Yes. The easiest approach is to use a cloud MQTT broker (HiveMQ, EMQX Cloud, or Adafruit IO) — your ESP32 nodes and your phone both connect to the broker over the internet. Alternatively, install Nabu Casa (Home Assistant Cloud) for a paid but effortless remote access solution, or self-host with Tailscale VPN (free for personal use).
Q: Is ESP32 safe to use with 230V mains appliances?
The ESP32 itself only handles low-voltage (3.3V) signals. The relay module does the actual switching of 230V. Always use optocoupler-isolated relay boards, keep the mains wiring inside a proper ABS enclosure, use appropriate wire gauges for your load current, and follow IS standards. If you are not comfortable with mains wiring, have a licensed electrician do that part.
Q: What happens if my Wi-Fi router goes offline?
Your ESP32 nodes lose MQTT connectivity and remote control, but local automations (like the PIR motion light) continue to work because they run directly on the microcontroller. Code your ESP32 to reconnect automatically with a non-blocking reconnect loop — this handles brief Wi-Fi dropouts gracefully.
Q: How many devices can one Raspberry Pi Mosquitto broker handle?
Mosquitto is extremely lightweight. On a Raspberry Pi 4, it can comfortably handle 1,000+ concurrent MQTT connections with low message rates. For a typical home with 10–50 nodes, resource usage is negligible.
Q: Can I use ESP32 with Google Home or Amazon Alexa?
Not directly, but through Home Assistant you can link your smart home to Google Home or Alexa. HA exposes your ESP32-controlled devices as smart home entities that Google and Alexa can discover and control via voice commands.
Build Your IoT Project
Shop ESP32, sensors, and wireless modules at Zbotic.in — fast shipping across India.
Add comment