1

我在 RPI 零 W 上使用 pygatt 从 Polar H10 胸带访问 HR 通知流。目标是让 LED 随心率闪烁。通知到达约。100s,然后没有人到达。调试日志中不显示任何错误消息或任何(对我而言可识别的)提示。任何帮助是极大的赞赏。

使用的代码是:

from pygatt.util import uuid16_to_uuid
from pygatt.exceptions import NotConnectedError, NotificationTimeout
import binascii
import time
import logging
import RPi.GPIO as gpio

MAC = 'E7:17:FD:20:B1:AA'   # MAC address of the Polar H10 belt
HR = 0
RRi1 = 0
RRi2 = 0
LED_On_time = 0.15       # seconds
GPIO_port = 19
gpio.setmode(gpio.BCM)
gpio.setup(GPIO_port, gpio.OUT)

logging.basicConfig(filename='/home/pi/python/debug.log',filemode='w',level=logging.DEBUG)
logging.getLogger('pygatt').setLevel(logging.DEBUG)

def callback(handle, measure):
    global HR, RRi1, RRi2

    if handle == 16:
        for i in range(len(measure)):
            if i == 1:
                print('Heart rate = ',measure[1],' bpm')
                HR = measure[1]
            if i == 2:
                RRi1 = round((measure[2] + 256*measure[3])/1024,2)
                print('RR intervall = %.2f' % RRi1,' s')
            if i == 4:
                RRi2 = round((measure[4] + 256*measure[5])/1024,2)
                print('RR intervall = %.2f' % RRi2,' s')


def Init():

    adapter = pygatt.GATTToolBackend()
    adapter.start()

    try:
        """ connect to bluetooth MAC addres with 5 seconds timeout"""
        device = adapter.connect(MAC, address_type=pygatt.BLEAddressType.random)
        device.bond()

        """ generate characteristics uuid's  """
        uuid_heart_service = uuid16_to_uuid(0x2A37)
        """ discover all characteristics uuid's"""
        device.discover_characteristics()


        device.subscribe(uuid_heart_service, callback, True)

    except NotConnectedError:
        print('No connection established ')
        quit()



Init()
t = time.time()    # Initialite with a reasonable value
while(1):
    gpio.output(GPIO_port, gpio.HIGH)
    time.sleep(LED_On_time)
    gpio.output(GPIO_port, gpio.LOW)
    time.sleep(max(0, 60/max(HR,30) - (time.time() - t)))    
    t = time.time()```
4

0 回答 0