Skip to content
Buy 10+ on select items — save 10% auto-applied
Free US shipping on orders $35+
Order by 3pm ET — ships same-day from the US
Skip to main content

ESP32 Relay: Build a Wi-Fi Smart Plug | ShillehTek

May 26, 2026 29 views

ESP32 Relay: Build a Wi-Fi Smart Plug | ShillehTek
Project

Build an ESP32 + relay Wi-Fi smart plug you can control via web UI or MQTT, with Tasmota-compatible options and no cloud lock-in from ShillehTek.

2 hr Intermediate (mains AC handl3 parts

Project Overview

ESP32 + 1-channel relay smart plug: In this project, you will use an ESP32 dev board and a 1-channel 5V relay module to build a Wi-Fi smart plug that can be controlled via a web UI, MQTT, or platforms like Home Assistant (including Tasmota-compatible workflows).

Most off-the-shelf smart plugs are essentially an ESP32/ESP8266 driving a relay. Building your own lets you choose the firmware (Tasmota, ESPHome, or custom code) and avoid cloud vendor lock-in.

  • Time: ~2 hours
  • Skill level: Intermediate (mains AC handling)
  • What you will build: A DIY Wi-Fi smart plug controllable from a phone, browser, or MQTT.
DIY ESP32 Wi-Fi smart plug enclosure with ESP32 and relay inside
ESP32 + relay + mains-rated wiring = open-source smart plug.

Parts List

From ShillehTek

External

  • Mains-rated AC outlet + AC power cord (3-pin) - the load side and mains input.
  • 5V USB power adapter (mains in, 5V out) - powers the ESP32 and the relay module.
  • Project enclosure properly rated for mains voltage - prevents accidental contact with live wiring.
  • Mains-rated wire (14 AWG or heavier) - for safe current handling on the AC side.

SAFETY: This project switches MAINS AC. Mistakes here are dangerous. If you are not 100% comfortable wiring mains, skip this build and use a commercial smart plug or a pre-built relay module triggered by Wi-Fi in a properly rated enclosure with no exposed mains.

Step-by-Step Guide

Step 1 - Plan the circuit

Goal: Understand what is being switched and where the ESP32 connects.

What to do: Plan for the relay to switch only the LIVE conductor. Neutral and earth should pass through directly to the outlet. On the low-voltage side, the ESP32 will drive the relay input pin from a GPIO.

ESP32 smart plug circuit overview showing mains input, relay switching live, and outlet output
Mains-in to relay (switched live wire) to outlet. ESP32 controls the relay over Wi-Fi.

Expected result: You have a clear plan for the mains path (live switched) and the logic path (ESP32 controls relay).

Step 2 - Wire the mains side

Goal: Route the AC live wire through the relay contacts so the outlet can be switched safely.

What to do: With power disconnected, wire the relay contacts so that the live conductor is switched, while neutral and earth remain uninterrupted.

AC mains wiring diagram for smart plug with live wired through relay COM and NO, neutral and earth pass-through
Live wire goes through relay COM and NO contacts. Neutral and earth pass through directly.
  • Mains-in LIVE to relay COM
  • Relay NO to outlet LIVE pin
  • Mains-in NEUTRAL to outlet NEUTRAL pin (uninterrupted)
  • Mains-in EARTH to outlet EARTH pin (uninterrupted)

Expected result: The outlet live is controlled by the relay, and neutral/earth are continuous from input to outlet.

Step 3 - Wire the logic side

Goal: Connect the ESP32 to the relay module so a GPIO can toggle the relay.

What to do: Power the ESP32 and relay module from the 5V USB adapter output, connect a GPIO to the relay input pin, and ensure the grounds are shared.

  • 5V USB adapter (mains in, 5V out) powers the ESP32 and relay module
  • ESP32 GPIO 13 to relay IN
  • Common ground between ESP32 and relay module

Expected result: The relay module has power and can be switched by ESP32 GPIO 13.

Step 4 - Assemble everything in an enclosure

Goal: Build a safe physical assembly with separation between mains and low-voltage wiring.

What to do: Mount the outlet, relay, ESP32, and power adapter securely. Use mains-rated wire on the AC side and secure connections with proper connectors (for example, WAGO clips or appropriate crimps). Keep mains and logic physically separated inside the enclosure.

Assembled ESP32 smart plug internals showing relay, mains wiring, and low-voltage wiring
Use mains-rated wire on the AC side and secure all connections with proper connectors.
ESP32 smart plug enclosure internals showing physical separation between mains wiring and ESP32 logic wiring
Mains and logic should be physically separated inside the enclosure for safety.

Expected result: The build is enclosed with no exposed mains, and the wiring is mechanically secure.

Step 5 - Choose and flash firmware

Goal: Get a working control interface (web UI and/or MQTT) to switch the relay over Wi-Fi.

What to do: Pick the firmware approach that matches your goals. Tasmota/ESPHome are common for quick setup, while custom code gives full control.

  1. Tasmota / ESPHome - pre-built, well-tested, has a web UI and MQTT out of the box. Flash via tasmotizer.
  2. Arduino IDE custom sketch - write your own ESP32WebServer page with on/off endpoints and scheduling.
  3. Blynk / Home Assistant - pair with cloud or local-network apps.

Code:

// Minimal Arduino sketch - visit ESP32-IP/on or /off
#include <WiFi.h>
#include <WebServer.h>
WebServer server(80);
const int RELAY = 13;

void setup() {
  pinMode(RELAY, OUTPUT);
  WiFi.begin("SSID", "PASS");
  while (WiFi.status() != WL_CONNECTED) delay(500);
  server.on("/on",  [](){ digitalWrite(RELAY, HIGH); server.send(200, "text/plain", "ON"); });
  server.on("/off", [](){ digitalWrite(RELAY, LOW);  server.send(200, "text/plain", "OFF"); });
  server.begin();
}
void loop() { server.handleClient(); }

Expected result: The ESP32 connects to Wi-Fi and you can toggle the relay from a browser using /on and /off (or from your chosen firmware UI).

Step 6 - Control the smart plug

Goal: Verify you can switch the outlet from your chosen interface.

What to do: Use the firmware web UI (or your own endpoints) on your local network. If you use Home Assistant or MQTT, add the device and test on/off control.

Web UI controlling ESP32 smart plug relay on a phone or browser
Web UI from anywhere on your network. Add it to Home Assistant for scheduling and automation.

Expected result: You can switch the relay on and off reliably via your web UI, MQTT, or Home Assistant.

Step 7 - Optional expansions

Goal: Identify safe upgrades you can add after the basic smart plug is working.

What to do: Consider extending the build with measurements, displays, or more channels.

  • Add an INA219 to measure how much current the load draws to see how much your TV or fridge actually uses
  • Add an SSD1306 OLED inside (visible through a window) for at-a-glance state
  • Use Tasmota's energy module on supported chips for live kWh tracking
  • Multi-channel power strip: 4-channel relay + ESP32 for individual outlet control

Expected result: You have a clear next-step roadmap without changing the core ESP32-to-relay smart plug design.

Conclusion

You built an ESP32-based Wi-Fi smart plug using a 1-channel relay module, with firmware options ranging from Tasmota/ESPHome to a custom Arduino web server. The result is a local-control friendly smart plug that can integrate with MQTT and platforms like Home Assistant.

Want the exact parts used in this build? Grab them from ShillehTek.com. If you want help customizing this project or building something for your product, check out our IoT consulting services.

Reference credit: Instructables.