Connected
Wi-Fi switch hub
Two switches you flip from a web page on your phone, for low-voltage things such as a USB lamp or a small fan.
- Medium
- about 40 min
- ESP32-S3
- 5 parts
- 4 wires
- 60 lines of code
- Uses Wi-Fi
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
A relay is a switch moved by an electromagnet, so a 3.3 V signal can control a separate circuit without touching it. This is how smart plugs work inside.
Before you start
Switch low-voltage loads only: 5 to 12 V. Never connect mains electricity (220 V) to the relay. Mains wiring is for a qualified adult, and this build is not designed for it.
What you learn
Controlling outputs from a web page, and why a relay keeps two circuits apart.
How it works
input
A phone on the same Wi-Fi
decides
ESP32-S3 · Wi-Fi
output
2-channel relay
Powered with 5 V through the board's USB-C port.
It joins your home Wi-Fi. You type the network name and password into the sketch before uploading; they stay on the board.
A reference design. The sketch is compiled with the real ESP32 toolchain, and its pins are checked against the wiring table automatically. It has not yet been built and tested on real parts by us, so wire it on the bench and run the checks before you build it into a case. Real modules vary: follow the labels printed on yours, not their position in the picture.
Parts
0 of 5 ready
No prices here: they change by shop and by week. Buy each part by its name and the note beside it.
You also need
A computer with Arduino IDE 2 installed (free)
3D wiring
One jumper per step, with the USB cable unplugged. Several wires may share 3V3, 5V or GND: the expansion board or breadboard gives each its own pin.
Step 0 of 4
Lay out the parts
Seat the board on its expansion board or breadboard and lay the modules out round it. Nothing is connected yet.
All connections
wifi-hub.ino
// Wi-Fi switch hub
// Two relays you switch from a web page on your phone.
// LOW VOLTAGE ONLY: switch a USB lamp or a 5-12 V fan. Never mains power.
#include <WiFi.h>
#include <WebServer.h>
const char* WIFI_SSID = "YOUR_WIFI_NAME";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";
const int PIN_RELAY_IN1 = 5;
const int PIN_RELAY_IN2 = 6;
// Most relay modules switch ON when the input is LOW. Flip this if yours is the other kind.
const int RELAY_ON = LOW;
const int RELAY_OFF = RELAY_ON == LOW ? HIGH : LOW;
WebServer server(80);
bool on[2] = {false, false};
const int PINS[2] = {PIN_RELAY_IN1, PIN_RELAY_IN2};
void apply() {
for (int i = 0; i < 2; i++) digitalWrite(PINS[i], on[i] ? RELAY_ON : RELAY_OFF);
}
void sendPage() {
String html = "<!doctype html><meta name=viewport content='width=device-width,initial-scale=1'><title>Switch hub</title>"
"<body style='font-family:sans-serif;max-width:24rem;margin:2rem auto;padding:0 1rem'><h1>Switch hub</h1>";
for (int i = 0; i < 2; i++) {
html += "<p style='font-size:1.3rem'>Switch " + String(i + 1) + ": <b>" + (on[i] ? "ON" : "OFF") + "</b> ";
html += "<a href='/toggle?n=" + String(i) + "'><button style='font-size:1.2rem;padding:.5rem 1rem'>Toggle</button></a></p>";
}
html += "</body>";
server.send(200, "text/html", html);
}
void setup() {
Serial.begin(115200);
for (int i = 0; i < 2; i++) pinMode(PINS[i], OUTPUT);
apply();
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) delay(250);
Serial.print("Open this address in a browser: http://");
Serial.println(WiFi.localIP());
server.on("/", sendPage);
server.on("/toggle", []() {
int n = server.arg("n").toInt();
if (n >= 0 && n < 2) on[n] = !on[n];
apply();
server.sendHeader("Location", "/");
server.send(303);
});
server.begin();
}
void loop() {
server.handleClient();
}
Libraries
None. It uses only what the Arduino IDE has built in.
Uploading
First time with an ESP32: in Arduino IDE open Boards Manager, search “esp32” and install “esp32 by Espressif Systems”.
- Before uploading, type your Wi-Fi name and password into WIFI_SSID and WIFI_PASS at the top of the sketch.
- Open the sketch in Arduino IDE.
- Plug the board in with the USB-C cable.
- Tools > Board > ESP32S3 Dev Module, then Tools > Port and choose the board's port.
- Press Upload (the arrow). Wait for “Done uploading”.