-
Notifications
You must be signed in to change notification settings - Fork 48
/
BLEHeartRateLogger.py
executable file
·347 lines (277 loc) · 10.8 KB
/
BLEHeartRateLogger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/python
# -*- encoding: utf-8 -*-
"""
BLEHeartRateLogger
~~~~~~~~~~~~~~~~~~~
A tool to log your heart rate using a Bluetooth low-energy (BLE) heart rate
monitor (HRM). The tool uses system commands (hcitool and gatttool) to
connect to the BLE HRM and parses the output of the tools. Data is
interpreted according to the Bluetooth specification for HRM and saved in a
sqlite database for future processing. In case the connection with the BLE
HRM is lost, connection is restablished.
:copyright: (c) 2015 by fg1
:license: BSD, see LICENSE for more details
"""
__version__ = "0.1.1"
import os
import sys
import time
import logging
import sqlite3
import pexpect
import argparse
import configparser
logging.basicConfig(format="%(asctime)-15s %(message)s")
log = logging.getLogger("BLEHeartRateLogger")
def parse_args():
"""
Command line argument parsing
"""
parser = argparse.ArgumentParser(description="Bluetooth heart rate monitor data logger")
parser.add_argument("-m", metavar='MAC', type=str, help="MAC address of BLE device (default: auto-discovery)")
parser.add_argument("-b", action='store_true', help="Check battery level")
parser.add_argument("-g", metavar='PATH', type=str, help="gatttool path (default: system available)", default="gatttool")
parser.add_argument("-o", metavar='FILE', type=str, help="Output filename of the database (default: none)")
parser.add_argument("-H", metavar='HR_HANDLE', type=str, help="Gatttool handle used for HR notifications (default: none)")
parser.add_argument("-v", action='store_true', help="Verbose output")
parser.add_argument("-d", action='store_true', help="Enable debug of gatttool")
confpath = os.path.join(os.path.dirname(os.path.realpath(__file__)), "BLEHeartRateLogger.conf")
if os.path.exists(confpath):
config = configparser.ConfigParser()
config.read([confpath])
config = dict(config.items("config"))
# We compare here the configuration given in the config file with the
# configuration of the parser.
args = vars(parser.parse_args([]))
err = False
for key in config.keys():
if key not in args:
log.error("Configuration file error: invalid key '" + key + "'.")
err = True
if err:
sys.exit(1)
parser.set_defaults(**config)
return parser.parse_args()
def interpret(data):
"""
data is a list of integers corresponding to readings from the BLE HR monitor
"""
byte0 = data[0]
res = {}
res["hrv_uint8"] = (byte0 & 1) == 0
sensor_contact = (byte0 >> 1) & 3
if sensor_contact == 2:
res["sensor_contact"] = "No contact detected"
elif sensor_contact == 3:
res["sensor_contact"] = "Contact detected"
else:
res["sensor_contact"] = "Sensor contact not supported"
res["ee_status"] = ((byte0 >> 3) & 1) == 1
res["rr_interval"] = ((byte0 >> 4) & 1) == 1
if res["hrv_uint8"]:
res["hr"] = data[1]
i = 2
else:
res["hr"] = (data[2] << 8) | data[1]
i = 3
if res["ee_status"]:
res["ee"] = (data[i + 1] << 8) | data[i]
i += 2
if res["rr_interval"]:
res["rr"] = []
while i < len(data):
# Note: Need to divide the value by 1024 to get in seconds
res["rr"].append((data[i + 1] << 8) | data[i])
i += 2
return res
def insert_db(sq, res, period, min_ce=2, max_ce=60 * 2, grace_commit=2 / 3.):
"""
Inserts data into the database
"""
if not hasattr(insert_db, "i"):
insert_db.i = 0
if not hasattr(insert_db, "commit_every"):
insert_db.commit_every = 5
tstamp = int(time.time())
if "rr" in res:
for rr in res["rr"]:
sq.execute("INSERT INTO hrm VALUES (?, ?, ?)", (tstamp, res["hr"], rr))
else:
sq.execute("INSERT INTO hrm VALUES (?, ?, ?)", (tstamp, res["hr"], -1))
# Instead of pushing the data to the disk each time, we commit only every
# 'commit_every'.
if insert_db.i < insert_db.commit_every:
insert_db.i += 1
else:
t = time.time()
sq.commit()
delta_t = time.time() - t
log.debug("sqlite commit time: " + str(delta_t))
sq.execute("INSERT INTO sql VALUES (?, ?, ?)", (int(t), delta_t, insert_db.commit_every))
# Because the time for commiting to the disk is not known in advance,
# we measure it and automatically adjust automatically 'commit_every'
# following a rule similar to TCP Reno.
if delta_t < period * grace_commit:
insert_db.commit_every = min(insert_db.commit_every + 1, max_ce)
else:
insert_db.commit_every = max(insert_db.commit_every / 2, min_ce)
insert_db.i = 0
def get_ble_hr_mac():
"""
Scans BLE devices and returs the address of the first device found.
"""
while 1:
log.info("Trying to find a BLE device")
hci = pexpect.spawn("hcitool lescan")
try:
hci.expect("([0-9A-F]{2}[:-]){5}([0-9A-F]{2})", timeout=20)
addr = hci.match.group(0)
hci.close()
break
except pexpect.TIMEOUT:
time.sleep(20)
continue
except KeyboardInterrupt:
log.info("Received keyboard interrupt. Quitting cleanly.")
hci.close()
return None
# We wait for the 'hcitool lescan' to finish
time.sleep(1)
return addr
def main(addr=None, sqlfile=None, gatttool="gatttool", check_battery=False, hr_handle=None, debug_gatttool=False):
"""
main routine to which orchestrates everything
"""
if sqlfile is not None:
# Init database connection
sq = sqlite3.connect(sqlfile)
with sq:
sq.execute("CREATE TABLE IF NOT EXISTS hrm (tstamp INTEGER, hr INTEGER, rr INTEGER)")
sq.execute("CREATE TABLE IF NOT EXISTS sql (tstamp INTEGER, commit_time REAL, commit_every INTEGER)")
if addr is None:
# In case no address has been provided, we scan to find any BLE devices
addr = get_ble_hr_mac()
if addr == None:
sq.close()
return
hr_ctl_handle = None
retry = True
while retry:
while 1:
log.info("Establishing connection to " + addr)
gt = pexpect.spawn(gatttool + " -b " + addr + " -t random --interactive")
if debug_gatttool:
gt.logfile = sys.stdout
gt.expect(r"\[LE\]>")
gt.sendline("connect")
try:
i = gt.expect(["Connection successful.", r"\[CON\]"], timeout=30)
if i == 0:
gt.expect(r"\[LE\]>", timeout=30)
except pexpect.TIMEOUT:
log.info("Connection timeout. Retrying.")
continue
except KeyboardInterrupt:
log.info("Received keyboard interrupt. Quitting cleanly.")
retry = False
break
break
if not retry:
break
log.info("Connected to " + addr)
if check_battery:
gt.sendline("char-read-uuid 00002a19-0000-1000-8000-00805f9b34fb")
try:
gt.expect("value: ([0-9a-f]+)")
battery_level = gt.match.group(1)
log.info("Battery level: " + str(int(battery_level, 16)))
except pexpect.TIMEOUT:
log.error("Couldn't read battery level.")
if hr_handle == None:
# We determine which handle we should read for getting the heart rate
# measurement characteristic.
gt.sendline("char-desc")
while 1:
try:
gt.expect(r"handle: (0x[0-9a-f]+), uuid: ([0-9a-f]{8})", timeout=10)
except pexpect.TIMEOUT:
break
handle = gt.match.group(1).decode()
uuid = gt.match.group(2).decode()
if uuid == "00002902" and hr_handle:
hr_ctl_handle = handle
break
elif uuid == "00002a37":
hr_handle = handle
if hr_handle == None:
log.error("Couldn't find the heart rate measurement handle?!")
return
if hr_ctl_handle:
# We send the request to get HRM notifications
gt.sendline("char-write-req " + hr_ctl_handle + " 0100")
# Time period between two measures. This will be updated automatically.
period = 1.
last_measure = time.time() - period
hr_expect = "Notification handle = " + hr_handle + " value: ([0-9a-f ]+)"
while 1:
try:
gt.expect(hr_expect, timeout=10)
except pexpect.TIMEOUT:
# If the timer expires, it means that we have lost the
# connection with the HR monitor
log.warn("Connection lost with " + addr + ". Reconnecting.")
if sqlfile is not None:
sq.commit()
gt.sendline("quit")
try:
gt.wait()
except:
pass
time.sleep(1)
break
except KeyboardInterrupt:
log.info("Received keyboard interrupt. Quitting cleanly.")
retry = False
break
# We measure here the time between two measures. As the sensor
# sometimes sends a small burst, we have a simple low-pass filter
# to smooth the measure.
tmeasure = time.time()
period = period + 1 / 16. * ((tmeasure - last_measure) - period)
last_measure = tmeasure
# Get data from gatttool
datahex = gt.match.group(1).strip()
data = map(lambda x: int(x, 16), datahex.split(b' '))
res = interpret(list(data))
log.debug(res)
if sqlfile is None:
log.info("Heart rate: " + str(res["hr"]))
continue
# Push the data to the database
insert_db(sq, res, period)
if sqlfile is not None:
# We close the database properly
sq.commit()
sq.close()
# We quit close the BLE connection properly
gt.sendline("quit")
try:
gt.wait()
except:
pass
def cli():
"""
Entry point for the command line interface
"""
args = parse_args()
if args.g != "gatttool" and not os.path.exists(args.g):
log.critical("Couldn't find gatttool path!")
sys.exit(1)
# Increase verbose level
if args.v:
log.setLevel(logging.DEBUG)
else:
log.setLevel(logging.INFO)
main(args.m, args.o, args.g, args.b, args.H, args.d)
if __name__ == "__main__":
cli()