This Arduino sketch was made to read and decrypt the data from my smart meter operated by Wiener Netze and publish it via MQTT. This way the data can be easily integrated into Home Assistant (see here). This sketch works both on the ESP32 and ESP8266. This project was only possible with the information from this thread; thank you to everyone who shared their findings (especially the user "pocki").
The sketch supports the following smart meter manufacturers:
- Landis+Gyr (tested with Landis+Gyr E450)
- Siemens (thanks @nomike)
To be able to use the optical user interface of the smart meter an IR reader with TTL-Interface is needed (I am using this one). This device reads the data from the optical user interface and transmits it to the ESP32 or ESP8266 via serial communication. Because normally UART0 is used for the USB connection (e.g. flashing the device, serial monitor, etc.), it makes sense to use UART2 for the IR reader. This is why we connect the IR reader to the ESP32 in the following way:
IR reader | ESP32 |
---|---|
VCC | 3.3V |
GND | GND |
RX | GPIO16 (RX2) |
TX | GPIO17 (TX2) |
The ESP8266 has two UARTs, however, UART1 is TX-only. Therefore, we have to use UART0 for the IR reader. In order to not interfere with the USB connection, we use Serial.swap()
(by defining SWAP_SERIAL
in the config file) to swap the pins for UART0 to GPIO15 (TX) and GPIO13 (RX). However, this way we can't use UART0 for logging anymore. But since we only want to send data to the serial monitor, we can use UART1 for that and still send the data through the USB connection by connecting GPIO2 (D4) to GPIO1 (TX). Hence, we connect the IR reader to the ESP8266 in the following way:
IR reader | ESP8266 |
---|---|
VCC | 3.3V |
GND | GND |
RX | GPIO13 (D7) |
TX | GPIO15 (D8) |
connect GPIO2 (D4) to GPIO1 (TX) |
After you cloned the repository, rename config_esp32.h
or config_esp8266.h
(depending on which device you have) to config.h
and open it. Adjust the settings and save the file (the decryption key can be obtained in the Smart Meter Webportal). Don't forget to uncomment the settings for your specific smart meter brand in the "SMART METER" section. After that open esp-smartmeter-reader.ino
in the Arduino IDE. The sketch uses the following libaries:
- FastCRC
- ArduinoJson
- PubSubClient
- Crypto (only for ESP8266*)
All those libraries must be installed via the Library Manager. Once that is done you should be able to compile and upload the sketch via the Arduino IDE.
*For the ESP32, we use the mbedtls library for decryption, which should be already available in the Arduino IDE. Because mbedtls is not available for the ESP8266, we use the Crypto library on the ESP8266 instead.
The example below shows how the data can be integrated into Home Assistant:
mqtt:
sensor:
- name: "Zählerstand"
state_topic: "homeassistant/sensor/smartmeter/state" # use topic from config.h
device_class: "energy"
unit_of_measurement: "kWh"
state_class: "total_increasing"
value_template: "{{ value_json['+A'] }}"
- name: "Momentanleistung"
state_topic: "homeassistant/sensor/smartmeter/state" # use topic from config.h
device_class: "power"
unit_of_measurement: "W"
state_class: "measurement"
value_template: "{{ value_json['+P'] }}"
# the history of instantaneous power is not interesting
# therefore exclude it from the recorder
recorder:
exclude:
entities:
- sensor.momentanleistung