From 2cef664c7f72abaf6039b0f8ebf44b8addb09925 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Odd=20Str=C3=A5b=C3=B8?= Date: Sat, 6 Nov 2021 19:54:50 +0100 Subject: [PATCH] Fix temperature parsing when below zero --- solar_ble.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/solar_ble.py b/solar_ble.py index 4403fef..15337d4 100755 --- a/solar_ble.py +++ b/solar_ble.py @@ -161,6 +161,12 @@ CMD_ = b"\xff\x78\x00\x00\x00\x01" STATUS = {} +def parse_temperature(bin): + if bin & 0x80: + return (bin & 0x7F) * -1 + return bin & 0x7F + + # GET_BATTERY_STATE def parse_battery_state(data: bytes) -> dict: res = dict( @@ -175,13 +181,15 @@ def parse_battery_state(data: bytes) -> dict: "load_current", "load_power", ), - struct.unpack("!HHHbbHHH", data), + struct.unpack("!HHHBBHHH", data), ) ) res["battery_voltage"] /= 10 res["battery_current"] /= 100 res["load_voltage"] /= 10 res["load_current"] /= 100 + res["_internal_temperature?"] = parse_temperature(res["_internal_temperature?"]) + res["battery_temperature"] = parse_temperature(res["battery_temperature"]) STATUS.update(res) log(str(res)) @@ -246,7 +254,7 @@ def parse_packet(data): def readMemory(fh: RawIOBase, address: int, words: int = 1): - log(f"Reading {words} words from 0x{address:04X}") + # log(f"Reading {words} words from 0x{address:04X}") write(fh, construct_request(address, words=words)) header = fh.read(3) if header and len(header) == 3: