Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for v2 devices #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/airthingswave-mqtt/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 36 additions & 1 deletion src/airthingswave-mqtt/airthingswave.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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]
Expand All @@ -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:
Expand Down