Documentation
¶
Overview ¶
Package voltgo provides a Go library for communicating with Voltgo and compatible LiFePO4 batteries via Bluetooth Low Energy (BLE).
These batteries are sold under various brand names including Enerwatt, TCED Worldwide, and others, but use the same BLE protocol compatible with the Voltgo mobile app.
This library implements the BLE protocol used by the Voltgo mobile app to monitor and control LiFePO4 battery management systems (BMS).
Basic usage:
client, err := voltgo.NewClient()
if err != nil {
log.Fatal(err)
}
defer client.Close()
// Scan for batteries
devices, err := client.Scan(ctx, 10*time.Second)
if err != nil {
log.Fatal(err)
}
// Connect to a device by its address string
battery, err := client.Connect(ctx, devices[0].Address)
if err != nil {
log.Fatal(err)
}
defer battery.Disconnect()
// Read battery status
status, err := battery.GetStatus(ctx)
if err != nil {
log.Fatal(err)
}
Data types returned to callers live in the battery package. BLE transport and Modbus framing are implementation details under internal/.
Protocol Details:
The BLE protocol uses the following UUIDs:
- Service: 00001006-0000-1000-8000-00805f9b34fb
- Write: 00001008-0000-1000-8000-00805f9b34fb
- Notify: 00001007-0000-1000-8000-00805f9b34fb
The batteries speak Modbus RTU framed over GATT: a standard Modbus read-holding-registers request (slave address 0x01, function 0x03, CRC-16/MODBUS) is written to the write characteristic, and the response frame arrives as a notification on the notify characteristic. Frames with an invalid CRC are silently ignored by the BMS. See PROTOCOL.md for the register map.
Index ¶
- Constants
- type Battery
- func (b *Battery) Disconnect() error
- func (b *Battery) GetBMSInfo(ctx context.Context) (*battery.BMSInfo, error)
- func (b *Battery) GetCellVoltages(ctx context.Context) ([]battery.Cell, error)
- func (b *Battery) GetDeviceIdentity(ctx context.Context) (*battery.DeviceIdentity, error)
- func (b *Battery) GetInfo(ctx context.Context) (*battery.Info, error)
- func (b *Battery) GetStatus(ctx context.Context) (*battery.Status, error)
- func (b *Battery) IsConnected() bool
- func (b *Battery) ReadRegisters(ctx context.Context, startReg, count uint16) ([]uint16, error)
- type Client
- type Transport
Constants ¶
const ( DefaultTimeout = 5 * time.Second DefaultScanDuration = 10 * time.Second )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Battery ¶
type Battery struct {
// contains filtered or unexported fields
}
Battery represents a connected battery
func NewBattery ¶ added in v0.2.0
NewBattery wraps an existing transport in a Battery. Most callers should use Client.Connect instead; this constructor exists for custom transports and testing.
func (*Battery) Disconnect ¶
Disconnect disconnects from the battery
func (*Battery) GetBMSInfo ¶
GetBMSInfo reads and parses the status register block. This is the low-level variant of GetStatus and includes the raw registers.
func (*Battery) GetCellVoltages ¶
GetCellVoltages retrieves individual cell voltages
func (*Battery) GetDeviceIdentity ¶ added in v0.2.0
GetDeviceIdentity reads the ASCII device-info register block (model, hardware version, manufacture date).
func (*Battery) GetInfo ¶
GetInfo retrieves battery identity information: chemistry and nominal voltage derived from the cell count, plus the device's ASCII identity strings (model, hardware version, manufacture date).
func (*Battery) IsConnected ¶
IsConnected returns whether the battery is connected
func (*Battery) ReadRegisters ¶
ReadRegisters reads count holding registers starting at startReg. This is the low-level primitive underlying all queries; the battery silently ignores malformed requests, which surfaces here as a timeout.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the main client for communicating with Voltgo batteries
func (*Client) Connect ¶
Connect connects to a battery device by the address string reported in battery.DeviceInfo: a MAC address ("a4:c1:37:43:a4:42"), or on macOS the CoreBluetooth UUID assigned during scanning.
type Transport ¶ added in v0.2.0
type Transport interface {
// Request writes a Modbus RTU frame and returns the response frame.
Request(ctx context.Context, frame []byte, timeout time.Duration) ([]byte, error)
Disconnect() error
IsConnected() bool
}
Transport is the request/response link a Battery uses to reach the BMS. *ble.Connection implements it; tests and alternative transports (e.g. an in-process emulator) can substitute their own.