Building a smart plug with energy monitoring using ESP32 and Blynk gives you a powerful tool to track, control, and optimise your appliance power consumption. Unlike commercial smart plugs that are limited to simple on/off control, a DIY ESP32 smart plug with PZEM-004T measures real-time voltage, current, wattage, and cumulative energy — giving you data that is directly usable for calculating your electricity bill and identifying energy hogs. This project costs Rs 800-1,200 to build versus Rs 1,500-3,000 for commercial energy-monitoring smart plugs.
Table of Contents
- Components and BOM
- Safety Guidelines for 230V Projects
- Circuit Design
- ESP32 Code with Blynk
- Blynk Dashboard Setup
- Alternative: MQTT to Home Assistant
- Indian Electricity Bill Calculator
- Frequently Asked Questions
Components and BOM
- ESP32 DevKit V1 — Rs 200-350
- PZEM-004T V3.0 energy monitoring module — Rs 400-600
- 5V 1-channel relay module — Rs 50-80
- HiLink HLK-PM03 3.3V or PM05 5V power module — Rs 150-250
- Indian 5A socket and plug (for enclosure outlets) — Rs 80-120
- ABS enclosure (suitable for mains wiring) — Rs 150-250
- Terminal blocks and 10A wire — Rs 80-120
Total: Rs 1,110-1,770. Cheaper than most commercial energy monitoring smart plugs with far more data.
Safety Guidelines for 230V Projects
- Always work with power disconnected from mains
- Use ABS or fireproof enclosure rated for mains use
- PZEM module must be connected via its CT clamp only — never connect high-voltage terminals to ESP32
- All mains connections must be properly insulated
- Add a 5A fuse in series with the load circuit
- Earthing: connect enclosure to earth if metal
Circuit Design
Power Section (230V Mains):
Input Live (L) -- [5A fuse] -- Relay COM
Relay NO -- PZEM CT wire -- Output Socket Live
Input Neutral -- Output Socket Neutral
Input Earth -- Output Socket Earth
PZEM-004T:
- Input clamps (L, N) connect to INPUT mains live and neutral
- Output terminal monitors the switched load
- CT clamp wraps around the OUTPUT live wire
- PZEM RX/TX connect to ESP32 Serial2
HiLink Power Module:
- L and N inputs from mains
- 5V output to relay VCC and ESP32 VIN
- GND output to common ground
ESP32 Connections:
GPIO26 -> Relay IN (control)
GPIO16 -> PZEM RX
GPIO17 -> PZEM TX
VIN <- 5V from HiLink
GND <- HiLink GND
ESP32 Code with Blynk
#define BLYNK_TEMPLATE_ID "TMPL_XXXXXX"
#define BLYNK_TEMPLATE_NAME "SmartPlug"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <PZEM004Tv30.h>
const char* ssid = "YOUR_WIFI";
const char* pass = "YOUR_PASS";
#define RELAY_PIN 26
PZEM004Tv30 pzem(Serial2, 16, 17);
BlynkTimer timer;
void sendEnergyData() {
float voltage = pzem.voltage();
float current = pzem.current();
float power = pzem.power();
float energy = pzem.energy(); // kWh accumulated
float frequency = pzem.frequency();
float pf = pzem.pf();
if (!isnan(voltage)) {
Blynk.virtualWrite(V0, voltage); // Volts
Blynk.virtualWrite(V1, current); // Amps
Blynk.virtualWrite(V2, power); // Watts
Blynk.virtualWrite(V3, energy); // kWh
Blynk.virtualWrite(V4, frequency); // Hz
Blynk.virtualWrite(V5, pf); // Power factor
// Calculate bill contribution
float ratePerUnit = 7.50; // Rs per kWh - adjust for your state
float dailyCost = energy * ratePerUnit;
Blynk.virtualWrite(V6, dailyCost);
}
}
// Control relay from Blynk app
BLYNK_WRITE(V7) {
int state = param.asInt();
digitalWrite(RELAY_PIN, state == 1 ? HIGH : LOW);
}
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Start OFF
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(5000L, sendEnergyData); // Every 5 seconds
}
void loop() {
Blynk.run();
timer.run();
}
Blynk Dashboard Setup
- Create a Blynk account at blynk.io
- Create a new template: “SmartPlug”
- Add datastreams for V0-V7
- Create a Web Dashboard with:
- Gauge (V0): Voltage display (220-240V range)
- Gauge (V1): Current (0-15A)
- Gauge (V2): Power (0-2300W)
- Chart (V3): Energy over time (kWh history)
- Label (V6): Daily cost in Rs
- Button (V7): ON/OFF relay control
- Create matching mobile dashboard in the Blynk app
Alternative: MQTT to Home Assistant
Replace Blynk with MQTT for local, cloud-free control:
#include <PubSubClient.h>
WiFiClient wifiClient;
PubSubClient mqtt(wifiClient);
void publishData() {
mqtt.publish("smartplug/voltage", String(pzem.voltage()).c_str());
mqtt.publish("smartplug/power", String(pzem.power()).c_str());
mqtt.publish("smartplug/energy", String(pzem.energy()).c_str());
}
mqtt.subscribe("smartplug/relay/set");
// Receive ON/OFF from Home Assistant
Indian Electricity Bill Calculator
Use the energy data to calculate actual running costs for each appliance:
// Indian electricity tariff rates (2026 approximate)
// Adjust for your state and DISCOM:
float calculateMonthlyCost(float dailyKwh) {
float monthlyKwh = dailyKwh * 30;
float cost = 0;
// Example: Maharashtra slab rates
if (monthlyKwh <= 100) cost = monthlyKwh * 2.15;
else if (monthlyKwh <= 300) cost = 100 * 2.15 + (monthlyKwh - 100) * 5.83;
else cost = 100 * 2.15 + 200 * 5.83 + (monthlyKwh - 300) * 7.97;
return cost;
}
Frequently Asked Questions
How accurate is PZEM-004T for Indian electricity billing?
The PZEM-004T has 0.5% voltage accuracy and 1% power accuracy — excellent for home monitoring. The accumulated kWh reading is calibrated but may drift slightly from your official DISCOM meter over time due to measurement methodology differences. Use it for relative consumption comparison rather than exact billing verification.
What is the maximum load this smart plug can handle?
Design it for 6A (1,380W at 230V) using the standard 5A relay and PZEM CT clamp. For heavier loads (geyser, AC, induction cooktop), use a 16A relay and the 100A CT version of PZEM-004T with external CT clamp, designing the enclosure and wiring for 16A continuously.
Can I reset the energy counter without losing accumulated data?
The PZEM-004T has a Modbus command to reset the energy counter (send register 0x42 with value 0). Implement a Blynk button (V8) mapped to this command for easy daily or weekly energy counter reset. Home Assistant can trigger this reset via an automation at midnight for daily energy tracking.
Add comment