Skip to content

Commit

Permalink
Merge pull request #503 from mr-manuel/jkbms_ble
Browse files Browse the repository at this point in the history
Some improvements and fixes (jkbms_ble)
  • Loading branch information
mr-manuel authored Apr 13, 2023
2 parents f5eb089 + 8eabbc4 commit acea6bf
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 36 deletions.
14 changes: 13 additions & 1 deletion etc/dbus-serialbattery/installlocal.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
#!/bin/sh

# install required packages
opkg update
opkg install python3-misc python3-pip
pip3 install bleak

# install driver
tar -zxf ./venus-data.tar.gz -C /data
sh /data/etc/dbus-serialbattery/reinstalllocal.sh
echo "make sure to disable Settings/Bluetooth in the Remote-Console to prevent reconnects every minute. In case of crash after ~12-16 hours disable raspberry pi 3 internal bluetooth via dtoverlay and use an external usb bluetooth-dongle"

# setup cronjob to restart Bluetooth
grep -qxF "5 0,12 * * * /etc/init.d/bluetooth restart" /var/spool/cron/root || echo "5 0,12 * * * /etc/init.d/bluetooth restart" >> /var/spool/cron/root


echo "Make sure to disable Settings -> Bluetooth in the Remote-Console to prevent reconnects every minute."
echo "ATTENTION!"
echo "- At the moment this driver needs a serial to USB adapter attached to start. The serial side hasn't to be connected anywhere."
echo "- If you changed the default connection PIN of JKBMS, then you have to pair the JKBMS first using OS tools like the \"bluetoothctl\"."
echo "See https://wiki.debian.org/BluetoothUser#Using_bluetoothctl for more details."
69 changes: 61 additions & 8 deletions etc/dbus-serialbattery/installqml.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,74 @@
#!/bin/sh
#backup old PageBattery.qml once. New firmware upgrade will remove the backup
#!/bin/bash
set -x

# elaborate version string for better comparing
# https://github.com/kwindrem/SetupHelper/blob/ebaa65fcf23e2bea6797f99c1c41174143c1153c/updateFileSets#L56-L81
function versionStringToNumber ()
{
local local p4="" ; local p5="" ; local p5=""
local major=""; local minor=""

# first character should be 'v' so first awk parameter will be empty and is not prited into the read command
#
# version number formats: v2.40, v2.40~6, v2.40-large-7, v2.40~6-large-7
# so we must adjust how we use paramters read from the version string
# and parsed by awk
# if no beta make sure release is greater than any beta (i.e., a beta portion of 999)

read major minor p4 p5 p6 <<< $(echo $1 | awk -v FS='[v.~-]' '{print $2, $3, $4, $5, $6}')
((versionNumber = major * 1000000000 + minor * 1000000))
if [ -z $p4 ] || [ $p4 = "large" ]; then
((versionNumber += 999))
else
((versionNumber += p4))
fi
if [ ! -z $p4 ] && [ $p4 = "large" ]; then
((versionNumber += p5 * 1000))
large=$p5
elif [ ! -z $p6 ]; then
((versionNumber += p6 * 1000))
fi
}

# get current Venus OS version
versionStringToNumber $(head -n 1 /opt/victronenergy/version)
((venusVersionNumber = $versionNumber))

# revert to VisualItemModel, if before v3.00~14 (v3.00~14 uses VisibleItemModel)
versionStringToNumber "v3.00~14"

qmlDir="/data/etc/dbus-serialbattery/qml"

if (( $venusVersionNumber < $versionNumber )); then
echo "Venus OS $(head -n 1 /opt/victronenergy/version) is olter than v3.00~14. Replacing VisibleItemModel with VisualItemModel"
fileList="$qmlDir/PageBattery.qml"
fileList+=" $qmlDir/PageBatteryCellVoltages.qml"
fileList+=" $qmlDir/PageBatterySetup.qml"
fileList+=" $qmlDir/PageLynxIonIo.qml"
for file in $fileList ; do
sed -i -e 's/VisibleItemModel/VisualItemModel/' "$file"
done
fi


