Skip to content
isarriderPublic
forked from volkszaehler/mbmd

About

ModBus Measurement Daemon - simple reading of data from ModBus meters and grid inverters

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

ModBus Measurement Daemon

Build Status

A daemon for collecting measurement data from smart meters and grid inverters over modbus.

mbmd provides an http interface to smart meters and grid inverters with modbus interface. Meter readings are made accessible through REST API and MQTT. Modbus communication is possible over RS485 connections as well as TCP sockets.

mbmd was originally developer by Mathias Dalheimer under the name of gosdm. Previous releases are still available.

About this fork

This is isarrider/mbmd, a fork of volkszaehler/mbmd. It follows upstream master and adds the changes below, which are not in upstream yet. This fork's master contains all of them, and the Docker image ghcr.io/isarrider/mbmd is built from it. Each change also lives on its own branch, from which it is (or is meant to be) proposed upstream. Once upstream merges a change, its row is removed here.

Change Files Upstream
DS100 driver brought in line with the manual. New values: L-L voltages L1-L2/L2-L3/L3-L1 and their average (0x0406–0x040E), L1/L2/L3 frequency (0x0432–0x0434), neutral current as new CurrentN measurement (0x0416), and forward, reverse and total reactive energy in total and per phase. Fixes: Export read 0x118A, which isn't in the register map, and now reads 0x0118; Sum and SumL1–SumL3 are read signed, so a negative total under import − export no longer shows up as ~42.9 million kWh. Tests pin the energy register addresses and check that the opcode table and the values read agree. The README explains Sum per combination code and that currents read negative while exporting. meters/rs485/ds100.go, meters/rs485/ds100_test.go, meters/measurements.go, meters/measurement_enumer.go, README.md #411
A rejected register no longer takes the meter offline. If a meter answers a read with an exception (illegal function, address or value), the register is skipped and the other values are still read. After three rejections in a row it is not read again until restart. Each rejection and the final decision are logged with the device id. Timeouts and a busy meter still take the meter offline as before. meters/rs485/rs485.go, server/handler.go fork only, branch feat/rs485-skip-failing-ops
Block reads. Meter types can declare how many registers they read in one request and how many unused registers may be read across a gap, and neighbouring values are then read together. If the meter rejects a block, its values are read one by one from then on, which is logged. The DS100 reads up to 100 registers per request with gaps of up to 8, so its 145 values take 7 requests. meters/rs485/rs485.go, meters/rs485/producer.go, meters/rs485/ds100.go fork only, branch feat/rs485-block-reads
88 new generic measurements, each in total and per phase: demand and maximum demand for import, export and total active (W) and reactive power (var), reactive energy per quadrant Q1–Q4 (kvarh), and resettable active and reactive energy counters. With rows in the web UI and tests for descriptions, names and MQTT topics. meters/measurements.go, meters/measurement_enumer.go, assets/index.html fork only, branch feat/measurements-demand-quadrant-resettable
DS100 reads every measured value it has: demand and maximum demand (0x043A–0x0498), reactive energy per quadrant and the resettable counters (0x062C–0x065A), in addition to the values above. meters/rs485/ds100.go, meters/rs485/ds100_test.go, README.md fork only, branch feat/ds100-all-values
Serial number and firmware version for RS485 meters. Read once on the first query, shown in the status API and web UI and by mbmd scan (see the next two rows), and logged, e.g. device DS1001.1: B+G e-tech DS100-00B, serial 202405270001, version V301 (hardware V301). Failures are logged and retried at most three times. The DS100 reads its serial (0x1000), software and hardware version, and identifies the model variant from the firmware checksum, e.g. "B+G e-tech DS100-00B". meters/rs485/rs485.go, meters/rs485/producer.go, meters/rs485/ds100.go fork only, branch feat/rs485-serial-version
Model, serial number and version in the status API and web UI. The status table in the web UI gets a column for each; empty values are left out of the API. server/status.go, assets/index.html fork only, branch feat/status-device-details
mbmd scan shows serial number and version of found meters. Scan also kept one device object per meter type for every address, so it showed the details of the last meter found for all meters of that type. It now keeps each meter's details and lists them sorted by address. cmd/scan.go, meters/rs485/rs485.go fork only, branch feat/scan-device-details
The web UI shows every measurement. 17 values had no row: per-phase frequency, line-to-line voltages and their average, the average L-N voltage, the three-phase vector current, the cabinet temperature, and inverter string energy and string 4. Adds a neutral current row too, and a test that fails if a measurement has no row. assets/index.html, server/assets_test.go fork only, branch feat/ui-missing-rows
InfluxDB write buffering, with tests. --influx-buffer / influx.buffer keeps unsent readings in memory and retries for that long while InfluxDB is down, e.g. 1h. The default 0 keeps the client's behaviour of 5 retries within 3 minutes, which holds about 10 seconds of data. The code is andig's implementation from #406, unchanged. Added on top: unit tests for the buffer options, an outage test against a fake InfluxDB that checks every reading arrives once the database is back, one that shows data loss without a buffer, and the wording above in the flag help and example config. server/influx.go, server/influx_test.go, cmd/run.go, cmd/confighandler.go, mbmd.dist.yaml, docs/mbmd_run.md #413, from branch feat/influx-buffer. andig closed his #406 with "Not gonna merge. A parameter is too ugly to do.", so this may stay fork-only.
Docker images published from the fork. The image name is taken from the repository, so this fork publishes to ghcr.io/isarrider/mbmd instead of ghcr.io/volkszaehler/mbmd. .github/workflows/docker.yml fork only

