Skip to content
Snippets Groups Projects
Commit c63eaa95 authored by schneider's avatar schneider
Browse files

iaq: let script write directly to influxdb

parent 65e10a47
Branches
No related tags found
1 merge request!508tools: add IAQ example in Python
Pipeline #5455 failed
#!/usr/bin/env python3
# Written 2021 by Stefan `Sec` Zehl
import argparse
import struct
import os
import select
import sys
from bluez import Manager
parser = argparse.ArgumentParser(description='Receive IAQ values from Card10')
parser.add_argument('--mac', help='mac of device to query')
parser.add_argument('-v', '--verbose', help='help debug', action='count', default=0)
parser.add_argument('-m', '--mac', help='mac of device to query')
parser.add_argument('-i', '--influx', metavar='URL',
help='url of influxdb to push to. E.g.: http://influxdb:8086/write?db=card10')
args = parser.parse_args()
mgr = Manager();
if args.influx is not None:
import requests
mgr = Manager()
a = mgr.get_adapter()
#print('Using adapter', a)
# Get devices which support environmental sensing
devices=a.get_devices('0000181a-0000-1000-8000-00805f9b34fb')
device=None
if len(devices)>1:
print("Mutiple devices found:")
for d in devices:
......@@ -36,30 +43,28 @@ if len(devices)>1:
print("")
elif len(devices)==1:
device=devices[0]
if args.verbose:
print("Using device", device.Name, device.Address)
else:
print("No capable device found")
sys.exit(1)
#print('Device:', device)
device.connect()
services = device.get_gattservices();
services = device.get_gattservices()
def p_temp(data):
tmp=struct.unpack('=H', data)
t=tmp[0]
return {'temp': tmp[0]/100}
return {'temperature': tmp[0]/100}
def p_humidity(data):
tmp=struct.unpack('H', data)
t=tmp[0]
return {'humidity': t/100}
return {'humidity': tmp[0]/100}
def p_pressure(data):
tmp=struct.unpack('I', data)
t=tmp[0]
return {'pressure': t/1000}
return {'pressure': tmp[0]/1000}
def p_iaq(data):
names=['iaq_accuracy','iaq','co2']
......@@ -81,19 +86,40 @@ for suuid,svc in services.items():
for uuid,char in chars.items():
# print(" - ", uuid)
if uuid in characteristics:
if args.verbose:
print("Found", uuid)
an=char.AcquireNotify() # (fd, mtu)
mapping[an[0]]=characteristics[uuid]
fds.append(an[0])
if len(fds)<len(characteristics):
print("WARN: Could only subscribe to %d characteristics"%len(fds), file=sys.stderr)
else:
print("Subscribed to %d characteristics on %s (%s)"%(len(fds), device.Name, device.Address))
post=None
while True:
if args.influx is not None:
post={}
ready,_,_ = select.select(fds,[],[])
for fd in ready:
print("notify:",end=" ")
data=os.read(fd, 100) #read max 100 characters
parse=mapping[fd](data)
print(parse)
ble_data=os.read(fd, 100) #read max 100 characters
parse=mapping[fd](ble_data)
if args.influx is not None:
post.update(parse)
else:
print("notify:", parse)
if args.influx is not None:
line=" ".join(["air",",".join([k+"="+str(v) for k,v in post.items()])])
if args.verbose:
print(line)
r = requests.post(args.influx, data=line)
if r.status_code != 204:
print(r.status_code, r.reason)
#device.disconnect()
print('Exit')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment