Reading from a Polar H7 Bluetooth Heartrate sensor



  • I have tried to connect to and read the pulse data from a Polar H7 sensor but is not managing to get it to work. Any ideas?
    The error I get is:
    Got services...
    Reading from service 180d
    Print chars found
    char 2a37
    Reading pulse data every second...
    Exception caught...
    the requested operation failed

    Here is the code:

    from network import Bluetooth
    import time
    bt = Bluetooth()
    bt.start_scan(-1)
    
    # Code to connect to Polar H7 Bluetooth heartrate sensor
    # The detected Heartrate is "published" over the LoRaWAN network
    
    while True:
        adv = bt.get_adv()
        if adv:
            name = bt.resolve_adv_data(adv.data, Bluetooth.ADV_NAME_CMPL)
            if name is not None:
                if name.startswith("Polar H7"):
                    print ("Found: " + name)
                    bt.stop_scan()
                    try:
                        conn = bt.connect(adv.mac)
                        print("Connected...")
                        services = conn.services()
                        print("Got services...")
                        for service in services:
                            time.sleep(0.05)
                            # According to specification from Polar, service.uuid = 0x180D is Heart Rate and char.uuid = 2a37 is value in beats per minutes
                            if service.uuid() == 0x180D:
                                print('Reading from service %x' % service.uuid())
                                chars = service.characteristics()
                                print("Print chars found")
                                for char in chars:
                                    print('char %x' % char.uuid())
                                    # Charactersistic 2a37 is a notification char and we need to enable notification to receive the ValueError
                                    if char.uuid() == 0x2a37:
                                        # Notify sensor that we want to receive pulse data.
                                        byte = bytes([1])
                                        char.write(byte)
                                        while True:
                                            print("Reading pulse data every second...")
                                            print('char {} value = {}'.format(char.uuid(), char.read()))
                    except Exception as ex:
                        print("Exception caught...")
                        print(ex)


  • I was afraid that the notification part was missing in the firmware but great if it appears soon :-). Thanks for the speedy reply.



  • @arneme
    The Heart Rate Measurement profile requires BLE notifications, it doesn't work with simple reads.

    However there isn't yet a system to receive BLE notifications on the LoPy so unfortunately you'll have to wait a bit more.

    EDIT: Was catching up on the other threads and apparently this functionality could come next week. I also have a Polar H7 and can look into it at some point.


Log in to reply
 

Looks like your connection to Pycom Forum was lost, please wait while we try to reconnect.