Ragel-based C-parser for Dutch Smart Meter P1-data
Author: Levien van Zon (levien at gnuritas.org)
DSMR (Dutch Smart Meter Requirements) is a standard for "smart" utility meters used in The Netherlands. Meters that comply with DSMR have several interfaces, including:
In addition to the above interfaces, most DSMR-meters also have additional interfaces, such as an optical IEC 62056-21 serial interface and an S0 (DIN 43864) pulse-output (which is usually not accessible, unfortunately).
Several versions of the official DSMR P1 standard are included in the doc/
directory of this repository. Also included there are notes on the broader IEC 62056-21 standard, on which the P1-standard is based.
This repository contains a parser for DSMR data-telegrams, based on the Ragel state machine compiler, as well as the DSMR P1 specification documents (in doc/
) and an example program in C for reading and parsing DSMR-data from a serial port in Linux (on any other POSIX system such as BSD or MacOS). To compile the Ragel parser and the example program, you need to install the Ragel state machine compiler (on Debian, Ubuntu and derivatives, simply do: sudo apt-get install ragel
, on other systems you can git clone git://colm.net/ragel.git
) and run ./make.sh
. In principle this parser can also be adapted for use on microcontrollers, although I haven't tried this yet.
In addition to DSMR P1-telegrams, the parser can handle more general IEC 62056-21 telegram data, albeit with a very limited set of OBIS-objects, so that it can be used to obtain energy readings from many non-DSMR smart meters through the optical port. The parser has been tested with example data from all currently known versions of DSMR (2.2, 3.0, 4.x and 5.0.2), and with real data from a Landis-Gyr and Kaifa DSMR 4.2 electricity meters, in some cases with a slave gas meter connected. This code is open-source under the Apache 2.0 licence.
The DSMR P1-port uses an RJ12 (6P6C) modular connector, which has 6 pins:
All P1-pins are galvanically isolated from mains through an opto-coupler, and have over-voltage protection. The data-line is an open-collector serial line output, which means that the signal is inverted relative to a normal serial line, and that it requires a pull-up resistor (1 - 10 kOhm) to be connected between RXD (pin 5) and the serial voltage required (for +5V you can connect it to Vcc / pin 1 or RTS / pin 2 on the P1-port).
If RTS is set to HIGH, an ASCII text-based data-packet called a "telegram" is sent by the smart meter, either every 10 seconds (DSMR < 5.0) or every second (DSMR >= 5.0). The format of the telegrams is derived from the IEC 62056-61 standard for exchanging data with utility meters.
There are roughly three ways to connect the P1-port to a computing device:
You can also get a prefabricated P1 to USB cable (easily ordered online, e.g. from SOS Solutions, slimmemeterkabel.nl, Sinforcon or Oloey). So far, every USB RS232-converter I tested worked, and most Linux-kernels support the more common converter chips out-of-the-box, and will make the data available on the /dev/ttyUSB0
serial device.
If you build your own cable, it's easiest to connect pins 1 and 2 of the P1-interface together, and to connect a 1-10 kOhm pull-up resistor between either of these and pin 5 (TXD). Then simply connect RXD of your serial interface to TXD on the P1-port, and connect the data ground on both interfaces. You may also have to connect pin 6 (VccGND) to the data ground (pin 3) for the cable to work on some meters. Cables can be made without soldering, e.g. by using Wago 221-412 splicing connectors. I've used a 4-port USB-interface with these DB9 female terminal block connectors to read up to four meters at once. I made the cables using three-pair UTP-cable and RJ12-connectors, with a 1 kOhm pull-up resistor and several splicing connectors neatly packed away in a small junction box.
By default, the software echoes P1-telegrams back to the serial interface, so you can connect other devices that also read P1-data. If you're using RS232, you will need to convert the ca. 15V signal on the TXD-pin to 5V (e.g. using a L7805 linear converter), before handing it on to another device. If you're using a TTL serial interface, you will need to invert the TXD signal using a transistor or inverter IC. Especially if you're using a 3.3V serial interface, it's probably easiest to use an inverter IC with an open collector output, such as the SN7406, or an NPN-transistor to invert the signal and create an open collector output.
If you're connecting the P1-interface directly to the serial pins on a Raspberry Pi using a transistor, you may need to configure your Raspberry Pi Linux system to leave the serial port alone. This involves removing the references to the serial device (/dev/ttyAMA0
) in /boot/cmdline.txt
(disable the kernel debugging output to serial) and /etc/inittab
(disable the serial command shell), instructions on how to do this can be found in this guide.
To test if data is coming in and the signal is inverted correctly, just compile and run the test program, and check if it sees any valid telegrams.
On Raspbian and some other systems, you may have to install a C-compiler to compile the parser and the test program, e.g. sudo apt-get install build-essential
. You may also have to install Ragel: sudo apt-get install ragel
. Then clone the git-repository, compile the test program and run it:
git clone https://github.com/lvzon/dsmr-p1-parser.git
cd dsmr-p1-parser
./make.sh
./p1-test-p1 /dev/ttyUSB0 errors.dat
If valid telegrams are coming in, you should see something like this within about 10 seconds:
Input device seems to be a serial terminal
Possible telegram found at offset 0
Possible telegram end at offset 624
New-style telegram with length 630
Header: XMX5LGBBFG1012463538
P1 version: 4.2
Timestamp: 1527686221
Equipment ID: E0031003262xxxxxx
Energy in, tariff 1: 1234.000000 kWh
Energy in, tariff 2: 1235.000000 kWh
Energy out, tariff 1: 0.000000 kWh
Energy out, tariff 2: 0.000000 kWh
Tariff: 2
Power in: 0.048000 kW
Power out: 0.000000 kW
Power failures: 2
Long power failures: 1
Power failure events: 1
Power failure event at 1484629982, 6885 s
Voltage sags L1: 0
Voltage swells L1: 0
Current L1: 0 A
Power in L1: 0.048000 kW
Power out L1: 0.000000 kW
Device 1 type: 3
Device 1 ID: G0025003407xxxxxx
Device 1 counter at 1527685200: 123.000000 m3
CRC: 0x9b8d
Parsing successful, data CRC 0x9b8d, telegram CRC 0x9b8d
If you get an error, check if the serial converter is connected and you're using the the correct serial device (depending on your setup it can also be /dev/ttyAMA0
, /dev/ttyS0
, /dev/ttyUSB1
or something else, check dmesg
to be sure). If no valid telegram is seen within 25 seconds or so, hit CTRL-C
and check errors.dat
. The program tries to autodetect the baud rate, so if errors.dat
contains garbage, you probably forgot to invert the signal, or you're inverting it twice. If errors.dat
is empty, try dumping the serial device data directly (e.g. cat /dev/ttyUSB1
). If no data comes in, check your cable connections and especially check if your data-line and ground and pull-up resistor are all connected correctly and the request-pin 2 is connected to at least +4V (and at most 5.5V).
If the cable-length is more than a few metres, this can cause the voltages to drop below 4 V, so you may need to measure this and either use a better cable or connect the pull-up resistor and Vcc-RTS at the P1-side rather than at the serial interface. Also note that older (DSMR 2.x or 3.x) metres do not have a 5V Vcc pin, so in this case you'll need to supply 5V or 3.3V from another source.
The packmsg
-branch contains an experimental application that reads telegrams from a serial device and echoes the telegrams back to the interface. Telegram data is parsed, and energy, power and gas data is sent directly to a server socket, using a very compact encoding based on MessagePack. This application is functional but still under development, I use it in a pilot project to collect and act upon smart meter data in real time. I will try to include an example server and more documentation soon.
I wrote this parser for use at Lens, because I needed something that was light-weight and complete, and there currently does not seem to be another full DSMR P1 telegram-parser that is open-source and can be used in regular C programs. However, there are many parsers in many other programming languages:
For more information on interfacing with the P1-port (mostly in Dutch):