|
| 1 | +""" |
| 2 | +Read data from a BerryMed pulse oximeter, model BM1000C, BM1000E, etc. |
| 3 | +Run this on Feather nRF52840 |
| 4 | +Log data to SD card on Autologger FeatherWing |
| 5 | +""" |
| 6 | + |
| 7 | +# Protocol defined here: |
| 8 | +# https://github.com/zh2x/BCI_Protocol |
| 9 | +# Thanks as well to: |
| 10 | +# https://github.com/ehborisov/BerryMed-Pulse-Oximeter-tool |
| 11 | +# https://github.com/ScheindorfHyenetics/berrymedBluetoothOxymeter |
| 12 | +# |
| 13 | +# The sensor updates the readings at 100Hz. |
| 14 | + |
| 15 | +import time |
| 16 | +import adafruit_sdcard |
| 17 | +import board |
| 18 | +import busio |
| 19 | +import digitalio |
| 20 | +import storage |
| 21 | +import _bleio |
| 22 | +import adafruit_ble |
| 23 | +from adafruit_ble.advertising.standard import ( |
| 24 | + Advertisement, |
| 25 | + ProvideServicesAdvertisement, |
| 26 | +) |
| 27 | +from adafruit_ble.services.standard.device_info import DeviceInfoService |
| 28 | +from adafruit_ble_berrymed_pulse_oximeter import BerryMedPulseOximeterService |
| 29 | + |
| 30 | +# Logging setup |
| 31 | +SD_CS = board.D10 |
| 32 | +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
| 33 | +cs = digitalio.DigitalInOut(SD_CS) |
| 34 | +sd_card = adafruit_sdcard.SDCard(spi, cs) |
| 35 | +vfs = storage.VfsFat(sd_card) |
| 36 | +storage.mount(vfs, "/sd_card") |
| 37 | + |
| 38 | +# PyLint can't find BLERadio for some reason so special case it here. |
| 39 | +ble = adafruit_ble.BLERadio() # pylint: disable=no-member |
| 40 | + |
| 41 | +pulse_ox_connection = None |
| 42 | +initial_time = time.monotonic() |
| 43 | + |
| 44 | +while True: |
| 45 | + print("Scanning for Pulse Oximeter...") |
| 46 | + for adv in ble.start_scan(Advertisement, timeout=5): |
| 47 | + name = adv.complete_name |
| 48 | + if not name: |
| 49 | + continue |
| 50 | + # "BerryMed" devices may have trailing nulls on their name. |
| 51 | + if name.strip("\x00") == "BerryMed": |
| 52 | + pulse_ox_connection = ble.connect(adv) |
| 53 | + print("Connected") |
| 54 | + break |
| 55 | + |
| 56 | + # Stop scanning whether or not we are connected. |
| 57 | + ble.stop_scan() |
| 58 | + print("Stopped scan") |
| 59 | + |
| 60 | + try: |
| 61 | + if pulse_ox_connection and pulse_ox_connection.connected: |
| 62 | + print("Fetch connection") |
| 63 | + if DeviceInfoService in pulse_ox_connection: |
| 64 | + dis = pulse_ox_connection[DeviceInfoService] |
| 65 | + try: |
| 66 | + manufacturer = dis.manufacturer |
| 67 | + except AttributeError: |
| 68 | + manufacturer = "(Manufacturer Not specified)" |
| 69 | + try: |
| 70 | + model_number = dis.model_number |
| 71 | + except AttributeError: |
| 72 | + model_number = "(Model number not specified)" |
| 73 | + print("Device:", manufacturer, model_number) |
| 74 | + else: |
| 75 | + print("No device information") |
| 76 | + |
| 77 | + pulse_ox_service = pulse_ox_connection[BerryMedPulseOximeterService] |
| 78 | + |
| 79 | + while pulse_ox_connection.connected: |
| 80 | + values = pulse_ox_service.values |
| 81 | + if values is not None: |
| 82 | + # unpack the message to 'values' list |
| 83 | + valid, spo2, pulse_rate, pleth, finger = values |
| 84 | + if not valid: |
| 85 | + continue |
| 86 | + |
| 87 | + print( |
| 88 | + "SpO2: {}% | ".format(spo2), |
| 89 | + "Pulse Rate: {} BPM | ".format(pulse_rate), |
| 90 | + "Pleth: {}".format(pleth), |
| 91 | + ) |
| 92 | + # print((pleth,)) # uncomment to see graph on Mu plotter |
| 93 | + |
| 94 | + try: # logging to SD card |
| 95 | + with open("/sd_card/log.txt", "a") as sdc: |
| 96 | + current_time = time.monotonic() |
| 97 | + time_stamp = current_time - initial_time |
| 98 | + print( |
| 99 | + "Seconds since current data log started:", |
| 100 | + int(time_stamp), |
| 101 | + ) |
| 102 | + sdc.write( |
| 103 | + "{}, {}, {}, {:.2f}\n".format( |
| 104 | + int(time_stamp), spo2, pulse_rate, pleth |
| 105 | + ) |
| 106 | + ) |
| 107 | + |
| 108 | + time.sleep(2) |
| 109 | + except OSError: |
| 110 | + pass |
| 111 | + except RuntimeError: |
| 112 | + pass |
| 113 | + except _bleio.ConnectionError: |
| 114 | + try: |
| 115 | + pulse_ox_connection.disconnect() |
| 116 | + except: |
| 117 | + pass |
| 118 | + pulse_ox_connection = None |
0 commit comments