From 8f06b28f316d6dacca89d748148909ff321c87ed Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Sun, 19 Jan 2020 17:34:18 +0100 Subject: [PATCH 1/4] Added power factor calculation --- sem6000.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/sem6000.py b/sem6000.py index 7622a78..0491ed8 100644 --- a/sem6000.py +++ b/sem6000.py @@ -8,6 +8,7 @@ class SEMSocket(): voltage = 0 current = 0 power = 0 + power_factor = 0 frequency = 0 mac_address = "" custom_service = None @@ -193,11 +194,18 @@ class SEMSocket(): print("Switch toggled") self.__btle_device.getStatus() elif message_type == 0x04: #status related data - self.__btle_device.voltage = data[8] - self.__btle_device.current = (data[9] << 8 | data[10]) / 1000 - self.__btle_device.power = (data[5] << 16 | data[6] << 8 | data[7]) / 1000 + voltage = data[8] + current = (data[9] << 8 | data[10]) / 1000 + power = (data[5] << 16 | data[6] << 8 | data[7]) / 1000 + + self.__btle_device.voltage = voltage + self.__btle_device.current = current + self.__btle_device.power = power self.__btle_device.frequency = data[11] self.__btle_device.powered = bool(data[4]) + + # calculated values + self.__btle_device.power_factor = power / (voltage * current) elif message_type == 0x17: if data[5] == 0x00 or data[5] == 0x01: if data[4]: -- 2.45.2 From 01cc1ed9b7115ccbcdb4ea466e0aeb76d57a5e1a Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Sun, 19 Jan 2020 17:34:57 +0100 Subject: [PATCH 2/4] example: removed invalid constructor argument --- example.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example.py b/example.py index b36b2fd..c7a2a5a 100644 --- a/example.py +++ b/example.py @@ -18,7 +18,7 @@ while True: # -1: infinite retries # integer: seconds before exception is thrown - socket = SEMSocket('f0:c7:7f:0d:e7:17', auto_reconnect_timeout=None) + socket = SEMSocket('f0:c7:7f:0d:e7:17') print("Connected.") #socket.login("1337") -- 2.45.2 From 2f13d6f6a513f20b7e88747a9f802af52d565b9d Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Sun, 19 Jan 2020 17:46:55 +0100 Subject: [PATCH 3/4] example: print power factor --- example.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example.py b/example.py index c7a2a5a..5758b0c 100644 --- a/example.py +++ b/example.py @@ -28,7 +28,7 @@ while True: socket.getStatus() socket.setStatus(True) print("=== {} ({}) ===".format(socket.mac_address, "on" if socket.powered else "off")) - print("\t{}V {}A → {}W@{}Hz".format(socket.voltage, socket.current, socket.power, socket.frequency)) + print("\t{}V {}A → {}W@{}Hz (PF: {})".format(socket.voltage, socket.current, socket.power, socket.frequency, socket.power_factor)) except (SEMSocket.NotConnectedException, bluepy.btle.BTLEDisconnectError): print("Restarting...") if socket != None: -- 2.45.2 From 7b82461f032f543d20482829abaca49a4002ef60 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Sun, 19 Jan 2020 17:48:02 +0100 Subject: [PATCH 4/4] example: restart on BrokenPipeError --- example.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example.py b/example.py index 5758b0c..b8cd2f5 100644 --- a/example.py +++ b/example.py @@ -29,7 +29,7 @@ while True: socket.setStatus(True) print("=== {} ({}) ===".format(socket.mac_address, "on" if socket.powered else "off")) print("\t{}V {}A → {}W@{}Hz (PF: {})".format(socket.voltage, socket.current, socket.power, socket.frequency, socket.power_factor)) - except (SEMSocket.NotConnectedException, bluepy.btle.BTLEDisconnectError): + except (SEMSocket.NotConnectedException, bluepy.btle.BTLEDisconnectError, BrokenPipeError): print("Restarting...") if socket != None: socket.disconnect() -- 2.45.2