Trzy sposoby odczytu licznika i jeden, który działa prawie wszędzie
W Holandii i Belgii port P1 daje tekstowy telegram sygnałem szeregowym 115200 baud, a w wielu wersjach DSMR sygnał jest odwrócony, co UART ESP32 potrafi programowo. W Niemczech głowica optyczna na liczniku z SML daje podobne dane. Typowe wartości to zużycie całkowite (OBIS 1.8.0) i moc chwilowa (16.7.0).
Gdy nie ma portu: licz diodę
Wiele liczników nie ma portu P1, ale prawie wszystkie mają diodę migającą stałą liczbę razy na kWh, np. 1000 lub 1600 impulsów, wydrukowaną na tabliczce. Głowica IR lub fotodioda na diodzie zlicza impulsy, a ESP32 zamienia je w moc i kWh.
Użyj przerwania na pinie i eliminuj drgania przez kilka milisekund.
Czas między impulsami daje moc chwilową: jedno mignięcie co 3,6 sekundy na liczniku 1000 imp/kWh to 1 kW.
Weź kabel P1 5 V z konwerterem poziomów i mocuj głowicę optyczną magnesem lub klipsem, by trzymała się na miejscu.
Komponenty
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)
Schemat połączeń
Moduł
Pin modułu
Pin ESP32
Uwaga
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
Kod
#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;
}
}
Wskazówki montażowe
Linia danych jest z otwartym kolektorem i zanegowana; na RX potrzebny rezystor podciągający 10 kΩ do 3,3 V.
Szkic nie sprawdza CRC. Liczniki optyczne SML wymagają binarnej biblioteki SML.
Komentarze
Brak komentarzy. Podziel się doświadczeniem lub zadaj pytanie.