From e7d5940f360f7c86da85d1a348d1941d94868ff4 Mon Sep 17 00:00:00 2001 From: Arthur Davis Date: Fri, 23 Jul 2021 16:52:39 -0600 Subject: [PATCH] add support for v2 devices --- src/airthingswave-mqtt/__main__.py | 4 +-- src/airthingswave-mqtt/airthingswave.py | 37 ++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/airthingswave-mqtt/__main__.py b/src/airthingswave-mqtt/__main__.py index 4d7e2a4..a672870 100644 --- a/src/airthingswave-mqtt/__main__.py +++ b/src/airthingswave-mqtt/__main__.py @@ -22,9 +22,7 @@ def main(): i = 0 while i < count: print(atw.waves[i]["name"], atw.waves[i]["addr"]) - handle = atw.ble_connect(atw.waves[i]["addr"]) - r = atw.get_readings(handle) - atw.ble_disconnect(handle) + r = atw.get_readings(i) print("{0} says Date: {1} Temp: {2} Humidity: {3} 24H: {4} Long term: {5}".format(atw.waves[i]["name"], r["DateTime"], r["Temperature"], r["Humidity"], r["Radon-Day"], r["Radon-Long-Term"], )) atw.publish_readings(atw.waves[i]["name"], r) i = i+1 diff --git a/src/airthingswave-mqtt/airthingswave.py b/src/airthingswave-mqtt/airthingswave.py index 0753123..20eb43f 100644 --- a/src/airthingswave-mqtt/airthingswave.py +++ b/src/airthingswave-mqtt/airthingswave.py @@ -51,6 +51,10 @@ def __init__(self, config_file): self.mqtt_client = mqtt.Client() self.mqtt_connect(self.config) self.mqtt_conf = [] + + # These are only used for the wave version 1 devices. The code for the version 2 devices hard-codes the order + # of these sensors when it outputs device readings, so update the V2 order in get_readings_v2() if changing + # the list of sensors here. self.sensors = [] self.sensors.append(Sensor("DateTime", UUID(0x2A08), 'HBBBBB', "\t", 0)) self.sensors.append(Sensor("Temperature", UUID(0x2A6E), 'h', "deg C\t", 1.0/100.0)) @@ -72,6 +76,8 @@ def check_config(self, conf): for wave in conf["waves"]: if "addr" in wave and "name" in wave: self.waves.append(wave) + if "version" not in wave: + self.waves[-1]["version"] = 1 else: print("Malformed config item: {0}".format(wave)) return True @@ -95,7 +101,17 @@ def ble_disconnect(self, p): # # Given a peripheral handle, populate readings for that peripheral # - def get_readings(self, p): + def get_readings(self, i): + readings = dict() + handle = self.ble_connect(self.waves[i]["addr"]) + if self.waves[i]["version"] == 1: + readings = self.get_readings_v1(handle) + elif self.waves[i]["version"] == 2: + readings = self.get_readings_v2(handle) + self.ble_disconnect(handle) + return readings + + def get_readings_v1(self, p): readings = dict() for s in self.sensors: ch = p.getCharacteristics(uuid=s.uuid)[0] @@ -109,6 +125,25 @@ def get_readings(self, p): return readings + def get_readings_v2(self, p): + readings = dict() + + # The V2 waves return all sensor readings as a single characteristic + ch = p.getCharacteristics(uuid=UUID("b42e4dcc-ade7-11e4-89d3-123b93f75cba"))[0] + val = ch.read() + data = struct.unpack("<4B8H", val) + + if data[0] != 1: + raise ValueError("Unsupported version from wave device (Expected 1, got{})".format(data[0])) + + readings["DateTime"] = str(datetime.now()) + readings["Temperature"] = str(data[6]/100.0) + readings["Humidity"] = str(data[1]/2.0) + readings["Radon-Day"] = str(data[4]) + readings["Radon-Long-Term"] = str(data[5]) + + return readings + def publish_readings(self, name, readings): print("name: {0} readings: {1}".format(name, readings)) for s in self.sensors: