ESP32 Wi-Fi switch with web buttons
Build it step by step
0 of 5 doneInstall the ESP32 boards
In the Arduino IDE Boards Manager, install "esp32 by Espressif Systems".
Wire the relays
VCC to VIN, GND to GND, IN1 to GPIO26, IN2 to GPIO27.
Add your Wi-Fi
Put your network name and password in SSID and PASS. The ESP32 needs 2.4 GHz Wi-Fi.
Upload and find the address
Upload at 115200 baud and read the IP address in the serial monitor.
Open the page
On a phone on the same Wi-Fi, open that address and tap the buttons.
How do you build an ESP32 Wi-Fi switch with web buttons?
An ESP32 hosts a small web page on your home Wi-Fi with buttons that switch two relays from any phone or laptop. You need ESP32 DevKit V1, Relay module 2-channel 5 V, 5 V 2 A USB adapter, USB A to micro-USB cable (data) and Jumper wires female to female (40 pcs). It takes 5 steps and about 75 minutes.
- Level
- Intermediate
- Time
- About 75 min
- Parts
- 5
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Relay VCC / GND | ESP32 VIN (5 V from USB) / GND | Relay coils need 5 V |
| Relay IN1 / IN2 | ESP32 GPIO26 / GPIO27 | 3.3 V logic is enough for most opto-isolated boards |
| Loads | Relay COM / NO | Practise with a 12 V lamp or fan |
Code
Arduino IDE with the ESP32 board package (Espressif), board "ESP32 Dev Module". WiFi and WebServer come with the package. Put your Wi-Fi name and password in the sketch.
Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <WiFi.h>
#include <WebServer.h>
const char* SSID = "your-wifi";
const char* PASS = "your-password";
const int RELAY[2] = {26, 27};
const bool ON = LOW;
bool state[2] = {false, false};
WebServer server(80);
String page() {
String h = "<!doctype html><meta name=viewport content='width=device-width'><title>Switches</title>"
"<style>body{font-family:sans-serif;padding:20px}a{display:block;margin:12px 0;padding:18px;border-radius:12px;"
"text-align:center;color:#fff;text-decoration:none;font-size:20px}</style>";
for (int i = 0; i < 2; i++) {
h += "<a style='background:" + String(state[i] ? "#16a34a" : "#64748b") + "' href='/t?r=" + String(i) + "'>Switch " +
String(i + 1) + ": " + (state[i] ? "ON" : "OFF") + "</a>";
}
return h;
}
void setup() {
Serial.begin(115200);
for (int i = 0; i < 2; i++) { pinMode(RELAY[i], OUTPUT); digitalWrite(RELAY[i], !ON); }
WiFi.begin(SSID, PASS);
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
Serial.print("\nOpen http://"); Serial.println(WiFi.localIP());
server.on("/", [] { server.send(200, "text/html", page()); });
server.on("/t", [] {
int i = server.arg("r").toInt();
if (i >= 0 && i < 2) { state[i] = !state[i]; digitalWrite(RELAY[i], state[i] ? ON : !ON); }
server.sendHeader("Location", "/"); server.send(303);
});
server.begin();
}
void loop() { server.handleClient(); }Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Serial monitor after reset | Dots, then "Open http://192.168.x.x" |
| Tap Switch 1 | Relay 1 clicks and the button turns green |
| Page reloaded from another phone | Shows the same states |
| Router restarts | Reset the ESP32 to reconnect (add WiFi.setAutoReconnect(true) to make it automatic) |
If it does not work
Never connects
Check the name and password, and that the network is 2.4 GHz, not 5 GHz only.
Relays do not click
Some boards need a 5 V input signal. Use a board with optocouplers, or switch through a transistor.
Page does not open
The phone must be on the same Wi-Fi, not mobile data.
Anyone on the Wi-Fi can switch it
Fine for learning. Add a password page before using it in a shared building.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the ESP32 Wi-Fi switch with web buttons?
ESP32 DevKit V1, Relay module 2-channel 5 V, 5 V 2 A USB adapter, USB A to micro-USB cable (data) and Jumper wires female to female (40 pcs).
How long does the ESP32 Wi-Fi switch with web buttons take to build?
About 75 minutes for a intermediate maker, in 5 steps.
How do I know the ESP32 Wi-Fi switch with web buttons works?
Serial monitor after reset: Dots, then "Open http://192.168.x.x". Tap Switch 1: Relay 1 clicks and the button turns green. Page reloaded from another phone: Shows the same states.
Never connects: what should I check?
Check the name and password, and that the network is 2.4 GHz, not 5 GHz only.
Relays do not click: what should I check?
Some boards need a 5 V input signal. Use a board with optocouplers, or switch through a transistor.
Page does not open: what should I check?
The phone must be on the same Wi-Fi, not mobile data.
Anyone on the Wi-Fi can switch it: what should I check?
Fine for learning. Add a password page before using it in a shared building.
