Send huge data over BLE using arduino nano 33 ble

Hi there. In my project I need to perform machine learning on the accelerometer and gyroscope values to determine whether a person did fall down or not. I used arduino nano 33 ble to perform such function, right now i am having issues while trying to do the data collection. I planned to send the gyroscope and accelerometer values to my PC via BLE. I am using python Bleak in my PC as the client and arduino nano 33 ble as the server. In my code, I am sending total 504 bytes of data through BLE by using array and struct method. On my PC side, my Bleak python code are receiving data and the data are well converted into float values, but it takes like 1.5seconds to receive a whole 504 bytes of data. I want to ask is this the correct way to send huge bytes of data through BLE? Through my research BLE is meant to send packet with smaller size, so this cause me to have some concern, and the delay on the Bleak python side. The gyroscope and accelerometer are also set to output data rate of around 119Hz, but on my PC only receive one 504 bytes of packet in around 1.5s. Is there any way to transfer the accelerometer and gyroscope values on arduino nano 33 ble resulting in more efficient and lesser data loss? Apologize if the sentences are not well structed as English is not my first language. Below is my code. Thank you.

#include <Arduino.h>
#include <Wire.h>
#include <ArduinoBLE.h>
#include <Arduino_LSM9DS1.h>

//----------------------------------------------------------------------------------------------------------------------
// BLE UUIDs
//----------------------------------------------------------------------------------------------------------------------

#define BLE_UUID_SENSOR_DATA_SERVICE              "2BEEF31A-B10D-271C-C9EA-35D865C1F48A"
#define BLE_UUID_MULTI_SENSOR_DATA                "4664E7A1-5A13-BFFF-4636-7D0A4B16496C"

#define NUMBER_OF_SENSORS 126

union multi_sensor_data
{
  struct __attribute__( ( packed ) )
  {
    float values[NUMBER_OF_SENSORS];
  };
  signed char bytes[ NUMBER_OF_SENSORS * sizeof( float ) ];
};

union multi_sensor_data multiSensorData;


//----------------------------------------------------------------------------------------------------------------------
// BLE
//----------------------------------------------------------------------------------------------------------------------

BLEService sensorDataService( BLE_UUID_SENSOR_DATA_SERVICE );
BLECharacteristic multiSensorDataCharacteristic( BLE_UUID_MULTI_SENSOR_DATA, BLERead | BLENotify, sizeof multiSensorData.bytes );


// const int BLE_LED_PIN = LED_BUILTIN;
// const int RSSI_LED_PIN = LED_PWR;


bool setupBleMode()
{
  if ( !BLE.begin() )
  {
    return false;
  }

  // set advertised local name and service UUID:
  BLE.setDeviceName( "Arduino Nano 33 BLE" );
  BLE.setLocalName( "Arduino Nano 33 BLE" );
  BLE.setAdvertisedService( sensorDataService );

  // BLE add characteristics
  sensorDataService.addCharacteristic( multiSensorDataCharacteristic );
  // sensorDataService.addCharacteristic( allowCharacteristics );

  // add service
  BLE.addService( sensorDataService );

  // start advertising
  BLE.advertise();

  return true;
}

void setup()
{
  Serial.begin( 115200 );
  // while ( !Serial );
  pinMode(LED_BUILTIN,OUTPUT);

  if ( setupBleMode() )
  {
    // digitalWrite( BLE_LED_PIN, HIGH );
  }
  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }
  IMU.setAccelFS(3);    
  IMU.setAccelODR(3);
  IMU.setGyroOffset (0, 0, 0);  // = uncalibrated
  IMU.setGyroSlope  (1, 1, 1);  // = uncalibrated

  IMU.setAccelFS(3);           
  IMU.setAccelODR(3);           // 
  IMU.setAccelOffset(0, 0, 0);  //   uncalibrated
  IMU.setAccelSlope (1, 1, 1);  //   uncalibrated
  IMU.gyroUnit= DEGREEPERSECOND;  //   DEGREEPERSECOND  RADIANSPERSECOND  REVSPERMINUTE  REVSPERSECOND  
  Serial.println("Gyroscope in degrees/second \n");
  Serial.print("Gyroscope Full Scale = ±");
  Serial.print(IMU.getGyroFS());
  Serial.println ("°/s");
  Serial.print("Gyroscope sample rate = ");
  Serial.print(IMU.getGyroODR());        //alias  IMU.gyroscopeSampleRate());
  Serial.println(" Hz");
  Serial.println("Accelerometer in degrees/second \n");
  Serial.print("Accelerometer Full Scale = ±");
  Serial.print(IMU.getAccelFS());
  Serial.println ("°/s");
  Serial.print("Accelerometer sample rate = ");
  Serial.print(IMU.getAccelODR());        //alias  IMU.gyroscopeSampleRate());
  Serial.println(" Hz");
  delay(4000);
}


void loop()
{
  BLEDevice central = BLE.central();

  if ( central )
  {
    Serial.println( central.address() );
    digitalWrite( LED_BUILTIN, HIGH );                  //Turn on peripheral LED to indicate valid connection with Central Device

    while ( central.connected() )
    {
          float gX, gY, gZ, aX, aY, aZ;
              for(int i=0;i<=120;i+=6){
                if(IMU.gyroAvailable() && IMU.accelAvailable()){
                  IMU.readGyro(gX,gY,gZ);
                  IMU.readAccel(aX,aY,aZ);
                  Serial.print(aX, 3);
                  Serial.print(',');
                  Serial.print(aY, 3);
                  Serial.print(',');
                  Serial.print(aZ, 3);
                  Serial.print(',');
                  Serial.print(gX, 3);
                  Serial.print(',');
                  Serial.print(gY, 3);
                  Serial.print(',');
                  Serial.print(gZ, 3);
                  Serial.println();
                  multiSensorData.values[i]=aX;
                  multiSensorData.values[i+1]=aY;
                  multiSensorData.values[i+2]=aZ;
                  multiSensorData.values[i+3]=gX;
                  multiSensorData.values[i+4]=gY;
                  multiSensorData.values[i+5]=gZ;
                }
              }
          multiSensorDataCharacteristic.writeValue( multiSensorData.bytes, sizeof multiSensorData.bytes );
        }
    }
    digitalWrite( LED_BUILTIN, LOW );
}```
1 Like

First off your Serial.prints can slow you up, see Arduino Serial I/O for the Real World for a non-blocking Serial print.
Second in my (limited) experience BLE is not great for high speed data.
WiFi is better. See Remote High Speed Data Logging

Having said that they use BLE to send audio but I have not see any code to do that for the Nano BLE

Alright, thanks for ur info!

A post was split to a new topic: Help with bluetooth please

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.