# sml-kit
**Read German smart meters in Rust.** sml-kit decodes SML (Smart Message Language) from a
meter's optical interface or the meter bus behind a smart-meter gateway into exact
readings: sans-I/O, `no_std`, no allocator, no dependencies.
[](https://crates.io/crates/sml-kit)
[](https://docs.rs/sml-kit)
[](https://github.com/hupe1980/sml-kit/actions/workflows/ci.yml)
[](https://github.com/hupe1980/sml-kit/blob/main/Cargo.toml)
[](#license)
**[Documentation](https://hupe1980.github.io/sml-kit/) · [What is SML?](https://hupe1980.github.io/sml-kit/docs/what-is-sml/) · [API reference](https://docs.rs/sml-kit)**
```rust
use sml_kit::{Decoder, Obis, readout};
// Call with every chunk of bytes from the reading head. Keep one decoder per meter
// (`Decoder::new([0; 1024])`): a frame often arrives across several reads.
fn on_bytes(decoder: &mut Decoder<[u8; 1024]>, bytes: &[u8]) {
decoder.push_slice(bytes, |frame| {
let Ok(list) = frame.and_then(readout) else { return };
if let Some(a_plus) = list.quantity(Obis::ENERGY_IMPORT) {
println!("{}: A+ = {a_plus}", list.server_id); // "1 EMH00 04321349: A+ = 15338555.7 Wh"
}
});
}
```
## Install
```toml
[dependencies]
sml-kit = "0.1" # desktop, Raspberry Pi, server
# sml-kit = { version = "0.1", default-features = false } # microcontroller: no std, no alloc
```
To read a real meter you need an infrared reading head on its optical interface
(9600 baud, 8N1) and, for power and full-precision energy, the PIN from your meter
operator. [What is SML?](https://hupe1980.github.io/sml-kit/docs/what-is-sml/) explains both.
## Tested against real meters
Every CRC-valid frame of the public [libsml-testing] collection reads: 216 frames from
EMH, ISKRA, DZG, eBZ, Holley, Itron, EasyMeter and Dr. Neuhaus meters, strictly for every
meter but one. Three meters deviate from the specification; sml-kit reads them when you
name the deviation:
| Holley DTZ541 (some) | CRC-16/KERMIT instead of X-25 | `ParseOptions::lenient()` |
| Holley DTZ541-ZDBA, DDZ285 | `SML_Time` without its choice tag | `ParseOptions::lenient()` |
| DZG DVS74 (affected serials) | 356.24 W sent as −299.12 W | `.with_dzg_power(true)` |
The capture tool tells you which one your meter needs:
```sh
cargo run --example decode -- capture.bin # raw or hex: readings, and what your meter needs
```
## Features
| **Transport v1** | sans-I/O `Decoder` over an owned or borrowed buffer; `encode_frame` |
| **Readings in one call** | `readout`, `GetListResponse::quantity`; exact `Quantity` with unit |
| **Values** | OBIS (parse, names), the DLMS unit table, `SML_Time`, `ServerId` as printed on the meter |
| **Energy direction** | `direction`, `signed_power` from the status word, for meters that send power without a sign |
| **Every message body** | Open, Close, GetList, Get/SetProcParameter, GetProfileList/Pack, Attention: typed, read **and** written |
| **Request files** | `SmlFile`: open, requests, close with transaction IDs and CRCs in one call |
| **Blocking I/O** (`std`) | `io::FrameReader` over any `std::io::Read` |
| **Async** | the same `Decoder` in Embassy, Tokio or any runtime, no dependency |
| **Simulator** | `sim::Telegram` for tests |
Not supported: verifying SML signatures (they are exposed as bytes), transport
version 2 (no device sends it), and IEC 62056-21 text (an older protocol).
## Design
- **Sans-I/O.** No ports or clocks inside: feed bytes from a reading head, a meter bus or
a file.
- **`no_std`, no allocator, no dependencies.** CI checks the Cortex-M release build for
panic paths.
- **Exact.** Readings are `mantissa × 10^scaler` with their unit, never floats: a meter
reading is evidence for an invoice.
- **Strict by default.** Both CRCs are checked; every deviation a real meter needs is a
named option.
- **Never panics on input.** Malformed, truncated and random bytes are errors; the
parsers are fuzzed.
- **No secrets in logs.** Request passwords print as `<redacted>`.
## Examples
```sh
cargo run --example readout # a simulated telegram
cargo run --example decode -- tests/corpus/emh-ehz-apollon77.hex
cargo run --example serial -- /dev/ttyUSB0 # a USB reading head
```
## License
Licensed under either of [Apache License, Version 2.0](https://github.com/hupe1980/sml-kit/blob/main/LICENSE-APACHE)
or [MIT license](https://github.com/hupe1980/sml-kit/blob/main/LICENSE-MIT) at your option.
The captures in `tests/corpus/` keep their own MIT notices.
Not affiliated with the BSI or any meter manufacturer. SML is specified in BSI
TR-03109-1, Annex IV.
[libsml-testing]: https://github.com/devZer0/libsml-testing