Three ways to read a meter, and one that is common in Ukraine
In the Netherlands and Belgium the P1 port gives a text telegram with a serial signal at 115200 baud, and in many DSMR versions the signal is inverted, which the ESP32 UART can handle in software. In Germany an optical head on a meter with SML gives the same type of data. Typical values include the total consumption (OBIS 1.8.0) and the instantaneous power (16.7.0).
The Ukrainian case: count the LED
Most Ukrainian meters have no P1 port, but nearly all have an LED that blinks a fixed number of times per kWh, such as 1000 or 1600 impulses, printed on the label. An IR or a photodiode head on the LED counts the pulses, and the ESP32 turns them into power and kWh.
Use an interrupt on the pin and debounce for a few milliseconds.
The time between pulses gives the instantaneous power: one blink every 3.6 seconds on a 1000 imp/kWh meter is 1 kW.
Use a 5 V P1 cable with a level shifter, and mount the optical head with a magnet or a clip so it stays in place.
Components
ESP32-C3 SuperMini
P1 port cable (RJ12) for smart meter
Optical IR read head for smart meters (SML/D0)
Logic level shifter 3.3V/5V (4-channel)
Dupont jumper wires (M-F, M-M)
Wiring
Module
Module pin
ESP32 pin
Note
P1 RJ12 pin 6 (GND)
GND
pin numbers: 1 = 5 V, 2 = request, 3 = GND data, 5 = data, 6 = GND
P1 RJ12 pin 5 (data)
open collector, inverted
GPIO4 (RX) + 10 kΩ pull-up to 3V3
the sketch inverts the UART signal
P1 RJ12 pin 2 (request)
to 5 V
pin 1 (5 V) via the cable
the meter sends a telegram every second or 10 s while pin 2 is high
Power
5V
from P1 pin 1 (max 250 mA) or USB
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 <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>
<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();
}
// DSMR / P1 reader: parses the OBIS codes for total import, total export and current power.
// This sketch does NOT verify the telegram CRC. Optical SML heads (many German meters) use a binary
// protocol and need a library such as libSML or ESPHome's sml component instead.
const int PIN_RX = 4;
float imp0 = NAN, imp1 = NAN, imp2 = NAN, exT0 = NAN, exT1 = NAN, exT2 = NAN, powerNowKw = NAN; String line;
float obisValue(const String& l) {
int a = l.indexOf('('), b = l.indexOf('*');
if (a < 0 || b < a) return NAN;
return l.substring(a + 1, b).toFloat();
}
float sumOf(float total, float t1, float t2) { return !isnan(total) ? total : ((isnan(t1) ? 0 : t1) + (isnan(t2) ? 0 : t2)); }
String buildJson() { return String("{"import_kwh":") + num(sumOf(imp0, imp1, imp2), 3) + ","export_kwh":" + num(sumOf(exT0, exT1, exT2), 3) + ","power_kw":" + num(powerNowKw, 3) + "}"; }
void handleLine(const String& l) {
if (l.startsWith("1-0:1.8.0")) imp0 = obisValue(l);
else if (l.startsWith("1-0:1.8.1")) imp1 = obisValue(l);
else if (l.startsWith("1-0:1.8.2")) imp2 = obisValue(l);
else if (l.startsWith("1-0:2.8.0")) exT0 = obisValue(l);
else if (l.startsWith("1-0:2.8.1")) exT1 = obisValue(l);
else if (l.startsWith("1-0:2.8.2")) exT2 = obisValue(l);
else if (l.startsWith("1-0:1.7.0")) powerNowKw = obisValue(l);
}
void setup() {
Serial.begin(115200);
Serial1.begin(115200, SERIAL_8N1, PIN_RX, -1, true); // last argument: inverted signal. Many DSMR 4 meters use 115200; older ones 9600.
connectWiFi(); startWeb();
}
void loop() {
server.handleClient();
while (Serial1.available()) {
char c = Serial1.read();
if (c == 'n') { line.trim(); if (line.length()) handleLine(line); line = ""; }
else if (line.length() < 120) line += c;
}
}
Build tips
The data line is open-collector and inverted; a 10 kΩ pull-up to 3.3 V is needed on RX.
The sketch does not check the CRC. Optical SML meters need a binary SML library instead.
Comments
No comments yet. Share your experience or ask a question.