Make energy visible
The conscious use of energy begins with transparency about the own consumption. Smart meters – i.e. digital electricity meters – deliver this data, usually via an optical interface according to IEC 62056-21, which sends in SML format (Smart Message Language).
But the data alone is not enough - what matters is that it is visible. to make it evaluable and linkable**. A look at one Energy dashboard can quickly provide surprising insights:
- An electric stove that draws 7W in standby
- Two garage door drives that permanently consume 10 W each
- An old router that causes 24W base load
Such seemingly small loads add up – and are often easier to optimize than large loads.
Smart meters, energy transition and digital sovereignty
Smart meters are often discussed in the context of the energy transition. Regardless of ideological debates: Create data Basis for decision-making. Anyone who knows their energy flow can Shift consumption over time, cut peaks or standby losses avoid.
Central monitoring helps to understand consumption patterns, not to exercise control, but to make meaningful decisions to be able to meet – in small and large ways.
I wanted the electricity meter data from my household directly to mine integrate central monitoring – without cloud connection, without black box appliance and without dependency on external services. The goal was a solution that works digitally confidently: locally, transparent, comprehensible and under full control of your own Data.
Hardware selection
The hardware requirements are minimal:
- a Raspberry Pi 2 Model B (or comparable)
- two USB-IR reading heads according to IEC 62056-21, e.g. B. ELV energy sensor ES-IEC
- Power and network
The selection was primarily pragmatically motivated: The Raspberry Pi 2 was still in the drawer – robust, economical and long-lasting Operation more than sufficient. The IR reading heads from ELV are as Kit available which not only keeps the price down but also a good understanding of how the optical works Interface conveyed.
This do-it-yourself character in particular fits the project well: simple, Comprehensible components that can be created with manageable effort can be combined with a full-fledged smart meter gateway - without Special hardware, without cloud, without dependencies.
Architecture
┌──────────────────────────────────────────────────────────────────────────┐
│ NetBSD dom0 │
│ (Xen Hypervisor Host) │
│ │
│ ┌──────────────────────────────────────────────────────────────────┐ │
│ │ Xen domU │ │
│ │ (Monitoring Virtual Machine) │ │
│ │ │ │
│ │ ┌────────────────────┐ ┌────────────────────┐ │ │
│ │ │ Prometheus │ <──── │ Grafana │ │ │
│ │ │ (scrapes metrics) │ │ (visualizes data) │ │ │
│ │ └────────┬───────────┘ └────────────────────┘ │ │
│ │ │ │ │
│ └────────────┼─────────────────────────────────────────────────────┘ │
│ │ HTTP pull (/metrics) │
└───────────────┼──────────────────────────────────────────────────────────┘
│
│
┌───────┴──────────────────────────────────────────────────────────┐
│ Raspberry Pi │
│ Smartmeter-Gateway (Flask App) │
│ http://pi:8080/metrics │
│ │
└──────────────────────────────────────────────────────────────────┘
│ │
USB(ttyUSB0) USB(ttyUSB1)
│ │
▼ ▼
┌────────────────────────┐ ┌────────────────────────┐
│ USB-IR-Lesekopf #1 │ │ USB-IR-Lesekopf #2 │
│ (IEC 62056-21, SML) │ │ (IEC 62056-21, SML) │
└────────────────────────┘ └────────────────────────┘
Software base: Buildroot
For the software side, the choice fell on Buildroot – a framework that that generates minimalist, tailor-made Linux systems. The Focus is on small images, clear structure and more stable Reproducibility – ideal for embedded systems.
Build preparation
$ wget https://buildroot.org/downloads/buildroot-2024.02.10.tar.gz
$ tar xvfz buildroot-2024.02.10.tar.gz
$ cd buildroot-2024.02.10/
$ make list-defconfigs | grep rasp
raspberrypi0_defconfig
raspberrypi2_defconfig
raspberrypi3_defconfig
raspberrypi4_defconfig
$ make raspberrypi2_defconfig
$ make menuconfig
Important settings
File system images
- Exact size: 256M
Tool chain
- C library: musl
System Configuration
- System hostname: gateway
- System banner: Welcome to Smartmeter Gateway
- /dev Management: Dynamic using devtmpfs + eudev
- Root password: *********
Target Packages
-
Interpreter Languages and scripting
- python3
- Core Python Modules: ssl, sqlite, xml, zlib
- External Python Modules:
- python flask
- python-flask-login
- python-flask-sqlalchemy
- python-pyzmq
- python requests
- python-serial
- python-serial-asyncio
- python-paho-mqtt
- python pip
- python3
-
Networking Applications
- rsync
- avahi (mDNS/DNS-SD daemon)
- dropbear
- chrony
- shellinabox
-
Shell and utilities
- tmux
Start build
$ make all
Result: A complete, minimal Linux image with Python and Network support. After the build, it can be dd onto an SD card transferred and then boots immediately on the Raspberry Pi.
After installation
After the first boot, the system (with display connected) basically configured. After completing this step you can The network interface is accessed via SSH with the created user become.
Chrony (time service)
# nano /etc/chrony.conf
pool pool.ntp.org iburst
initstepslew 10 pool.ntp.org
driftfile /var/lib/chrony/chrony.drift
rtcsync
cmdport 0
Set up users
# mkdir /home
# adduser user
# vi /etc/group
dialout:x:18:user
Python Virtual Environment
$ cd /home/user
$ python -m venv --system-site-packages .venv
$ . .venv/bin/activate
(.venv) $ pip install smllib flask
The Prometheus Exporter
The following Python script reads the SML data of both counters, extracts the relevant values and presents them as Prometheus compatible metrics available on port 8080.
#!/usr/bin/env python
import serial
import threading
import queue
from smllib import SmlStreamReader
from flask import Flask, Response
app = Flask(__name__)
data_store = {}
data_queue = queue.Queue()
@app.route("/metrics")
def metrics():
body = ""
for key in data_store.keys():
body += key + " " + str(data_store[key]) + "\n"
return Response(body, mimetype='text/plain')
def read_from_serial(port, baudrate, key):
try:
ser = serial.Serial(port, baudrate, timeout=True)
stream = SmlStreamReader()
while True:
data = ser.readline()
stream.add(data)
sml_frame = stream.get_frame()
if sml_frame is None:
continue
parsed_msgs = sml_frame.parse_frame()
obis_values = parsed_msgs[1].message_body.val_list
data_queue.put((f"total_power_consumption_watt_hours{{meter=\"{key}\"}}", obis_values[2].value / 10))
data_queue.put((f"current_power_consumption_watts{{meter=\"{key}\"}}", obis_values[4].value))
except serial.SerialException as e:
print(f"Fehler beim Öffnen der seriellen Schnittstelle {port}: {e}")
def update_data_store():
while True:
key, value = data_queue.get()
data_store[key] = value
data_queue.task_done()
if __name__ == '__main__':
threading.Thread(target=read_from_serial, daemon=True, args=('/dev/ttyUSB0', 9600, 'Heizung')).start()
threading.Thread(target=read_from_serial, daemon=True, args=('/dev/ttyUSB1', 9600, 'Allgemein')).start()
threading.Thread(target=update_data_store, daemon=True).start()
app.run(debug=False, host='0.0.0.0', port=8080)
Result: Prometheus endpoint
After starting the script, calling returns
http://<raspberrypi>:8080/metrics e.g. E.g. the following output:
total_power_consumption_watt_hours{meter="Heizung"} 182456.7
current_power_consumption_watts{meter="Heizung"} 64.3
total_power_consumption_watt_hours{meter="Allgemein"} 521109.1
current_power_consumption_watts{meter="Allgemein"} 243.6
This data can be queried regularly by Prometheus can be visualized directly in Grafana.
Integration with Grafana
A simple energy dashboard shows:
- current performance per meter
- Daily, weekly and monthly consumption
- Base load curves
This means that load peaks and idle consumers can be identified immediately - and concrete optimizations can be derived from them.
Conclusion
Conclusion: A smart meter gateway can be created with little effort build that provides measurement data in real time and fits seamlessly into existing monitoring solutions can be integrated.
The project shows: Energy efficiency starts with transparency. Man recognizes load profiles, discovers standby consumers and can be targeted Derive measures. Even a few watts here and there add up – and anyone who knows the data can use it. A small gateway that helps make energy visible.
Image credit: The Prometheus logo is a registered trademark of the Cloud Native Computing Foundation (CNCF). Use within the context of editorial reporting.