Drei Wege, einen Zähler zu lesen
In den Niederlanden und Belgien liefert der P1-Port ein Text-Telegramm mit seriellem Signal bei 115200 Baud, und bei vielen DSMR-Versionen ist das Signal invertiert, was die UART des ESP32 per Software beherrscht. In Deutschland liefert ein optischer Lesekopf an einem Zähler mit SML ähnliche Daten. Typische Werte sind der Gesamtverbrauch (OBIS 1.8.0) und die Momentanleistung (16.7.0).
Wenn es keinen Port gibt: die Impuls-LED zählen
Viele Zähler haben keinen P1-Port, aber fast alle haben eine LED, die eine feste Zahl Mal pro kWh blinkt, zum Beispiel 1000 oder 1600 Impulse, auf dem Typenschild aufgedruckt. Ein IR-Kopf oder eine Fotodiode auf der LED zählt die Impulse, und der ESP32 macht daraus Leistung und kWh.
Nutzen Sie einen Interrupt am Pin und entprellen Sie einige Millisekunden.
Die Zeit zwischen Impulsen ergibt die Momentanleistung: Ein Blinken alle 3,6 Sekunden bei einem Zähler mit 1000 Imp/kWh entspricht 1 kW.
Nehmen Sie ein 5-V-P1-Kabel mit Pegelwandler und befestigen Sie den optischen Kopf mit Magnet oder Klammer, damit er an Ort und Stelle bleibt.
Komponenten
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)
Verdrahtung
Modul
Modul-Pin
ESP32-Pin
Hinweis
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;
}
}
Bautipps
Die Datenleitung ist Open-Collector und invertiert; am RX wird ein 10-kΩ-Pull-up auf 3,3 V gebraucht.
Der Sketch prüft die CRC nicht. Optische SML-Zähler brauchen stattdessen eine binäre SML-Bibliothek.
Kommentare
Noch keine Kommentare. Teilen Sie Ihre Erfahrung oder stellen Sie eine Frage.