1

I am attempting to pass motion data (accelerometer, gyroscope,...) from an IMU to my computer over a Bluetooth Low Energy (BLE) connection. I purchased the FXOS8700 + FXAS21002 to generate motion data and am using a wired I2C connection to pass that data to my BLE board, an nRF52840. The I2C connection works and I can get data off the nRF52840 via a wired connection. I have also managed to establish a Bluetooth connection between my BLE board and my computer and can view the BLE characteristics, but I can't figure out how to program the nRF52840 to write my motion data into these characteristics to pass them over Bluetooth.

Below is the code I have used to establish an I2C connection and create the BLE connection. Missing is the code to create BLE characteristics.

# Import ble libraries
import _bleio
import adafruit_ble
from adafruit_ble.advertising.standard import Advertisement
from adafruit_ble.services.standard.device_info import DeviceInfoService

# Import libraries for the FXOS8700 accelerometer and magnetometer
import time
import board
import busio
import adafruit_fxos8700


# CircuitPython <6 uses its own ConnectionError type. So, is it if available. Otherwise,
# the built in ConnectionError is used.
connection_error = ConnectionError
if hasattr(_bleio, "ConnectionError"):
    connection_error = _bleio.ConnectionError

# PyLint can't find BLERadio for some reason so special case it here
ble = adafruit_ble.BLERadio()  # pylint: disable=no-member


# Initialize I2C bus and device
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_fxos8700.FXOS8700(i2c)

isConnected = 0
while not isConnected:
    print("Scanning...")
    for adv in ble.start_scan(Advertisement, timeout=5):
        name = adv.complete_name
        if not name:
            continue
        isConnected = 1
        print("Connected")
        break

    # Stop scanning whether or not we are connected
    ble.stop_scan()
    print("Stopped scan")
    
while True:
    # Read acceleration and magnetometer
    accel_x, accel_y, accel_z = sensor.accelerometer
    mag_x, mag_y, mag_z = sensor.magnetometer
    
    # Write acceleration and magnetometer data to BLE characteristics...

How do I write data into a BLE characteristic and send that data over Bluetooth? Do I need to create a new BLE characteristic or service? I feel that I am missing something fundamental here...

Additional information: I am using the ble() function in MATLAB to connect to the nRF52840 and read the BLE characteristic data. I was initially trying to pass every single measurement over Bluetooth (likely requiring a more complex program with data buffers), but at this point I would be happy simply being able to read the most recent measurement.

Since the BLE board I'm using is so popular, I expected to find numerous examples of data transfer over Bluetooth, but I haven't found any examples of sending data with BLE. There are numerous examples using a BLE-UART connection (not visible to MATLAB) or sending data to the BLE board, but none sending data from the BLE board, except via this UART method (but I can't find a way to access that data).

blobberman
  • 11
  • 2
  • Have you installed the nRF5 SDK? It includes many examples. – alemv Nov 18 '20 at 07:13
  • I couldn't find any examples on how to implement a custom service + characteristic. Have you seen the docs at https://circuitpython.readthedocs.io/projects/ble/en/latest/? What is your MATLAB code? – Michael Kotzjan Nov 18 '20 at 07:48
  • My MATLAB code is simply calling the ble() function and passing the returned object into the characteristic() function to instantiate (then read) one of the readable characteristics. Enabling the BLE-UART does not create a new characteristic, so it isn't clear to me if this data is available anywhere in MATLAB. It is not available in any of the services/characteristics of the ble-object. – blobberman Nov 19 '20 at 09:07
  • @alemv I was trying to find code for the Adafruit nRF52840 board specifically. My understanding was that the nRF52 SDK has more functionality than the Adafruit breakout board and may not reflect the communication protocols available to the Adafruit board. – blobberman Nov 19 '20 at 09:10

0 Answers0