Calculate power factor #10

Merged
sqozz merged 4 commits from cfr34k/sem6000:power_factor into master 2020-01-20 21:09:36 +01:00
2 changed files with 14 additions and 6 deletions

View file

@ -18,7 +18,7 @@ while True:
# -1: infinite retries # -1: infinite retries
# integer: seconds before exception is thrown # 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.") print("Connected.")
#socket.login("1337") #socket.login("1337")
@ -28,8 +28,8 @@ while True:
socket.getStatus() socket.getStatus()
socket.setStatus(True) socket.setStatus(True)
print("=== {} ({}) ===".format(socket.mac_address, "on" if socket.powered else "off")) 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): except (SEMSocket.NotConnectedException, bluepy.btle.BTLEDisconnectError, BrokenPipeError):
print("Restarting...") print("Restarting...")
if socket != None: if socket != None:
socket.disconnect() socket.disconnect()

View file

@ -8,6 +8,7 @@ class SEMSocket():
voltage = 0 voltage = 0
current = 0 current = 0
power = 0 power = 0
power_factor = 0
frequency = 0 frequency = 0
mac_address = "" mac_address = ""
custom_service = None custom_service = None
@ -193,11 +194,18 @@ class SEMSocket():
print("Switch toggled") print("Switch toggled")
self.__btle_device.getStatus() self.__btle_device.getStatus()
elif message_type == 0x04: #status related data elif message_type == 0x04: #status related data
self.__btle_device.voltage = data[8] voltage = data[8]
self.__btle_device.current = (data[9] << 8 | data[10]) / 1000 current = (data[9] << 8 | data[10]) / 1000
self.__btle_device.power = (data[5] << 16 | data[6] << 8 | data[7]) / 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.frequency = data[11]
self.__btle_device.powered = bool(data[4]) self.__btle_device.powered = bool(data[4])
# calculated values
self.__btle_device.power_factor = power / (voltage * current)
elif message_type == 0x17: elif message_type == 0x17:
if data[5] == 0x00 or data[5] == 0x01: if data[5] == 0x00 or data[5] == 0x01:
if data[4]: if data[4]: