Build a WiFi Smart Plug with ESP8266: Control Any Appliance from Your Phone

A WiFi smart plug works by putting a small WiFi-connected microcontroller in charge of a relay: the microcontroller (an ESP8266 in this build) joins your home WiFi network and listens for an on/off command from your phone, and when that command arrives it flips a GPIO pin, which switches a relay module's coil, which physically closes or opens the mains circuit feeding whatever appliance is plugged into it — a lamp, a fan, anything that just needs power cut or restored. The ESP8266 itself never touches the 220V mains current; it only ever handles the relay's low-voltage control side, which is the entire reason the relay exists as a separate part rather than wiring the lamp straight to the board.

What parts does a DIY smart plug need?

Four roles have to be filled, regardless of which specific parts you buy:

  • Microcontroller with WiFi built in — an ESP8266 (commonly sold as a NodeMCU development board) has WiFi on the same chip, so it can join your home network and receive phone commands without a separate wireless module.
  • Relay module — the switch that actually interrupts mains power to the appliance; it takes a low-voltage on/off signal from the microcontroller and uses that to control a much higher-voltage circuit on its other side.
  • Power supply — a 5V adapter to run the microcontroller and the relay's control side. Note this only powers the electronics; it is separate from the mains power the relay switches for the appliance.
  • Prototyping and wiring — a breadboard and jumper wires to connect the ESP8266's GPIO pin to the relay's control input while you build and test, before any permanent enclosure.

How does a relay let a microcontroller switch mains power safely?

A relay module physically separates two circuits that never touch each other electrically. On one side is the control circuit: the ESP8266 sends 3.3V logic to the relay's input pin, which energizes a small electromagnetic coil (or, on a solid-state relay, triggers an optically isolated switch). On the other side is the load circuit: that coil pulls a mechanical contact closed, completing or breaking the mains path to the appliance. Because the two sides are only linked magnetically (or optically), the low-voltage GPIO pin is never electrically connected to the 220V line — the relay is what makes it safe to let a 3.3V signal decide whether 220V reaches a lamp. This is also why the relay module, not the microcontroller, is rated for the mains-side voltage and current, and why buying a pre-built relay module with its mains-side terminals already isolated behind a plastic housing is preferable to bare mains wiring on a breadboard.

Is it safe to build your own smart plug?

This build involves exposed 220V mains wiring on the relay's load-side terminals, and that carries a real risk of electric shock if touched while live. Only attempt this if you are comfortable working around mains electricity: always unplug the relay from the wall before wiring or rewiring it, never work on the mains side with power connected, and double-check every mains-side connection before plugging in. Use a pre-built relay or solid-state relay (SSR) module rather than bare wires spliced into a mains cable — the module's PCB keeps the mains-side terminals separated from the low-voltage side by a proper isolation gap, which is far safer than any improvised wiring. Once the circuit is tested and working, house the relay and its mains-side wiring inside an enclosure (a plastic project box is enough) so the live terminals are never exposed to accidental touch, especially if the finished plug will sit somewhere a child or a pet has access to it. If you are not confident about any of this, buy a certified, ready-made WiFi smart plug instead of wiring mains power yourself — the electronics side of this project is beginner-friendly, but the mains-wiring side is not a place to guess.

What can you control with a smart plug?

Anything that works by simply cutting or restoring its power — a lamp, a desk fan, a phone charger, string lights, a space heater, or an appliance you just want to schedule or switch off remotely instead of walking over to the switch. It will not work for anything that needs a specific shutdown sequence or loses its settings when power drops abruptly (some routers, or electronics with a soft power-off), and it is not a substitute for a purpose-built controller on anything that draws more current than the relay module is rated for — check the relay's rated load current against the appliance's before switching anything power-hungry through it.

Watch it built live

Rather than describe a generic parts list, we typed one real sentence into Compoden's AI build assistant on the storefront and let it pick parts, price them, and explain its reasoning — unscripted, screenshotted as it happened:

"I want to build a WiFi smart plug using an ESP8266 to control a lamp from my phone"

Compoden AI build assistant screenshot: WiFi smart plug parts tray for ESP8266 and relay module

It matched five in-stock parts on the first try (no follow-up question was needed) and explained the reasoning behind each one. On the microcontroller and relay specifically, it wrote:

"The NodeMCU ESP8266 Development Board is a small computer, called a microcontroller, that has built-in WiFi. It can connect to your home network and talk to your phone... This board is chosen because it combines a microcontroller with WiFi, which is essential for a smart plug."

"The 5V Single Channel Relay Module is a switch that can control the AC power going to your lamp. It has a digital interface, meaning it can be controlled by a simple on/off signal from the microcontroller... This relay is needed to safely turn the lamp's power on and off, as the microcontroller cannot directly handle the high voltage of a lamp."

