Over the years

So I get to this point every year, look back on my fitness activity and think “should’ve done better.” Except for 2019 I’m feeling pretty good about it, but to make sure that I’m not just kidding myself I needed some data and that meant I first needed to import 7 years of data from Garmin into Strava so all of my activity data was in one place. I actually have data going back to 2008 but it’s a bit sparse so for the sake of the table below I’ve dropped it.

Importing all the things from Garmin into Strava was made easy thanks to garminexport (well I had a local archive of 1300+ activities that I had to manually import, 25 at a time, and check were assigned to the correct activity type.)

Amusing to note that as I was adding the entries I spotted a 3km run in 2015 that took me 22 minutes. In 2019 I managed to get my Parkrun PB down to 22:13 ;-)

So with the data all in one place, time to look at it (a bit)…. using this handy site I was able to quickly see annual summary data which is below, the biggest surprise for me is the lack of swimming, oh and a lot of the riding is made up of bike commuting. The interesting part (for me) is the improvement in my running pace over the last few years.

2012 2013 2014 2015 2016 2017 2018 2019
Count 10 12 79 151 163 53 84 74
413 262 423 399 672 250 394 350
21:53:51 10:50:22 23:56:42 21:09:31 34:24:52 13:34:27 21:50:58 18:00:36
Count 58 37 33 31 85 89 71 131
358 247 201 211 238 457 270 847
35:49:27 26:55:34 23:54:00 23:58:01 30:28:49 52:12:56 27:29:12 78:02:32
Avg / Speed
6:00 6:32 7:08 6:50 7:42 6:51 6:07 5:32
Count 5 1 23 44
8 1 14 55
02:34:34 00:46:34 03:57:34 15:40:27

This is due to my “if you want to run faster, run faster” brain wave I had in the middle of 2018, where I suddenly worked out what I needed to do to stop my parkrun taking so long (and getting longer) and to just generally improve my running, going from a tedious and sometime boring plod, into something that almost feels like running. Just need to apply it to my swimming now ;-)

Pi + Bluetooth = Is the track boss going to have a heart attack

So I had an idea that fell out of being Track Boss at re:Invent for a few hours each day during the DeepRacer championship. Wouldn’t it be interesting to see what the heart rate and step count of the track boss was….?

So quick bit of DuckDuckGo(ing) later and I had a Polar H10 on the way and was looking at how I could get going – this post on RepRage formed the basis of some early work.

So I was able to scan for my device:

$ sudo hcitool lescan
 LE Scan …
 F4:DF:3F:95:DE:EA (unknown)
 F4:DF:3F:95:DE:EA Polar H10 65AAF325

But trying to connect to it failed using hcitool, so I switched to using gatttool with success (connection and data):

$ gatttool -t random -b F4:DF:3F:95:DE:EA -I
 [F4:DF:3F:95:DE:EA][LE]> connect
 Attempting to connect to F4:DF:3F:95:DE:EA
 Connection successful
 [F4:DF:3F:95:DE:EA][LE]> characteristics
 handle: 0x0002, char properties: 0x02, char value handle: 0x0003, uuid: 00002a00-0000-1000-8000-00805f9b34fb
 handle: 0x0004, char properties: 0x02, char value handle: 0x0005, uuid: 00002a01-0000-1000-8000-00805f9b34fb
 handle: 0x0006, char properties: 0x02, char value handle: 0x0007, uuid: 00002a04-0000-1000-8000-00805f9b34fb
 handle: 0x0008, char properties: 0x02, char value handle: 0x0009, uuid: 00002aa6-0000-1000-8000-00805f9b34fb
 handle: 0x000b, char properties: 0x20, char value handle: 0x000c, uuid: 00002a05-0000-1000-8000-00805f9b34fb

So that’s some information from the strap, now to subscribe to notifications to get the heart rate data:

 [F4:DF:3F:95:DE:EA][LE]> char-write-req 0x0011 0100
 Characteristic value was written successfully
 Notification handle = 0x0010 value: 10 40 e4 03 
 Notification handle = 0x0010 value: 10 40 92 03 
 Notification handle = 0x0010 value: 10 40 7e 03 
 Notification handle = 0x0010 value: 10 41 8f 03 76 03

For me I get ~79 notifications before an error, however for now getting something back is better than nothing especially given the second “value” is the important one, my heart rate in hexadecimal. So we can (sort of) read the data from the strap, now to do this in code.

Python is my current go to language so it was time to add some libraries and see what we could get working:

$ sudo apt-get install -y python3 python3-pip libglib2.0-dev
$ sudo pip3 install bluepy

Adding in MQTT and python libraries so the data can be used in a presentation layer:

$ sudo apt-get install -y mosquitto mosquitto-clients
$ sudo systemctl enable mosquitto.service
$ sudo pip3 install argparse paho-mqtt

My code is still bombing out with a connection error though after 147 notifications from bluepy. On the up side I’m not the only hitting this issue, on the down side there doesn’t appear to be a decent fix. For me running:

$ hcitool con
         < LE F4:DF:3F:95:DE:EA handle 64 state 1 lm MASTER 

To find out the connection handle (in this case 64) followed by:

$ sudo hcitool lecup --handle 64 --min 250 --max 400 --latency 0 --timeout 600

Fixes the problem (todo: make this happen using magic)

So with the code below:

import datetime
import bluepy.btle as btle
import paho.mqtt.client as mqtt
import argparse
import json

packets = 0

class MyDelegate(btle.DefaultDelegate):
    def __init__(self):

    def handleNotification(self, cHandle, data):
        global packets 
        packets += 1

        global hr
        hr = str(data[1])

        global time
        time = datetime.datetime.now().time()
        print("time: {} packet: {} Handle: {} HR (bpm): {}".format(time, packets, cHandle, data[1]))

parser = argparse.ArgumentParser(description="Connect to Polar H10 HRM")
parser.add_argument('device', type=str, help='HRM strap device ID')

args = parser.parse_args()
print('args: {}'.format(args.device))

p = btle.Peripheral(args.device, addrType="random")

#start hr notification
service_uuid = 0x180D
svc = p.getServiceByUUID(service_uuid)
ch = svc.getCharacteristics()[0]
desc = ch.getDescriptors()[0]
desc.write(b"\x01\x00", True)

broker_url = ""
broker_port = 1883

client = mqtt.Client()
client.connect(broker_url, broker_port)

# listen for notifications
while True:
    if p.waitForNotifications(1.0):
        payload = json.dumps({'time': str(time), 'heart_rate': hr})
        client.publish(topic="TrackBossHRM", payload=str(payload), qos=0, retain=False)

I have continuous heart rate data getting added into an MQTT based queue for use elsewhere…

$ mosquitto_sub -d -t TrackBossHRMClient mosqsub|2671-raspberryp sending CONNECT
 Client mosqsub|2671-raspberryp received CONNACK (0)
 Client mosqsub|2671-raspberryp sending SUBSCRIBE (Mid: 1, Topic: TrackBossHRM, QoS: 0)
 Client mosqsub|2671-raspberryp received SUBACK
 Subscribed (mid: 1): 0
 Client mosqsub|2671-raspberryp received PUBLISH (d0, q0, r0, m0, 'TrackBossHRM', … (47 bytes))
 {"time": "17:26:43.999799", "heart_rate": "72"}
 Client mosqsub|2671-raspberryp received PUBLISH (d0, q0, r0, m0, 'TrackBossHRM', … (47 bytes))
 {"time": "17:26:44.997310", "heart_rate": "72"}

Now to do something with it.