From ac5544696c8752bf41c9ea22cc72fc985ac8aafd Mon Sep 17 00:00:00 2001 From: Louis van der Walt Date: Thu, 27 Aug 2020 18:20:33 +0200 Subject: [PATCH 1/4] Clear Cells --- dbus-serialbattery.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dbus-serialbattery.py b/dbus-serialbattery.py index 1049a48f..3d2c5f1e 100644 --- a/dbus-serialbattery.py +++ b/dbus-serialbattery.py @@ -136,6 +136,8 @@ def is_bit_set(self, tmp): def to_cell_bits(self, byte_data): tmp = bin(byte_data)[2:].rjust(self.cell_count, self.zero_char) + for c in self.cells: + self.cells.remove(c) for bit in reversed(tmp): self.cells.append(Cell(self.is_bit_set(bit))) From 7d8ca634a92668ecdfa7d8a3be95cf726b59c679 Mon Sep 17 00:00:00 2001 From: Louis van der Walt Date: Wed, 2 Sep 2020 08:28:26 +0200 Subject: [PATCH 2/4] WIP - serial-starter config Add config to get serail-sarter to start the battery driver as well --- dbus-serialbattery.py | 13 ++++++++++--- rc.local | 2 ++ serial-starter.d | 3 +++ service/log/run | 3 +++ service/run | 3 +++ 5 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 rc.local create mode 100644 serial-starter.d create mode 100644 service/log/run create mode 100644 service/run diff --git a/dbus-serialbattery.py b/dbus-serialbattery.py index 3d2c5f1e..ebd63498 100644 --- a/dbus-serialbattery.py +++ b/dbus-serialbattery.py @@ -1,4 +1,4 @@ -#!/usr/bin/python -u +#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import absolute_import, division, print_function, unicode_literals @@ -324,7 +324,7 @@ def setup_vedbus(self, instance): self._dbusservice.add_path('/Alarms/LowTemperature', 0, writeable=True) -INTERVAL = 3000 +INTERVAL = 2000 def main(): @@ -340,8 +340,15 @@ def poll_battery(loop): logger.info('dbus-serialbattery') # Have a mainloop, so we can send/receive asynchronous calls to and from dbus DBusGMainLoop(set_as_default=True) + + # Get the port we need to use from the argument + if len(sys.argv) > 1: + port = sys.argv[1] + else: + logger.info('No Port') + port = '/dev/ttyUSB2' + # create a new battery object that can read the battery - port = '/dev/ttyUSB2' battery = Battery(port) result = battery.read_hardware_data() if result is False: diff --git a/rc.local b/rc.local new file mode 100644 index 00000000..1668aad7 --- /dev/null +++ b/rc.local @@ -0,0 +1,2 @@ +#!/bin/sh +ln -s /data/etc/dbus-serialbattery/service /service/dbus-serialbattery diff --git a/serial-starter.d b/serial-starter.d new file mode 100644 index 00000000..1c1f7289 --- /dev/null +++ b/serial-starter.d @@ -0,0 +1,3 @@ +service sbattery dbus-serialbattery + +alias default sbattery diff --git a/service/log/run b/service/log/run new file mode 100644 index 00000000..ca6196de --- /dev/null +++ b/service/log/run @@ -0,0 +1,3 @@ +#!/bin/sh +exec 2>&1 +exec multilog t s25000 n4 /var/log/dbus-serialbattery.TTY diff --git a/service/run b/service/run new file mode 100644 index 00000000..e31b709c --- /dev/null +++ b/service/run @@ -0,0 +1,3 @@ +#!/bin/sh +exec 2>&1 +exec softlimit -d 100000000 -s 1000000 -a 100000000 /data/etc/dbus-serialbattery/dbus-serialbattery.py TTY \ No newline at end of file From c1b855b63c4f7529952811eee26e7915916cd5f8 Mon Sep 17 00:00:00 2001 From: Louis van der Walt Date: Thu, 3 Sep 2020 08:36:46 +0200 Subject: [PATCH 3/4] serial-starter config serial-starter config --- dbus-serialbattery.py | 2 +- rc.local | 2 +- serial-starter.d | 1 - service/run | 2 +- start-serialbattery.sh | 12 ++++++++++++ 5 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 start-serialbattery.sh diff --git a/dbus-serialbattery.py b/dbus-serialbattery.py index ebd63498..7afc2008 100644 --- a/dbus-serialbattery.py +++ b/dbus-serialbattery.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/python # -*- coding: utf-8 -*- from __future__ import absolute_import, division, print_function, unicode_literals diff --git a/rc.local b/rc.local index 1668aad7..9fd1a122 100644 --- a/rc.local +++ b/rc.local @@ -1,2 +1,2 @@ #!/bin/sh -ln -s /data/etc/dbus-serialbattery/service /service/dbus-serialbattery +ln -s /data/etc/dbus-serialbattery /opt/victronenergy/dbus-serialbattery diff --git a/serial-starter.d b/serial-starter.d index 1c1f7289..bfa2061e 100644 --- a/serial-starter.d +++ b/serial-starter.d @@ -1,3 +1,2 @@ service sbattery dbus-serialbattery - alias default sbattery diff --git a/service/run b/service/run index e31b709c..3ad9750e 100644 --- a/service/run +++ b/service/run @@ -1,3 +1,3 @@ #!/bin/sh exec 2>&1 -exec softlimit -d 100000000 -s 1000000 -a 100000000 /data/etc/dbus-serialbattery/dbus-serialbattery.py TTY \ No newline at end of file +exec softlimit -d 100000000 -s 1000000 -a 100000000 python /opt/victronenergy/dbus-serialbattery/dbus-serialbattery.py /dev/TTY \ No newline at end of file diff --git a/start-serialbattery.sh b/start-serialbattery.sh new file mode 100644 index 00000000..23e06c1f --- /dev/null +++ b/start-serialbattery.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# +# Start script for dbus-serialbattery +# First parameter: tty device to use +# +# Keep this script running with daemon tools. If it exits because the +# connection crashes, or whatever, daemon tools will start a new one. +# + +. /opt/victronenergy/serial-starter/run-service.sh + +exec /opt/victronenergy/dbus-serialbattery/dbus-serialbattery.py /dev/$tty From ecd29edc2ea762a7270259adfd6232682ed13cda Mon Sep 17 00:00:00 2001 From: Louis van der Walt Date: Thu, 3 Sep 2020 14:54:40 +0200 Subject: [PATCH 4/4] Update README with instructions Update README with instructions --- README.md | 23 ++++++++++++++++++++++- start-serialbattery.sh | 12 ------------ 2 files changed, 22 insertions(+), 13 deletions(-) delete mode 100644 start-serialbattery.sh diff --git a/README.md b/README.md index f31bad69..f7e5d38e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,23 @@ # dbus-serialbattery -dbus driver for serial battery in VenusOS +This is a driver for VenusOS devices (any GX device sold by Victron or a Raspberry Pi running the VenusOS image). + +The driver will communicate with a Battery Management System (BMS) that support serial communication (RS232 or RS485) +and publish this data to the dbus used by VenusOS. The main purpose is to supply up to date State Of Charge (SOC) values +to the inverter, but many extra parameters is also published if available from the BMS. + +Driver support: + * Smart BMS range from [LLT Power](https://www.lithiumbatterypcb.com/product-instructionev-battery-pcb-boardev-battery-pcb-board/ev-battery-pcb-board/smart-bms-of-power-battery/) +(Cell values are currently only supported for the first 16 cells: Min Cell V, Max Cell V, Cell Balance) + +Planned support: + * AntBMS + * Smart Daly BMS + +### How to install +1. You need to have a VenusOS device set up and running on your system and have [root access](https://www.victronenergy.com/live/ccgx:root_access). +2. You also need to connect your BMS to the VenusOS device using a serial interface. A USB->232 converter like the FT232R. The FT232R already has a driver included in the VenusOS. +3. Use a FTP client that support SFTP to copy the driver files to the rooted VenusOS device. [Filezilla](https://filezilla-project.org/) is a good option + - copy the dbus-serialbattery folder to `/data/etc/` + - copy or move rc.local to `/data/` + - copy or move serial-starter.d to `/data/conf/` + - reboot your VenusOS device and check if your battery is connected \ No newline at end of file diff --git a/start-serialbattery.sh b/start-serialbattery.sh deleted file mode 100644 index 23e06c1f..00000000 --- a/start-serialbattery.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -# -# Start script for dbus-serialbattery -# First parameter: tty device to use -# -# Keep this script running with daemon tools. If it exits because the -# connection crashes, or whatever, daemon tools will start a new one. -# - -. /opt/victronenergy/serial-starter/run-service.sh - -exec /opt/victronenergy/dbus-serialbattery/dbus-serialbattery.py /dev/$tty