It also flagged the one wiring detail worth double-checking before you connect anything: "the NodeMCU ESP8266 Development Board uses 3.3V logic, while the 5V Single Channel Relay Module uses 5V logic. This difference in voltage means you need to be careful when connecting them to ensure they communicate correctly without damaging either part." Most 5V relay modules sold for microcontroller use accept a 3.3V trigger signal as HIGH, but it is worth confirming on the specific module you buy rather than assuming.

Watch the firmware get generated

We then asked Compoden's build companion, Soldr, to write the actual control firmware: connect to WiFi, switch the relay on and off, and report status over serial.

Soldr firmware editor screenshot: generated ESP8266 WiFi smart plug relay-control firmware

Soldr matched the same five parts against its own catalog (5/5), then generated firmware built on the ESP8266WiFi and ESP8266WebServer libraries, hosting a small web server on the board itself to receive the on/off command. It described the result this way: "Here's the firmware for your ESP8266 WiFi smart plug. It connects to your WiFi network, hosts a simple web server to control the relay, and prints status messages to the serial monitor. The relay is connected to GPIO5 and is active-low, meaning LOW turns it ON." The generated source defines the relay pin with an explicit wiring comment — NodeMCU.GPIO5 -> Relay.IN, NodeMCU.GND -> Relay.GND, NodeMCU.5V -> Relay.VCC — and leaves placeholder YOUR_WIFI_SSID and YOUR_WIFI_PASSWORD defines for you to fill in with your own network credentials before flashing. Soldr's own instructions were direct about the remaining steps: "save it as an .ino file, replace YOUR_WIFI_SSID and YOUR_WIFI_PASSWORD with your actual WiFi credentials, then upload it to your NodeMCU ESP8266 board using the Arduino IDE. Install via Library Manager: ESP8266WiFi, ESP8266WebServer." The code appeared in the Firmware tab essentially all at once once generation finished, in about a minute — there was no visible character-by-character streaming to watch.

Who this build is for

This is a reasonable first WiFi project if you've already wired at least one basic circuit on a breadboard before, since the new territory here is WiFi credentials and mains safety rather than programming fundamentals. Soldr's own build profile for this exact tray rated it "Difficulty: Beginner (every part plugs in, no soldering)" and suitable for "roughly 10+", with the added note that "younger builders should have an adult handle the wall adapter." That guidance covers the low-voltage electronics; treat the mains-side relay wiring covered above as adult-supervised regardless of age, and budget a separate short session just for testing the relay's click and LED before you ever connect it to a live lamp.

Get everything in this build

The exact parts Soldr picked above, at today's live price and stock — each links straight to checkout, or add the whole tray in one click.

Part Qty Price
NodeMCU ESP8266 Development Board 1 ₹166 Add to cart →
5V Single Channel Relay Module 1 ₹45 Add to cart →
5V 2A Micro-USB Power Adapter 1 ₹140 Add to cart →
400-point solderless breadboard 1 ₹40 Add to cart →
23 AWG Multi-Strand Breadboard Wire 1 ₹10 Add to cart →
Total ₹401 Add all to cart →

Prices and stock verified live at the time this was written; Compoden's storefront always reflects the current price at checkout. This parts list does not include an enclosure or mains-rated cable and plug for housing the finished relay wiring — source those separately and see the safety section above before wiring anything to a live outlet.

Built and Backed by Compoden

Every part above ships from Compoden's own India stock, checked for compatibility before it's carded together. Delivery in 3–7 days across India, with COD and UPI available at checkout. This exact tray also carries over into Soldr.dev after purchase, already populated with these parts, where it generates the wiring diagram, firmware, and test steps for this specific hardware combination rather than a generic smart-plug guide. If a part doesn't perform as described, Compoden's support team will help you troubleshoot or replace it.

FAQ

Can an ESP8266 switch mains power directly?
No. An ESP8266's GPIO pins output 3.3V logic and cannot handle 220V mains current at all. A relay module sits between the ESP8266 and the appliance, using the ESP8266's low-voltage signal to control a magnetically or optically isolated switch on the mains side.

Is it dangerous to build a smart plug that switches mains power?
Yes, there is a real risk of electric shock on the relay's mains-side terminals, and it should only be attempted by someone comfortable working around mains electricity. Always unplug the relay before wiring it, use a pre-built relay or solid-state relay module rather than bare spliced wires, and enclose the finished wiring in a project box so the live terminals are never exposed to accidental touch.

How much does this exact build cost?
In a real live quote from Compoden's storefront, the five parts — a NodeMCU ESP8266 board, a 5V single-channel relay module, a 5V 2A power adapter, a breadboard, and jumper wire — came to ₹401 total.

Does the firmware need any special libraries?
Yes. The generated firmware uses the ESP8266WiFi and ESP8266WebServer libraries, both installable through the Arduino IDE's Library Manager, and it hosts a small web server directly on the board to receive the on/off command over your home network.

Back to blog