Smart energy meter

Reads voltage, current, power and kWh with PZEM-004T and publishes them to a web page, MQTT or Home Assistant.

Power & batteries Board: ESP32 DevKit V1 (WROOM-32) Level: ●●○ ⏱ 2–4 h

⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

Your own meter, with your own tariff

The meter on the wall gives you one number a month. The PZEM-004T v3 gives voltage, current, power, frequency, power factor and accumulated energy once a second, and the ESP32 can turn it into daily kWh and a cost, including a night zone if your tariff has one.

Practical notes

The module talks Modbus-RTU over UART at 9600 baud and keeps the energy counter in its own non-volatile memory, so it survives restarts of the ESP32. Read it once or twice a second; faster polling only produces errors. The serial side is optically isolated, but its TX line is driven from the 5 V side, so put a divider or a level shifter before the ESP32 RX pin.

  • The 10 A version uses an internal shunt, the 100 A version uses a split-core current transformer that must clamp one conductor only, never both.
  • Stated accuracy is about 1 %, good for trends and costs, not for billing disputes.
  • Save a snapshot of the counter at midnight, then daily consumption is a simple difference.

Components

  • ESP32 DevKit V1 (30 pin, WROOM-32)
  • PZEM-004T v3.0 (100A, coil)
  • Plastic project enclosure
  • Lever connectors (WAGO type) 2/3-pin
  • Inline fuse holder + fuses

Wiring

Module Module pin ESP32 pin Note
PZEM-004T 5V / GND 5V / GND the logic side is optically isolated
PZEM-004T TX UART GPIO16 (RX2)
PZEM-004T RX UART GPIO17 (TX2)
PZEM AC terminals + CT mains L / N via fuse qualified person, closed enclosure

Libraries

  • PZEM004Tv30 (mandulaj)

Code

#include <WiFi.h>

const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_PASSWORD";

bool connectWiFi(uint32_t timeoutMs = 20000) {
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  uint32_t t0 = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - t0 < timeoutMs) {
    delay(250);
    Serial.print(".");
  }
  Serial.println();
  if (WiFi.status() == WL_CONNECTED) {
    Serial.print("IP: ");
    Serial.println(WiFi.localIP());
    return true;
  }
  return false;
}
#include <PZEM004Tv30.h>
PZEM004Tv30 pzem(Serial2, 16, 17);
#include <WebServer.h>

WebServer server(80);
String buildJson();   // implemented in the project below

String num(float v, int d = 1) { return isnan(v) ? String("null") : String(v, d); }

const char PAGE_HTML[] PROGMEM = R"rawliteral(
<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>ESP32</title>
<style>body{font-family:system-ui;background:#0b0f14;color:#e6edf3;margin:20px}table{border-collapse:collapse}td{padding:6px 14px;border-bottom:1px solid #223}button,input{font-size:16px;padding:8px 14px;margin:4px;border-radius:6px;border:1px solid #345;background:#16202b;color:#e6edf3}</style>
</head><body><h2>ESP32</h2><table id="t"></table>
float V, A, W, kWh, F, PF;
String buildJson() { return String("{"V":") + num(V) + ","A":" + num(A, 2) + ","W":" + num(W, 0) + ","kWh":" + num(kWh, 3) + ","Hz":" + num(F) + ","PF":" + num(PF, 2) + "}"; }

<script>
function load(){fetch('/json').then(r=>r.json()).then(d=>{let h='';for(const k in d){h+='<tr><td>'+k+'</td><td>'+d[k]+'</td></tr>';}document.getElementById('t').innerHTML=h;});}
load();setInterval(load,2000);
</script></body></html>
)rawliteral";

void startWeb() {
  server.on("/", []() { server.send_P(200, "text/html", PAGE_HTML); });
  server.on("/json", []() { server.send(200, "application/json", buildJson()); });
  server.begin();
}
void setup() {
  Serial.begin(115200);
  connectWiFi();
  startWeb();
}
void loop() {
  server.handleClient();
  static uint32_t t = 0;
  if (millis() - t > 2000) { t = millis(); V = pzem.voltage(); A = pzem.current(); W = pzem.power(); kWh = pzem.energy(); F = pzem.frequency(); PF = pzem.pf(); }
}

Build tips

  • Use the coil (clamp) version so you do not cut the live wire.
  • Divide the PZEM TX line with 1 k / 2 k before the ESP32 RX pin.

What you need

Buy the parts kit · Estimated total: $20.00

ESP32 DevKit V1 (30 pin, WROOM-32) × 1
$4.00
Buy at AliExpress Buy at Amazon.com
PZEM-004T v3.0 (100A, coil) × 1
$9.00
Buy at AliExpress Buy at Amazon.com
Plastic project enclosure × 1
$3.00
Buy at AliExpress Buy at Amazon.com
Lever connectors (WAGO type) 2/3-pin × 1
$2.50
Buy at AliExpress Buy at Amazon.com
Inline fuse holder + fuses × 1
$1.50
Buy at AliExpress Buy at Amazon.com
🇺🇦 Ukraine🌍 Other countries

Ukraine: we buy and sell these parts ourselves, order them on this site.

Some links are affiliate links: we may earn a commission at no extra cost to you.

Related ideas

⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

Balcony solar power monitor

Measures output and yield of a plug-in solar kit with a PZEM meter and shows it on a web page or in Home Assistant.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●○ Intermediate
  • Build time 2–4 h
Pick parts / kit
⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

Mains voltage monitor

Measures 220 V mains through an isolated ZMPT101B module and warns about sags, surges and blackouts in Telegram or Home Assistant.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●○ Intermediate
  • Build time 2–3 h
Pick parts / kit
⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

Inverter load manager

Watches the inverter / battery current and switches off non-critical loads with a relay when the battery runs low.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●● Advanced
  • Build time 3–5 h
Pick parts / kit

← All module ideas →

Comments

No comments yet. Share your experience or ask a question.

Comments are moderated.