Level: Advanced Board: ESP32 DevKit V1 (WROOM-32) Power & blackouts
⚠ Safety Measurement of mains uses PZEM-004T: follow its safety rules. DC measurement must be fused.
What you need Buy the parts kit · Estimated total: $50.50
ESP32 DevKit V1 (30 pin, WROOM-32) × 1
$4.00
PZEM-004T v3.0 (100A, coil) × 1
$9.00
Orange Pi Zero 3 (2 GB) × 1
$25.00
microSD 32 GB A1 (industrial / high endurance) × 1
$8.00
Plastic project enclosure × 1
$3.00
Inline fuse holder + fuses × 1
$1.50
Some links are affiliate links: we may earn a commission at no extra cost to you.
Libraries Arduino IDE → Tools → Manage Libraries, then install:
Code opi-offline-energy-monitor.ino Copy Download .ino
#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 <PubSubClient.h>
const char* MQTT_HOST = "192.168.1.10"; // IP of your Raspberry Pi / mini-PC
const uint16_t MQTT_PORT = 1883;
const char* MQTT_USER = "esp32";
const char* MQTT_PASS = "mqtt-password";
WiFiClient netClient;
PubSubClient mqtt(netClient);
bool mqttConnect(const char* clientId) {
mqtt.setServer(MQTT_HOST, MQTT_PORT);
uint32_t t0 = millis();
while (!mqtt.connected() && millis() - t0 < 10000) {
if (mqtt.connect(clientId, MQTT_USER, MQTT_PASS)) return true;
delay(500);
}
return mqtt.connected();
}
#include <PZEM004Tv30.h>
PZEM004Tv30 pzem(Serial2, 16, 17);
void setup() {
Serial.begin(115200);
connectWiFi();
mqttConnect("esp32-energy");
}
void loop() {
if (!mqtt.connected()) mqttConnect("esp32-energy");
mqtt.loop();
static uint32_t t = 0;
if (millis() - t > 5000) {
t = millis();
String s = String("{\"V\":") + pzem.voltage() + ",\"A\":" + pzem.current() + ",\"W\":" + pzem.power() + ",\"kWh\":" + pzem.energy() + "}";
mqtt.publish("home/energy/main", s.c_str());
}
}docker-compose.yml Copy Download .ino
services:
mosquitto:
image: eclipse-mosquitto:2
restart: unless-stopped
ports: ["1883:1883"]
volumes: ["./mosquitto:/mosquitto/config", "./mosquitto-data:/mosquitto/data"]
nodered:
image: nodered/node-red:latest
restart: unless-stopped
ports: ["1880:1880"]
volumes: ["./nodered:/data"]
node-red-note.txt Copy Download .ino
In Node-RED: mqtt in (topic home/energy/main) → json → function (daily kWh) → file / dashboard. Install node-red-dashboard from the palette.
Flash a ready firmware from the browser Paste your error into the AI Error Doctor
Build tips Run all services in Docker and keep a nightly backup to a USB drive. Keep one ESP32 per measuring point so a failure does not stop the others. ESP32, Arduino and mini-PC: who does what
← ← All projects
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.
Reads voltage, current, power and kWh with PZEM-004T and publishes them to a web page, MQTT or Home Assistant.
⛔ 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.
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.
⛔ 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.
Recognises the start and the end of an appliance cycle from its power draw and sends a message when it is finished, handy when power comes in windows.
Comments
No comments yet. Share your experience or ask a question.