A Go library and command-line tool for reading power samples (voltage and current) from compatible USB power meter devices (e.g., WITRN C4/C5 meters) via USB HID.
- Reads real-time voltage and current samples from supported USB power meters
- Outputs samples in JSON format (optional)
- Computes and displays statistics (min, max, average, percentiles) for voltage and current
- Supports sampling for a specified duration
- Go 1.18 or newer
- Compatible USB power meter (e.g., WITRN)
- macOS, Linux, or Windows
Clone the repository and build the command-line tool:
git clone <this-repo-url>
cd usbpower
cd cmd/usbpower
go build -o usbpower./usbpower [flags]-duration <duration>: Stop sampling after the specified duration (e.g.,10s,1m). If not set, runs until interrupted.-output json: Output each sample as a JSON object to stdout.
Sample for 10 seconds and print statistics:
./usbpower -duration 10sSample for 5 seconds and output raw JSON samples:
./usbpower -duration 5s -output jsonAfter sampling, the tool prints statistics for voltage and current:
- Minimum
- Maximum
- Average
- 50th percentile (median)
- 90th percentile
You can use the usbpower package in your own Go programs to access USB power meter data:
import "github.com/rs/usbpower"
device, err := usbpower.OpenDevice()
if err != nil {
// handle error
}
defer device.Close()
sample, err := device.Read()
// use sample.Voltage, sample.Current, sample.TimestampMIT