Building a smart home in India does not have to be expensive. With an ESP32 development board and a relay module, you can build smart WiFi switches for under ₹500 each — significantly cheaper than commercial options like Sonoff, Wipro Smart, or Philips Hue switches that cost ₹1,000–₹3,000 per switch. The best part? Your DIY ESP32 smart switches are fully customisable and do not depend on any company’s cloud servers.
In this guide, we will show you how to build affordable WiFi-controlled switches that work with your existing Indian switchboards and 230V AC appliances.
Why Build DIY Smart Switches?
Commercial smart switches in India come with several limitations that DIY ESP32 switches do not have:
- Cost: A single Sonoff Basic costs ₹800–₹1,200. A Wipro Smart switch costs ₹1,500+. Your DIY switch costs under ₹500.
- Cloud dependency: Most commercial switches require the manufacturer’s cloud server. If the company shuts down or their servers go offline, your switches stop working. DIY switches work entirely on your local network.
- Customisation: You control the firmware. Add features like scheduling, power monitoring, or integration with any platform you like.
- Privacy: No data leaves your home network. Commercial switches often send usage data to Chinese or American cloud servers.
- Learning: Building your own smart switches teaches you about electronics, networking, and programming — skills that are increasingly valuable.
Cost Breakdown: Under ₹500 Per Switch
Here is the actual cost of building one smart WiFi switch in India (prices as of 2026):
| Component | Approximate Price (₹) |
|---|---|
| ESP32 Development Board | ₹250–₹350 |
| 1-Channel 5V Relay Module | ₹35–₹60 |
| Hi-Link HLK-PM01 (AC to 5V DC converter) | ₹80–₹120 |
| Wires, connectors, push button | ₹30–₹50 |
| Total | ₹395–₹480 |
Compare this to a single Sonoff Basic R4 at ₹999 or a Wipro Smart Switch at ₹1,499. You save 50–70% per switch, and those savings multiply when you are automating your entire house.
Components and Tools Needed
You will also need:
- A soldering iron and solder wire
- Heat-shrink tubing for insulating connections
- A small push button (for manual override)
- A micro USB cable for initial programming
- Arduino IDE installed on your computer
Building the ESP32 Web-Controlled Switch
Our smart switch will host a tiny web server on your home WiFi network. You open a browser on your phone, type the switch’s IP address, and get a simple ON/OFF toggle button. No app installation required — it works on any device with a web browser.
Wiring Diagram
Hi-Link PM01 Input → 230V AC Live and Neutral
Hi-Link PM01 Output → 5V DC to ESP32 VIN and Relay VCC
ESP32 GPIO 26 → Relay IN1
ESP32 GPIO 27 → Push Button (other leg to GND)
ESP32 GND → Relay GND, Button GND
Relay COM → 230V AC Live (from mains)
Relay NO → Appliance Live wire
230V AC Neutral → Direct to appliance (not through relay)
Complete ESP32 Smart Switch Code
#include <WiFi.h>
#include <WebServer.h>
#include <Preferences.h>
// WiFi credentials
const char* ssid = "Your_WiFi_SSID";
const char* password = "Your_WiFi_Password";
// Pin definitions
#define RELAY_PIN 26
#define BUTTON_PIN 27
WebServer server(80);
Preferences prefs;
bool relayState = false;
bool lastButtonState = HIGH;
unsigned long lastDebounce = 0;
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Restore last state from flash memory
prefs.begin("switch", false);
relayState = prefs.getBool("state", false);
digitalWrite(RELAY_PIN, relayState ? LOW : HIGH);
// Connect to WiFi
WiFi.begin(ssid, password);
WiFi.setAutoReconnect(true);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("
IP Address: ");
Serial.println(WiFi.localIP());
// Web server routes
server.on("/", handleRoot);
server.on("/toggle", handleToggle);
server.on("/status", handleStatus);
server.begin();
}
void handleRoot() {
String page = R"rawliteral(
<!DOCTYPE html><html><head>
<meta name='viewport' content='width=device-width,initial-scale=1'>
<title>Smart Switch</title>
<style>
body{font-family:Arial;text-align:center;padding:40px;background:#1a1a2e;color:white;}
.switch-btn{width:120px;height:120px;border-radius:50%;border:4px solid white;
font-size:20px;font-weight:bold;cursor:pointer;transition:all 0.3s;}
.on{background:#00b894;box-shadow:0 0 30px #00b894;}
.off{background:#d63031;box-shadow:0 0 30px #d63031;}
h1{margin-bottom:30px;}
</style>
</head><body>
<h1>Smart Switch</h1>
<button class='switch-btn )rawliteral";
page += relayState ? "on" : "off";
page += "' onclick='fetch("/toggle").then(()=>location.reload())'>";
page += relayState ? "ON" : "OFF";
page += "";
server.send(200, "text/html", page);
}
void handleToggle() {
relayState = !relayState;
digitalWrite(RELAY_PIN, relayState ? LOW : HIGH);
prefs.putBool("state", relayState);
server.send(200, "text/plain", relayState ? "ON" : "OFF");
}
void handleStatus() {
server.send(200, "text/plain", relayState ? "ON" : "OFF");
}
void loop() {
server.handleClient();
// Physical button handling with debounce
bool reading = digitalRead(BUTTON_PIN);
if (reading != lastButtonState && (millis() - lastDebounce) > 50) {
lastDebounce = millis();
if (reading == LOW) { // Button pressed
relayState = !relayState;
digitalWrite(RELAY_PIN, relayState ? LOW : HIGH);
prefs.putBool("state", relayState);
}
}
lastButtonState = reading;
}
This code includes several smart features:
- State persistence: Uses ESP32’s flash memory to remember the last state across power cuts
- Physical button: The wall switch still works even if WiFi is down
- Auto-reconnect: Automatically reconnects to WiFi if the connection drops
- Responsive web UI: Works perfectly on mobile phones
Adding a Physical Button Override
One of the most important features for a practical smart switch is the physical button override. Your family members should be able to use the switch normally without needing a phone. The code above already handles this — GPIO 27 is configured as a button input with debouncing.
For the physical button, you have two options:
- Existing wall switch: Wire your existing modular switch in parallel with the relay, using the ESP32 to detect switch state changes
- Dedicated push button: Mount a small tactile push button on the switchboard cover
Controlling Multiple Switches from One Dashboard
When you have multiple ESP32 switches around your house, you will want a single dashboard to control them all. There are two approaches:
Option 1: mDNS Discovery
Give each ESP32 a unique hostname (e.g., bedroom-light.local, kitchen-fan.local) using the ESPmDNS library. Then create a master dashboard on one ESP32 that queries all other switches.
Option 2: Home Assistant Integration
Install Home Assistant on a Raspberry Pi or old laptop, and configure each ESP32 switch as an MQTT device. This gives you a professional-grade dashboard, automation rules, and voice control through Google Home or Alexa.
We cover the MQTT approach in detail in our MQTT for Home Automation guide.
Fitting Inside an Indian Switchboard
Indian modular switchboards (Anchor Roma, Legrand, Havells) have limited space behind the plate. Here are tips for fitting your ESP32 switch inside:
- Use an ESP32 Mini: Smaller form factor boards like the ESP32-C3 or Wemos D1 Mini ESP32 fit more easily
- Mount vertically: Stack the relay module behind the ESP32 with double-sided tape
- Use a deeper back box: If your wall allows, switch to a deeper back box (available at any electrical shop for ₹30–₹50)
- External mounting: For older homes with round back boxes, mount the ESP32 setup inside a small plastic junction box beside the switchboard
- Heat management: Drill small ventilation holes in the enclosure. The ESP32 generates minimal heat, but the Hi-Link power supply can get warm
Frequently Asked Questions
Can I use ESP32 with a ceiling fan regulator?
A relay gives you only ON/OFF control. For speed control, you need a TRIAC-based dimmer module (like the BTA16 or RobotDyn AC dimmer). These can be controlled via ESP32 PWM to adjust fan speed in steps.
Will the switch work during internet outages?
Yes. The ESP32 web server runs on your local WiFi network. As long as your router is on (even without internet), you can control the switch from your phone. Only cloud-based features (like Google Home integration) require internet.
How much electricity does the ESP32 switch consume?
An ESP32 in active WiFi mode draws about 80–160mA at 3.3V, which translates to roughly 0.5W. Running 24/7, that is about 0.36 kWh per month — costing less than ₹3/month on most Indian electricity tariffs.
Can I use this with a 3-phase AC supply?
Each relay can switch one phase independently. For 3-phase appliances, you would need 3 relays — one for each phase — triggered simultaneously. However, for most home appliances in India, single-phase switching is sufficient.
What if my WiFi router is far from the switchboard?
The ESP32 has a decent WiFi range (about 10–15 metres through walls). If your router is far away, either use a WiFi repeater or set up an ESP-NOW mesh network between ESP32 devices, with one acting as a gateway near the router.
Conclusion
Building smart WiFi switches with ESP32 is one of the most cost-effective ways to start your smart home journey in India. At under ₹500 per switch, you can automate your entire house for less than the cost of a single commercial smart home kit. The ESP32’s built-in WiFi and Bluetooth, combined with its powerful dual-core processor, make it the ideal brain for your DIY smart home.
Start with one switch — perhaps your bedroom light — and expand from there. Once you experience the convenience of controlling your home from your phone, you will want to automate everything.
Get all the components you need at Zbotic.in — India’s largest electronics component store with fast shipping and genuine products. Browse our relay modules and ESP32 boards to get started today.
Add comment