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).