The fork-only branches start from upstream master or from the branch they need, so each can be proposed upstream as is, in this order: feat/rs485-skip-failing-ops, feat/rs485-block-reads, feat/measurements-demand-quadrant-resettable, then feat/ds100-all-values once #411 is merged, then feat/rs485-serial-version and feat/scan-device-details. feat/ui-missing-rows, feat/status-device-details and feat/influx-buffer do not depend on the others. The new DS100 registers come from the manual and have not been checked on a real meter yet.

Upstream PR history

PR What State
#358 Add B+G e-tech DS100 merged
#359 More readings in measurements.go merged
#385 DS100 line voltages and frequencies, opened from this fork's master closed, continued in #411
#409 DS100 neutral current closed, included in #411
#410 DS100 line voltages and per-phase frequency closed, included in #411
#408 Own InfluxDB write buffering closed, superseded by #413
#412 Tests for andig's #406 closed, included in #413
#411 DS100 driver brought in line with the manual open
#413 InfluxDB write buffering (andig's #406) with tests open

To see the exact difference against upstream: compare volkszaehler:master...isarrider:master.

Table of Contents

Requirements

You'll need:

  • A supported Modbus/RTU smart meter OR an supported Modbus/TCP SunSpec-compatible grid inverter.
  • In case of Modbus/RTU: A USB RS485 adapter. See USB-ISO-RS485 project for a home-grown adapter.
  • Optionally an RS485 to Ethernet converter (see SO discussion)

Installation

Using the precompiled binaries

Precompiled release packages are available. Download the right package for the target platform and unzip.

Building from source

mbmd is developed in Go. To build from source two steps are needed:

  • use make install to install the build tools (make sure $GOPATH/bin is part of the path to make the installed tools accessible for the next step)
  • then run make build which creates the ./mbmd binary

To cross-build for a different archtecture (e.g. Raspberry Pi), use

GOOS=linux GOARCH=arm GOARM=5 make build

Running

To get help on the various command line options run

mbmd -h

The full documentation is available in the docs folder. A typical invocation looks like this:

$ ./bin/mbmd run -a /dev/ttyUSB0 -d janitza:26,sdm:1
2017/01/25 16:34:26 config: creating RTU connection via /dev/ttyUSB0 (9600baud, 8N1)
2017/01/25 16:34:26 httpd: starting api at :8080

This call queries a Janitza B23 meter with ID 26 and an Eastron SDM meter at ID 1. Not all devices are by default configured to use ID 1. The default device IDs depend on the meter type and documented in the meter's manual.

To use RTU devices with RS485/Ethernet adapters, add the --rtu switch to configure mbmd to use the TCP connection with RTU data format:

❯ ./bin/mbmd run -a rs485.fritz.box:23 --rtu -d sdm:1
2020/01/02 10:43:53 mbmd unknown version (unknown commit)
2020/01/02 10:43:53 config: creating RTU over TCP connection for rs485.fritz.box:23
2020/01/02 10:43:53 initialized device SDM1.1: {SDM Eastron SDM meters   }
2020/01/02 10:43:53 httpd: starting api at :8080

If you use the -v commandline switch you can see modbus traffic and the current readings on the command line. At http://localhost:8080 you can see an embedded web page that updates itself with the latest values:

realtime view of incoming measurements

Run using Docker

Alternatively run mbmd using the Docker image:

