volkszaehler / mbmd

ModBus Measurement Daemon - simple reading of data from ModBus meters and grid inverters
BSD 3-Clause "New" or "Revised" License
241 stars 84 forks source link
golang grid-inverters modbus modbus-meters openhab smart-meter sunspec volkszaehler

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.

Table of Contents

Requirements

You'll need:

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 and requires ^1.16. To build from source two steps are needed:

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 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:

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.

Homie API

Homie is an MQTT convention for IoT/M2M. mbmd publishes all devices and readings using the Homie protocol. This allows systems like e.g. OpenHAB to auto-discover devices operated by mbmd:

auto-discovery of thinks in OpenHAB

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

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):

Meter Phases Voltage Current Power Power Factor Total Import Total Export Per-phase Import/Export Line/Neutral THD
SDM72 3 - - + - + + - -
SDM120/220 1 + + + + + + - -
SDM530 3 + + + + + + - -
SDM630 3 + + + + + + + +
Inepro PRO1/2 1 + + + + + + - -
Inepro PRO380 3 + + + + + + + -
Janitza B23-312 3 + + + + + + - -
DZG DVH4013 3 + + - - + + - -
SBC ALE3 3 + + + + + + - -
ABB A/B-Series 3 + + + + + + + +
BE MPM3MP 3 + + + + + + - -
KOSTAL Smart Energy Meter 3 + + + + + + + -
ORNO WE-504/514/515 1 + + + + + - - -
ORNO WE-516/517 3 + + + + + + + -
iEM3000 Series 3 + + + + + + (+) +

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.