Bluetooth communication between NRF52840 and computer

need

Use nrf52840 as the main control to connect various sensors, and transmit the sensor data to the computer for reception via Bluetooth. Due to different protocol stacks, hc-06 cannot retrieve nrf52840.

actual configuration

  1. The main control is Seeed XIAO BLE , and its chip is nrf52840, which is equipped with arduino base, so the upper layer of the user uses arduino IDE to program the code.
  • His Bluetooth part uses the Adafruit related library.
  1. The PC part uses the Adafruit_CircuitPython_BLE library through practice, so it runs in linux, but not in windows for the time being.
  • OS: Ubuntu 20.04

Implementation process

install library

My python version is 3.8 that comes with ubuntu

pip3 install adafruit-circuitpython-ble

If you need to install in a virtual environment, then

mkdir project-name && cd project-name
python3 -m venv .venv
source .venv/bin/activate
pip3 install adafruit-circuitpython-ble

test

from adafruit_ble import BLERadio

radio = BLERadio()
print("scanning")
found = set()
for entry in radio.start_scan(timeout=60, minimum_rssi=-80):
    addr = entry.address
    if addr not in found:
        print(entry)
    found.add(addr)

print("scan done")

Official library: Adafruit_CircuitPython_BLE

Search bluetooth and connect

The above demo is the part of retrieving Bluetooth. Through testing, we can easily retrieve the Bluetooth of nrf52840. Then make the connection:

radio.connect(entry)

Stop searching when you find it

radio.stop_scan()

data transmission

ntypes is the number of transmitted data, up to 64 by default, you can modify its buffer capacity in the UARTService class in the downloaded data package.
There are three functions that can be used to read data:

def read(self, nbytes: Optional[int] = None) -> Optional[bytes]
def readinto(self, buf: WriteableBuffer, nbytes: Optional[int] = None) -> Optional[int]
def readline(self) -> Optional[bytes]

Writing data can:

def write(self, buf: ReadableBuffer) -> None

for example

data = device[UARTService].read(ntypes)
message = data.decode()

For specific cases, please refer to my github code

Guess you like

Origin blog.csdn.net/Carol_learning/article/details/128754779