# backup old PageBattery.qml once. New firmware upgrade will remove the backup
if [ ! -f /opt/victronenergy/gui/qml/PageBattery.qml.backup ]; then
cp /opt/victronenergy/gui/qml/PageBattery.qml /opt/victronenergy/gui/qml/PageBattery.qml.backup
fi
if [ ! -f /opt/victronenergy/gui/qml/PageLynxIonIo.qml.backup ]; then
cp /opt/victronenergy/gui/qml/PageLynxIonIo.qml /opt/victronenergy/gui/qml/PageLynxIonIo.qml.backup
fi
#copy new PageBattery.qml

# copy new PageBattery.qml
cp /data/etc/dbus-serialbattery/qml/PageBattery.qml /opt/victronenergy/gui/qml/
#copy new PageLynxIonIo.qml
# copy new PageLynxIonIo.qml
cp /data/etc/dbus-serialbattery/qml/PageLynxIonIo.qml /opt/victronenergy/gui/qml/
#copy new PageBatteryCellVoltages
# copy new PageBatteryCellVoltages
cp /data/etc/dbus-serialbattery/qml/PageBatteryCellVoltages.qml /opt/victronenergy/gui/qml/
cp /data/etc/dbus-serialbattery/qml/PageBatterySetup.qml /opt/victronenergy/gui/qml/
#stop gui
# stop gui
svc -d /service/gui
#sleep 1 sec
# sleep 1 sec
sleep 1
#start gui
# start gui
svc -u /service/gui
20 changes: 18 additions & 2 deletions etc/dbus-serialbattery/jkbms_ble.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,11 @@ def refresh_data(self):
)
self.balancing_action = st["cell_info"]["balancing_action"]

# show wich cells are balancing
for c in range(self.cell_count):
if self.balancing and (
st["cell_info"]["max_voltage_cell"] == c
or st["cell_info"]["min_voltage_cell"] == c
st["cell_info"]["min_voltage_cell"] == c
or st["cell_info"]["max_voltage_cell"] == c
):
self.cells[c].balance = True
else:
Expand Down Expand Up @@ -209,3 +210,18 @@ def reset_bluetooth(self):

def get_balancing(self):
return 1 if self.balancing else 0


def reset_bluetooth(self):
logger.info("reset of bluetooth triggered")
self.resetting = True
# if self.jk.is_running():
# self.jk.stop_scraping()
logger.info("scraping ended, issuing sys-commands")
os.system("kill -9 $(pidof bluetoothd)")
# os.system("/etc/init.d/bluetooth stop") is not enugh, kill -9 via pid is needed
time.sleep(2)
os.system("rfkill block bluetooth")
os.system("rfkill unblock bluetooth")
os.system("/etc/init.d/bluetooth start")
logger.info("bluetooth should have been restarted")
6 changes: 2 additions & 4 deletions etc/dbus-serialbattery/jkbms_brn.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

# zero means parse all incoming data (every second)
CELL_INFO_REFRESH_S = 0
DEVICE_INFO_REFRESH_S = 60 * 60 * 10 # every 10 Hours
CHAR_HANDLE = "0000ffe1-0000-1000-8000-00805f9b34fb"
MODEL_NBR_UUID = "00002a24-0000-1000-8000-00805f9b34fb"

Expand Down Expand Up @@ -380,12 +379,11 @@ async def enable_charging(self, c):
await self.write_register(0x1F, b"\x01\x00\x00\x00", 4, c)
await self.write_register(0x40, b"\x01\x00\x00\x00", 4, c)


'''
if __name__ == "__main__":
jk = JkBmsBle("C8:47:8C:E4:54:0E")
info("sss")
jk.start_scraping()
while True:
print("asdf")
print(jk.get_status())
time.sleep(5)
'''
5 changes: 3 additions & 2 deletions etc/dbus-serialbattery/qml/PageBattery.qml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ MbPage {
}
}

