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
New to SML, OBIS codes or reading heads? Start with What is SML?.
Add the crate๐
cargo add sml-kitFor a microcontroller, turn off the default std feature. Nothing else changes:
cargo add sml-kit --no-default-featuresRead 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.binIt 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 WhCommon OBIS codes๐
| OBIS | Obis:: | Meaning |
|---|---|---|
1-0:1.8.0*255 | ENERGY_IMPORT | Active energy imported (A+), total |
1-0:1.8.1*255 | ENERGY_IMPORT_T1 | A+, tariff 1 |
1-0:2.8.0*255 | ENERGY_EXPORT | Active energy exported (Aโ), total |
1-0:16.7.0*255 | POWER | Active power, all phases |
1-0:36.7.0*255 โฆ 76.7.0 | POWER_L1 โฆ POWER_L3 | Active power per phase |
1-0:0.0.9*255 | DEVICE_ID | Server ID |
1-0:32.7.0*255 โฆ 72.7.0 | VOLTAGE_L1 โฆ VOLTAGE_L3 | Voltage per phase |
1-0:96.1.0*255 | SERIAL | Server ID on newer meters |
129-129:199.130.3*255, 1-0:96.50.1*1 | MANUFACTURER, MANUFACTURER_ALT | Manufacturer |
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