Intermediate IoT Project

Build a Smart Wi-Fi Plug

A smart plug allows you to control electrical appliances remotely using a smartphone, web browser or home automation platform. It can switch power on or off, schedule operating times, monitor energy consumption and integrate with voice assistants such as Google Assistant or Amazon Alexa. This project demonstrates how to build a smart plug using an ESP32, relay module and optional power-monitoring IC for a complete IoT power controller.

ESP32 Smart Plug

Project Overview

The ESP32 connects to the local Wi-Fi network and controls a relay that switches the mains power supplied to an appliance.

Users can operate the smart plug through a web interface, mobile application or MQTT server.

Advanced versions include energy monitoring, automatic scheduling, overload protection and integration with Home Assistant.

Project Difficulty

ItemValue
Difficulty⭐⭐⭐ Intermediate
Build Time4–8 Hours
Supply Voltage5 V DC (Controller)
Load Voltage110–240 V AC
ControllerESP32

Main Sections

Section Function
ESP32 Controls communication and switching.
Relay Module Switches the AC load.
Power Supply Converts mains voltage to 5 V or 3.3 V.
Power Monitoring Circuit (Optional) Measures voltage, current and power.
Push Button Allows manual switching.
Status LED Shows operating status.

Typical Components

QuantityComponent
1ESP32 Development Board
110 A or 16 A Relay Module
1HLK-PM01 or Similar AC-DC Power Module
1Push Button
1Status LED
1Fuse
1Power Socket and Plug
1Fire-Resistant Enclosure

Optional Energy Monitoring

Module Function
HLW8012 Voltage, current and power measurement.
PZEM-004T AC voltage, current, power and energy monitoring.
ADE7753 High-accuracy energy metering.
INA219 (DC Only) Voltage and current monitoring for DC loads.

How the Smart Plug Works

After power-up, the ESP32 connects to the configured Wi-Fi network.

The user can send commands through a web interface, smartphone app or MQTT broker.

The ESP32 energises or releases the relay, switching the connected appliance on or off.

If an energy-monitoring module is installed, the ESP32 continuously measures voltage, current, power and energy consumption and displays the information locally or remotely.

Typical Features

Feature Description
Remote ON/OFF Control appliances from anywhere.
Timer Automatic switching schedules.
Countdown Timer Turns appliances off after a preset time.
Energy Monitoring Displays power consumption.
Voice Control Works with compatible smart assistants.
Manual Override Operate without Wi-Fi.

Applications

  • Home automation.
  • Remote appliance control.
  • Lighting control.
  • Coffee machine scheduling.
  • Aquarium automation.
  • Holiday lighting.
  • Energy-saving systems.
  • Workshop equipment control.

Testing

  1. Verify all wiring before applying mains power.
  2. Test the low-voltage section independently.
  3. Connect the ESP32 to the Wi-Fi network.
  4. Verify manual relay operation.
  5. Test remote switching using the web interface.
  6. Measure relay switching under load.
  7. Verify timer functions.
  8. Check energy readings if monitoring is included.

Troubleshooting

Problem Possible Cause
Cannot connect to Wi-Fi Incorrect network settings or weak signal.
Relay does not switch Incorrect GPIO configuration or relay wiring.
Incorrect power readings Calibration required or sensor wiring fault.
ESP32 keeps restarting Power supply cannot provide sufficient current.
Relay chatters Insufficient supply voltage or software error.

Project Improvements

  • Add Bluetooth configuration.
  • Integrate with Home Assistant using MQTT.
  • Support OTA firmware updates.
  • Add overload and over-temperature protection.
  • Log energy consumption to a MicroSD card.
  • Create a smartphone dashboard.
  • Support multiple independently controlled sockets.
  • Add voice control integration.

Skills Learned

  • ESP32 programming.
  • Relay control.
  • Wi-Fi networking.
  • Energy monitoring.
  • IoT communications.
  • Home automation.

Safety Notes

  • Extreme care is required when working with mains voltage.
  • Always disconnect the power before modifying the circuit.
  • Use a properly rated fuse to protect against overloads.
  • Ensure adequate clearance and insulation between mains and low-voltage circuits.
  • Use a flame-retardant enclosure and relay rated for the intended load.
  • Test the project with low-power loads before connecting high-power appliances.

Key Points

  • A smart plug enables remote control of electrical appliances over Wi-Fi.
  • An ESP32 provides processing power and wireless connectivity.
  • Relay modules safely switch AC loads when correctly designed.
  • Energy monitoring helps identify high-consumption appliances and reduce electricity costs.
  • Smart plugs are among the most practical and popular IoT projects for home automation.

Next Project

Continue by building a Smart Energy Meter, capable of monitoring household power consumption, logging energy usage and displaying real-time statistics through a web dashboard.

Next Project → Smart Energy Meter