docker run -p 8080:8080 --device=/dev/ttyUSB0 ghcr.io/volkszaehler/mbmd run -a /dev/ttyUSB0 -u 0.0.0.0:8080 -d sdm:1

To mount the config file into the docker container use -v $(pwd)/mbmd.yaml:/etc/mbmd.yaml.

Raspberry Pi

Download the ARM package for usage with Raspberry Pi and copy the binary into /usr/local/bin. The following sytemd unit can be used to start mbmd as service (put this into a new file /etc/systemd/system/mbmd.service):

[Unit]
Description=mbmd
After=syslog.target
After=network-online.target
[Service]
ExecStart=/usr/local/bin/mbmd run -a /dev/ttyAMA0
Restart=always
[Install]
WantedBy=multi-user.target

You might need to adjust the -a parameter depending on where your RS485 adapter is connected. Then, use

systemctl start mbmd

to test your installation. If you're satisfied use

systemctl enable mbmd

to start the service at boot time automatically.

WARNING: When using an FTDI-based USB-RS485 adaptor the Raspberry Pi might become unreachable after a while. This is most likely not an issue with the RS485-USB adaptor or this software, but because of a bug in the Raspberry Pi kernel. To fix switch the internal dwc USB hub of the Raspberry Pi to USB1.1 by adding the following parameter to /boot/cmdline.txt:

dwc_otg.speed=1

Detecting connected meters

MODBUS/RTU does not provide a mechanism to discover devices. There is no reliable way to detect all attached devices. As workaround mbmd scan attempts to read the L1 voltage from all device IDs and reports which one replied correctly (i.e. 110/230V +/-10%):

./mbmd scan -a /dev/ttyUSB0
2017/06/21 10:22:34 Starting bus scan
2017/06/21 10:22:35 Device 1: n/a
...
2017/07/27 16:16:39 Device 21: SDM type device found, L1 voltage: 234.86
2017/07/27 16:16:40 Device 22: n/a
2017/07/27 16:16:40 Device 23: n/a
2017/07/27 16:16:40 Device 24: n/a
2017/07/27 16:16:40 Device 25: n/a
2017/07/27 16:16:40 Device 26: Janitza type device found, L1 voltage: 235.10
...
2017/07/27 16:17:25 Device 247: n/a
2017/07/27 16:17:25 Found 2 active devices:
2017/07/27 16:17:25 * slave address 21: type SDM
2017/07/27 16:17:25 * slave address 26: type JANITZA
2017/07/27 16:17:25 WARNING: This lists only the devices that responded to a known L1 voltage request. Devices with different function code definitions might not be detected.

API

Rest API

mbmd provides a convenient REST API. Supported endpoints under /api are:

  • /api/last/{ID} latest data for device
  • /api/avg/{ID} averaged data over last minute
  • /api/status daemon status

Both device APIs can also be called without the device id to return data for all connected devices.

Monitoring

The /api/status endpoint provides the following information:

$ curl http://localhost:8080/api/status
{
  "StartTime": "2017-01-25T16:35:50.839829945+01:00",
  "UpTime": 65587.177092186,
  "Goroutines": 11,
  "Memory": {
    "Alloc": 1568344,
    "HeapAlloc": 1568344
  },
  "Modbus": {
    "TotalModbusRequests": 1979122,
    "ModbusRequestRatePerMinute": 1810.5264666764785,
    "TotalModbusErrors": 738,
    "ModbusErrorRatePerMinute": 0.6751319688261972
  },
  "ConfiguredMeters": [
    {
      "Id": 26,
      "Type": "JANITZA",
      "Status": "available"
    }
  ]
}

This is a snapshot of a process running over night, along with the error statistics during that timeframe. The process queries continuously, the cabling is not a shielded, twisted wire but something that I had laying around. With proper cabling the error rate should be lower, though.

Websocket API

Data read from the meters can be observed by clients in realtime using the Websocket API. As soon as new readings are available, they are pushed to connected websocket clients.

The websocket API is available on /ws. All connected clients receive status and meter updates for all connected meters without further subscription.

MQTT API

Another option for receiving client updates is by using the built-in MQTT publisher. By default, readings are published at /mbmd/<unique id>/<reading>. Rate limiting is possible.

InfluxDB support

There is also the option to directly insert the data into an influxdb database by using the command-line options available. InfluxDB 1.8 and 2.0 are currently supported. to enable this, add the --influx-database and the --influx-url commandline parameter. More advanced configuration is available, to learn more checkout the mbmd_run.md documentation