model: VisualItemModel {
model: VisibleItemModel {
MbItemOptions {
description: qsTr("Switch")
bind: service.path("/Mode")
Expand Down Expand Up @@ -310,6 +310,7 @@ MbPage {

MbSubMenu {
property VBusItem allowToCharge: VBusItem { bind: service.path("/Io/AllowToCharge") }
property VBusItem allowToBalance: VBusItem { bind: service.path("/Io/AllowToBalance") }

description: qsTr("IO")
subpage: Component {
Expand All @@ -318,7 +319,7 @@ MbPage {
bindPrefix: service.path("")
}
}
show: allowToCharge.valid
show: allowToCharge.valid || allowToBalance.valid
}

MbSubMenu {
Expand Down
18 changes: 9 additions & 9 deletions etc/dbus-serialbattery/qml/PageBatteryCellVoltages.qml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ MbPage {
property string c24: _b24.valid && _b24.text == "1" ? "#ff0000" : "#ddd"
title: service.description + " | Cell Voltages"

model: VisualItemModel {
model: VisibleItemModel {

MbItemRow {
description: qsTr("Cells Sum")
Expand Down Expand Up @@ -142,21 +142,21 @@ MbPage {
height: 22
show: volt17.valid
values: [
MbTextBlock { item: volt17; width: 70; height: 20; color: c13 },
MbTextBlock { item: volt18; width: 70; height: 20; color: c14 },
MbTextBlock { item: volt19; width: 70; height: 20; color: c15 },
MbTextBlock { item: volt20; width: 70; height: 20; color: c16 }
MbTextBlock { item: volt17; width: 70; height: 20; color: c17 },
MbTextBlock { item: volt18; width: 70; height: 20; color: c18 },
MbTextBlock { item: volt19; width: 70; height: 20; color: c19 },
MbTextBlock { item: volt20; width: 70; height: 20; color: c20 }
]
}
MbItemRow {
description: qsTr("Cells (21/22/23/24)")
height: 22
show: volt21.valid
values: [
MbTextBlock { item: volt21; width: 70; height: 20; color: c13 },
MbTextBlock { item: volt22; width: 70; height: 20; color: c14 },
MbTextBlock { item: volt23; width: 70; height: 20; color: c15 },
MbTextBlock { item: volt24; width: 70; height: 20; color: c16 }
MbTextBlock { item: volt21; width: 70; height: 20; color: c21 },
MbTextBlock { item: volt22; width: 70; height: 20; color: c22 },
MbTextBlock { item: volt23; width: 70; height: 20; color: c23 },
MbTextBlock { item: volt24; width: 70; height: 20; color: c24 }
]
}
}
Expand Down
2 changes: 1 addition & 1 deletion etc/dbus-serialbattery/qml/PageBatterySetup.qml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ MbPage {

title: service.description + " | Cell Voltages"

model: VisualItemModel {
model: VisibleItemModel {

MbSpinBox {
description: qsTr("Maximum charge current")
Expand Down
14 changes: 7 additions & 7 deletions etc/dbus-serialbattery/qml/PageLynxIonIo.qml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ MbPage {
id: root
property string bindPrefix

model: VisualItemModel {
model: VisibleItemModel {
MbItemOptions {
id: systemSwitch
description: qsTr("System Switch")
Expand Down Expand Up @@ -37,16 +37,16 @@ MbPage {
]
}

MbItemOptions {
description: qsTr("Allow to balance")
bind: service.path("/Io/AllowToBalance")
readonly: true
show: item.valid
MbItemOptions {
description: qsTr("Allow to balance")
bind: service.path("/Io/AllowToBalance")
readonly: true
show: item.valid
possibleValues:[
MbOption{description: qsTr("No"); value: 0},
MbOption{description: qsTr("Yes"); value: 1}
]
}
}

MbItemOptions {
description: qsTr("External relay")
Expand Down
6 changes: 6 additions & 0 deletions etc/dbus-serialbattery/restart-driver-and-ble.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
sh /data/etc/dbus-serialbattery/reinstalllocal.sh

sh /data/etc/dbus-serialbattery/restartservice.sh

/etc/init.d/bluetooth restart
4 changes: 2 additions & 2 deletions etc/dbus-serialbattery/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
]

# Constants - Need to dynamically get them in future
DRIVER_VERSION = 0.14
DRIVER_SUBVERSION = "b3ble"
DRIVER_VERSION = '0.15'
DRIVER_SUBVERSION = ".0-ble"
zero_char = chr(48)
degree_sign = "\N{DEGREE SIGN}"

Expand Down

0 comments on commit acea6bf

Please sign in to comment.