adafruit / Adafruit_CircuitPython_BLE_BerryMed_Pulse_Oximeter

BLE Support for BerryMed Pulse Oximeters
MIT License
7 stars 6 forks source link

Can't use multiprocessing on the BLE connections to retrieve data from multipe devices at the same time #8

Open sreerajpunnoli opened 3 years ago

sreerajpunnoli commented 3 years ago

I have been trying to get data out from multiple pulse-oximeters at the same time. I was able to run them separately in two different environments and able to extract data from two pulse-oximeters at the same time. I tried to use multiprocessing after the BLE connection to retrieve data parallelly from two pulse-oximeters in the same environment, the connection is getting lost and data is no longer getting received. Then I removed multiprocessing to check whether the code is right and verified that the data is getting retrieved from the code. I am using the example simple test code as a reference.

How do I retrieve data from multiple BLE devices at the same time using multiprocessing with the library?

dhalbert commented 3 years ago

What are you running the library on? Is it a host computer that's running regular Python, and you're using Blinka, or are you using an nRF or Airlift board? My guess is the first thing, if you're using multiprocessing.

sreerajpunnoli commented 3 years ago

I have tried it on a Raspberry Pi 4 and a Mac

dhalbert commented 3 years ago

The adafruit-blinka-bleio library, which you are using, uses the bleak library to do BLE on host computers. Bleak in turn uses different BLE software on each different operating system. My guess is that something either about bleak or the underlying software is getting confused about two seemingly identical devices. I'd suggest trying on Windows as well, to confirm. Then try a simple bleak-based program that tries to connect to two identical pulse oximeters and see what happens. You might also inquire with bleak about whether people have tried running multiple instances in separate processes.

It would also be interesting for you to sidestep multiprocessing right now and just try running two processes by hand in different terminal windows to connect to the two pulse oximeters to make sure the issue is not with multiprocessing or your use of it.

sreerajpunnoli commented 3 years ago

Thanks, @dhalbert for your reply. I modified the code a little bit and ran it on mac, windows, and Raspberry Pi OS. Both Mac and Windows machines are now streaming at the same time just fine in multiprocessing. But the issue is still happening in Raspberry Pi OS. In Raspberry Pi 4 with Raspberry Pi OS, the connect method in BLERadio class is getting called and it is waiting forever and not throwing timeout exception. When I removed the multiprocessing, connect method works fine in Raspberry Pi OS. It seems like a library issue. Please suggest.

dhalbert commented 3 years ago

There is an issue with the BLE firmware for Raspberry Pi 3B+ and 4. See https://learn.adafruit.com/circuitpython-ble-libraries-on-any-computer/install-ble-libraries#raspberry-pi-3b-plus-and-4b-firmware-fix-3074928-6. The firmware is now at version rpt7, I think, but I am not sure this issue has been fixed. Try the rp2 version. I have found it necessary to power-cycle the board after updating the firmware, at least some of the time.