If InfluxDB is unavailable, mbmd can keep unsent data in memory and write it once the database is back. Set --influx-buffer (or influx.buffer) to how long data should be kept, e.g. 1h. The default 0 keeps the InfluxDB client's behaviour of 5 retries within 3 minutes, which holds about 10 seconds of data. Buffered data is lost if mbmd restarts.

Supported Devices

mbmd supports a range of DIN rail meters and grid inverters.

Modbus RTU Meters

The meters have slightly different capabilities. The Eastron SDM630 offers a lot of features, while the smaller devices only support basic features. The table below gives an overview (please consult the manuals for definitive guidance). The Type column is the identifier passed to -d <Type>:<id> (case-insensitive).

Legend: + supported, - not available, (+) only one energy direction (import or export) available per phase.

Meter Type Phases Voltage Current Power Power Factor Total Import Total Export Per-phase Import/Export Line/Neutral THD
ABB A/B-Series ABB 3 + + + + + + + -
B+G e-tech DS100 DS100 3 + + + + + + + -
B+G e-tech WS100 WS100 1 + + + + + + - -
Bernecker (BE) MPM3PM MPM 3 + + + + + + - -
Carlo Gavazzi EM24 CGEM24 3 + + + + + + (+) -
Carlo Gavazzi EM24 E1 CGEM24_E1 3 + + + + + + (+) -
Carlo Gavazzi EM/ET 330/340 CGEX3X0 3 + + + + + + (+) -
Chint DTSU666 DTSU666 3 + + + + + + + -
DDM18SD DDM 1 + + + + + - - -
DZG DVH4013 DZG 3 + + - + + + + -
Eastron SDM54 SDM54 3 + + + + + + + +
Eastron SDM72 SDM72 3 - - + - + + - -
Eastron SDM72 v2 SDM72V2 3 + + + + + + - -
Eastron SDM120 SDM120 1 + + + + + + - -
Eastron SDM220 SDM220 1 + + + + + + - -
Eastron SDM230 SDM230 1 + + + + + + - -
Eastron SDM630 SDM 3 + + + + + + + +
Eastron SMART X96-1A X961A 3 + + + + + + + +
Eltako DSZ15DZMOD/DSZ16 ELTAKODSZ15 / ELTAKODSZ16 3 + + + + + + - -
Finder 7M.24 FIND7M24 1 + + + + + + - +
Finder 7M.38 FIND7M38 3 + + + + + + - +
Gossen Metrawatt ENERGYMID EM228x/EM238x ENERGYMID 1/3 + + + + + + - -
Inepro Pro 380 INEPRO 3 + + + + + + + -
Janitza B-Series JANITZA 3 + + + + + + + -
Lovato DMG610 DMG610 3 + + + - + + + +
ORNO WE-504 ORNO1P504 1 + + + + + - - -
ORNO WE-514/515 ORNO1p 1 + + + + + - - -
ORNO WE-516/517 ORNO3p 3 + + + + + + + -
ORNO WE-525/526 ORNO1p525 1 + + + + + + - -
Saia Burgess (SBC) ALE3 SBC 3 + + + + + + - -
Schneider Electric iEM3000 IEM3000 3 + + + - + + (+) -
Siemens PAC2200 PAC2200 3 + + + + + + - -
Socomec Countis E3x/E4x SOCOMEC 3 + + + + + + - -
SolarEdge SE-MTR-3Y SEMTR 3 + + + + + + + -
Wago 879-30xx WAGO87930 3 + + + + + + + -
  • B+G e-tech DS100: Similarly capable as the SDM630 if not better, and very cheap in Germany (below 50€). It supports a higher baud rate than the SDM630, so measurements can be taken more often. There is also a MID version and a multi-tariff ("-30B") version (manual). What Sum (total energy) contains depends on the meter's calculation method (combination code): 1 = import, 2 = export, 3 = import + export, 4 = import − export, 5 = netting (DS100-00B and -00B-R from model year 2024 on). With 4 or 5, Sum turns negative once export exceeds import. Import and Export are always the separate counters. Currents are signed and follow the direction of active power, so they read negative while exporting. Demand values are the average power over the meter's demand period (15 minutes by default), maximum demand is the highest of these since the demand values were last cleared on the meter. The resettable counters only differ from Import, Export and Sum after they were reset on the meter.
  • B+G e-tech WS100: Cheap and small (1TE), 1P MID meter.
  • Bernecker (BE) MPM3PM: Compact (4TE) three phase meter.
  • Carlo Gavazzi EM24: Compact (4TE), 3P meter with RS-485 interface.
  • Carlo Gavazzi EM24 E1: Compact (4TE), 3P meter with Ethernet interface.
  • DZG DVH4013: This meter does not provide raw phase power measurements and only aggregated import/export measurements. The meter is only partially implemented and not recommended. By default, the meter communicates using 9600 8E1. The meter ID is derived from the serial number taking the last two numbers of the serial number (top right of the device), e.g. 23, and add one (24). Assume this is a hexadecimal number and convert it to decimal (36). Use this as the meter ID.
  • Eastron SDM54: Compact (3TE), 3P meter with a lot of features. Can be configured using the builtin display.
  • Eastron SDM72: Compact (4TE), 3P meter with bare minimum of total measurements, no currents. Can be configured using the builtin display.
  • Eastron SDM120: Cheap and small (1TE), but communication parameters can only be set over MODBUS, which is currently not supported by this project. You can use e.g. SDM120C to change parameters.
  • Eastron SDM220/230: More comfortable (2TE), can be configured using the builtin display.
  • Eastron SDM630: v1 and v2, both MID and non-MID. Compact (4TE) and with lots of features. Can be configured for 1P2 (single phase with neutral), 3P3 (three phase without neutral) and 3P4 (three phase with neutral) systems.
  • Eltako DSZ15DZMOD/DSZ16: Bidirectional 3P MID meters with Modbus RTU. Voltage, current, power, power factor and total import/export energy are supported; the DSZ16's additional measurements (reactive/apparent power, frequency, per-phase energy) are not yet implemented (Modbus spec).
  • Gossen Metrawatt ENERGYMID EM228x/EM238x: MID meters for direct (EM2281/EM2289) and transformer (EM2381/EM2387/EM2389) connection. Voltages, currents and powers are decoded using the device's exponent registers, energies using the primary energy factor, so CT/VT ratios are applied by the meter. Modbus RTU defaults to 8E1 (Modbus spec).
  • Inepro Pro 380: Compact (4TE) MID meter with extensive features. Can be connected 3P4W, 3P3W and 1P2W. Includes per-direction active/reactive energy consumption and supports two tariffs. Energy resolution is 2 digits per kWh.
  • Janitza B-Series: These meters have a higher update rate than the Eastron devices, but they are more expensive. The B23-312 variant is the one with a MODBUS interface.
  • ORNO WE-504/514/515: Low cost single phase meter. By default, the meter communicates using 9600 8E1. The meter ID is 1. Meter ID, bus speed and other parameters are configurable via Software (Windows only). WE-515 has a lithium battery and multi-tariff support, WE-514 does not support tariff zones.
  • ORNO WE-516/517: Low cost three phase meter. By default, the meter communicates using 9600 8E1. The meter ID is 1. Meter ID, bus speed and other parameters are configurable via Software (Windows only). WE-517 has a lithium battery and multi-tariff support, WE-516 does not support tariff zones.
  • Saia Burgess (SBC) ALE3: This compact Saia Burgess Controls meter is comparable to the SDM630. It has two tariffs, both import and export depending on meter version and compact (4TE). It's often used with Viessmann heat pumps.
  • Schneider Electric iEM3000: Professional meter with loads of configurable max/average measurements with timestamp functionality.
  • Socomec Countis E3x/E4x: 3P meters using Socomec's JBUS common table, so other devices of that family may work as well. Voltages, currents, powers and power factor come from the metrology table and are scaled by the configured CT/VT ratio. Total import/export energy has a resolution of 1 kWh (Modbus spec).

Modbus TCP Grid Inverters

Apart from meters, SunSpec-compatible grid inverters connected over TCP are supported, too. SunSpec defines a default register layout for accessing the devices.

Supported inverters include popular devices from SolarEdge (SE3000, SE9000) and SMA (Sunny Boy and Sunny TriPower).

In case of TCP connection, the adapter parameter becomes the hostname and port:

./mbmd run -a 192.168.0.44:502 -d SMA:23

SunSpec devices can host multiple subdevices, e.g. to expose a meter attached to an inverter. To access a subdevice, append its id to the slave id:

./mbmd run -a 192.168.0.44:502 -d FRONIUS:1.0 -d FRONIUS:1.1

Releases

Download the lastest release from github.com/volkszaehler/mbmd/releases.

About

ModBus Measurement Daemon - simple reading of data from ModBus meters and grid inverters

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages