This repository contains a Rust library and a command-line tool for interacting with Eastron SDM72 series energy meters via the Modbus protocol.
- Hardware Requirements
- Technical Documentation
- Technical Specifications
- Installation & Compilation
- Command-Line Usage
- Library Usage
- Cargo Features
- License
To use this tool, you need:
- An Eastron SDM72 series energy meter.
- A USB-to-RS485 converter (for RTU mode) or a Modbus TCP gateway.
For more detailed information, please refer to the official datasheets available in the docs/ directory:
| Feature | Details |
|---|---|
| Nominal Voltage | 230/400V AC (3~) |
| Operational Voltage | 80%~120% of nominal voltage |
| Current Measurement | Up to 100A direct connection |
| Communication Protocol | Modbus RTU/TCP |
| Baud Rates | 2400, 4800, 9600, 19200, 38400 |
| Data Format | N, 8, 1 (No parity, 8 data bits, 1 stop bit) |
Ensure you have the following dependencies installed before proceeding:
- Rust and Cargo: Install via rustup
- Git: To clone the repository
- A C compiler and linker
- Clone the repository:
git clone https://github.com/acpiccolo/SDM72-Powermeter.git cd SDM72-Powermeter - Compile the project:
The compiled binary will be available at:
cargo build --release
target/release/sdm72
- (Optional) Install the binary system-wide:
This installs
cargo install --path .sdm72to$HOME/.cargo/bin, making it accessible from anywhere.
To list all available commands and their options, run:
sdm72 --helpFor RTU Modbus (RS485) connected devices:
sdm72 rtu --address 1 --baudrate 9600 read-allFor TCP Modbus connected devices:
sdm72 tcp 192.168.0.222:502 read-allYou can also run the tool as a daemon that publishes data to an MQTT broker. The connection is configured via an mqtt.yaml file.
sdm72 rtu --address 1 --baudrate 9600 daemon mqttThe sdm72_lib crate provides two main ways to interact with the SDM72 energy meters:
-
High-Level, Safe Clients: Stateful, thread-safe clients that are easy to share and use in concurrent applications. This is the recommended approach for most users. See
tokio_sync_safe_client::SafeClient(blocking) andtokio_async_safe_client::SafeClient(async). -
Low-Level, Stateless Functions: A set of stateless functions that directly map to the device's Modbus commands. This API offers maximum flexibility but requires manual management of the Modbus context. See the
tokio_syncandtokio_asyncmodules.
Here's a quick example of how to use the synchronous SafeClient to read all values over a TCP connection.
First, add the required dependencies to your project:
cargo add [email protected] --no-default-features --features "tokio-tcp-sync,safe-client-sync,serde"
cargo add [email protected]
cargo add tokio@1 --features fulluse sdm72_lib::{
protocol::Address,
tokio_sync_safe_client::SafeClient,
};
use tokio_modbus::client::sync::tcp;
use tokio_modbus::Slave;
use std::time::Duration;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Connect to the device and create a stateful, safe client
let socket_addr = "192.168.1.100:502".parse()?;
let ctx = tcp::connect_slave(socket_addr, Slave(*Address::default()))?;
let mut client = SafeClient::new(ctx);
// Use the client to interact with the device
let values = client.read_all(&Duration::from_millis(100))?;
println!("Successfully read values: {:#?}", values);
Ok(())
}This crate uses a feature-based system to minimize dependencies. When using it as a library, you should disable default features and select only the components you need.
default: Enablesbin-dependencies, intended for compiling thesdm72command-line tool.
tokio-rtu-sync: Synchronous (blocking) RTU client.tokio-tcp-sync: Synchronous (blocking) TCP client.tokio-rtu: Asynchronous (non-blocking) RTU client.tokio-tcp: Asynchronous (non-blocking) TCP client.
safe-client-sync: A thread-safe, stateful wrapper for synchronous clients.safe-client-async: A thread-safe, stateful wrapper for asynchronous clients.
serde: Implementsserde::Serializeandserde::Deserializefor protocol structs.bin-dependencies: All features required to build thesdm72binary.
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.