sml-kit

Getting started

Add sml-kit to a Rust project, feed it bytes from an infrared reading head, and read exact meter values.

On this page
  1. Add the crate
  2. Read a meter
  3. If your meter does not decode
  4. Use the value
  5. Common OBIS codes
  6. Try it without a meter

New to SML, OBIS codes or reading heads? Start with What is SML?.

Add the crate๐Ÿ”—

cargo add sml-kit

For a microcontroller, turn off the default std feature. Nothing else changes:

cargo add sml-kit --no-default-features

Read a meter๐Ÿ”—

use sml_kit::{Decoder, Obis, readout};

// Once per meter, not per read: a frame often arrives across several reads, and the
// decoder keeps the part it has seen. It owns its buffer: the largest frame you accept.
let mut decoder = Decoder::new([0u8; 1024]);

loop {
    let n = port.read(&mut chunk)?;    // your serial port, IR head or capture file
    decoder.push_slice(&chunk[..n], |frame| {
        match frame.and_then(readout) {
            Ok(list) => {
                println!("meter {}", list.server_id);              // 1 EMH00 04321349
                for entry in list.entries.into_iter().flatten() {
                    if let Some(q) = entry.quantity() {
                        println!("{} = {q}", entry.obis);          // 1-0:1.8.0*255 = 15338555.7 Wh
                    }
                }
            }
            Err(e) => eprintln!("bad frame: {e}"),
        }
    });
}

The decoder skips noise before a start sequence and checks the frame CRC; readout checks every message CRC and hands back the readings. Both are strict by default.

On a desktop or a Raspberry Pi, io::FrameReader does the byte pump for you:

use sml_kit::{Obis, io::FrameReader, readout};

for frame in FrameReader::new(std::fs::File::open("/dev/ttyUSB0")?) {
    let Ok(frame) = frame else { continue };           // damaged frame or read timeout
    if let Ok(list) = readout(&frame) {
        println!("{:?}", list.quantity(Obis::POWER));
    }
}

In an async task (Embassy, Tokio), use the same decoder; see Async.

If your meter does not decode๐Ÿ”—

Run the capture tool on a few seconds of bytes from the head:

cat /dev/ttyUSB0 > capture.bin     # after stty 9600 raw; Ctrl-C after a few seconds
cargo run --example decode -- capture.bin

It reads strictly first and, if that fails, names the option your meter needs. See Meter quirks.

Use the value๐Ÿ”—

entry.quantity() (or list.quantity(obis)) returns a Quantity { mantissa, exponent, unit }. It is exact:

let q = list.quantity(Obis::ENERGY_IMPORT).unwrap();
q.to_string();           // "15338555.7 Wh"
q.to_scale(-3);          // Some(15338555700): milli-Wh as an integer
q.to_scale(0);           // None: 15338555.7 is not a whole number of Wh

Common OBIS codes๐Ÿ”—

OBISObis::Meaning
1-0:1.8.0*255ENERGY_IMPORTActive energy imported (A+), total
1-0:1.8.1*255ENERGY_IMPORT_T1A+, tariff 1
1-0:2.8.0*255ENERGY_EXPORTActive energy exported (Aโˆ’), total
1-0:16.7.0*255POWERActive power, all phases
1-0:36.7.0*255 โ€ฆ 76.7.0POWER_L1 โ€ฆ POWER_L3Active power per phase
1-0:0.0.9*255DEVICE_IDServer ID
1-0:32.7.0*255 โ€ฆ 72.7.0VOLTAGE_L1 โ€ฆ VOLTAGE_L3Voltage per phase
1-0:96.1.0*255SERIALServer ID on newer meters
129-129:199.130.3*255, 1-0:96.50.1*1MANUFACTURER, MANUFACTURER_ALTManufacturer

Codes also parse from text: "1.8.0".parse::<Obis>() is Obis::ENERGY_IMPORT, and Obis::name() describes the common ones.

Many basic meters show power and full-precision energy only after the PIN is entered on the meter. Without it, they send energy in whole kWh (scaler +3, e.g. 2324000 Wh) and leave out power. sml-kit shows exactly what the meter sent.

Try it without a meter๐Ÿ”—

cargo run --example readout                                   # a simulated telegram
cargo run --example decode -- tests/corpus/emh-ehz-apollon77.hex   # a real EMH capture

Edit this page on GitHub