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.
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
| Item | Value |
|---|---|
| Difficulty | ⭐⭐⭐ Intermediate |
| Build Time | 4–8 Hours |
| Supply Voltage | 5 V DC (Controller) |
| Load Voltage | 110–240 V AC |
| Controller | ESP32 |
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
| Quantity | Component |
|---|---|
| 1 | ESP32 Development Board |
| 1 | 10 A or 16 A Relay Module |
| 1 | HLK-PM01 or Similar AC-DC Power Module |
| 1 | Push Button |
| 1 | Status LED |
| 1 | Fuse |
| 1 | Power Socket and Plug |
| 1 | Fire-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
- Verify all wiring before applying mains power.
- Test the low-voltage section independently.
- Connect the ESP32 to the Wi-Fi network.
- Verify manual relay operation.
- Test remote switching using the web interface.
- Measure relay switching under load.
- Verify timer functions.
- 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.