Assistance in transferring gyroscope data from peripheral to central over BLE

I need some assistance with determining whether if my peripheral device is having trouble transferring data via BLE over to my central device, or if my central device is having trouble reading/obtaining the delivered data from the peripheral device, or both. I am currently using two Arduino Nano 33 Sense devices.

As of right now, I am able to have the two devices successfully connect with each other over the BLE feature but running into complications when gyroscope data is trying to be sent from the peripheral to the central device. The goal is to first establish connection between the two devices, have the peripheral float its gyroscope data (x, y, z) and send it to the central, while the central device after establishing connection float its own gyroscope data (x, y, z), receive the incoming data from the peripheral and then combine the two sets of gyroscope data to determine an angle based off of the position at that given moment.

However, I am still very new to Arduino coding (and coding in general) and would like if I could get another pair of eyes on my work, and determine what I am doing incorrectly and provide assistance on what I should do next.

Thank you in advance!

This is the code for the peripheral:

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

const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";

BLEService gyroService(deviceServiceUuid); 
BLEByteCharacteristic gyroCharacteristic(deviceServiceCharacteristicUuid, BLERead | BLEWrite);

// Define custom BLE service for position (read-only)
BLEService posService("95ff7bf8-aa6f-4671-82d9-22a8931c5387");
BLEFloatCharacteristic posX("95ff7bf8-aa6f-4671-82d9-22a8931c5387", BLERead | BLEWrite);
BLEFloatCharacteristic posY("f49caa00-17f8-4e92-b5fd-d27137ca4515", BLERead | BLEWrite);
BLEFloatCharacteristic posZ("84f9b003-6d14-44d7-8db1-d574d29c10c3", BLERead | BLEWrite);

void setup() {
  Serial.begin(115200);
  while (!Serial);  

   if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }
  
  if (!BLE.begin()) {
    Serial.println("- Starting BLE module failed!");
    while (1);
  }

  BLE.setLocalName("Arduino Nano 33 BLE (Peripheral)");
  BLE.setAdvertisedService(gyroService);
  gyroService.addCharacteristic(gyroCharacteristic);
  BLE.addService(gyroService);
  gyroCharacteristic.writeValue(-1);
  
  posService.addCharacteristic(posX);
  posService.addCharacteristic(posY);
  posService.addCharacteristic(posZ);
  BLE.addService(posService);


  // Set default values for characteristics
  posX.writeValue(0.0f);
  posY.writeValue(0.0f);
  posZ.writeValue(0.0f);
  
  BLE.advertise();

  Serial.println("Nano 33 BLE (Peripheral Device)");
  Serial.println(" ");
}

void loop() {
  BLEDevice central = BLE.central();
  Serial.println("- Discovering central device...");
  delay(500);

  if (central) {
    Serial.println("* Connected to central device!");
    Serial.print("* Device MAC address: ");
    Serial.println(central.address());
    Serial.println(" ");

  
  while (central.connected()) {
    float x, y, z;
    byte ledValue = 0x0;
    
    if (IMU.gyroscopeAvailable()) {
      IMU.readGyroscope(x, y, z);

    // Write values on BLE
    posX.writeValue(x);
    posY.writeValue(y);
    posZ.writeValue(z);

    // Read values on BLE
    if(ledValue == 0x0) {
      digitalWrite(LED_BUILTIN, LOW);
    } else {
      digitalWrite(LED_BUILTIN, HIGH);
    }

    delay(1000);
  
  }
}
}
}
_____________________________________________________________
This is the code for the central: 

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

const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";

// Define custom BLE service for position (read-only)
BLEService posService("95ff7bf8-aa6f-4671-82d9-22a8931c5387");
BLEFloatCharacteristic posX("95ff7bf8-aa6f-4671-82d9-22a8931c5387", BLERead);
BLEFloatCharacteristic posY("f49caa00-17f8-4e92-b5fd-d27137ca4515", BLERead);
BLEFloatCharacteristic posZ("84f9b003-6d14-44d7-8db1-d574d29c10c3", BLERead);

void setup() {
  Serial.begin(115200);
  while (!Serial);
  
  if (!IMU.begin()) {
    Serial.println("* Error initializing APDS9960 sensor!");
  } 

  if (!BLE.begin()) {
    Serial.println("* Starting BLE module failed!");
    while (1);
  }
  
  BLE.setLocalName("Nano 33 BLE (Central)"); 
  
  posService.addCharacteristic(posX);
  posService.addCharacteristic(posY);
  posService.addCharacteristic(posZ);
  BLE.addService(posService);

   // Set default values for characteristics
  posX.writeValue(0.0f);
  posY.writeValue(0.0f);
  posZ.writeValue(0.0f);
  
  BLE.advertise();

  Serial.println("Arduino Nano 33 BLE Sense (Central Device)");
  Serial.println(" ");
}

void loop() {
  connectToPeripheral();
}

void connectToPeripheral(){
  BLEDevice peripheral;
  
  Serial.println("- Discovering peripheral device...");

  do
  {
    BLE.scanForUuid(deviceServiceUuid);
    peripheral = BLE.available();
  } while (!peripheral);
  
  if (peripheral) {
    Serial.println("* Peripheral device found!");
    Serial.print("* Device MAC address: ");
    Serial.println(peripheral.address());
    Serial.print("* Device name: ");
    Serial.println(peripheral.localName());
    Serial.print("* Advertised service UUID: ");
    Serial.println(peripheral.advertisedServiceUuid());
    Serial.println(" ");
    BLE.stopScan();
    controlPeripheral(peripheral);
  }
}

void controlPeripheral(BLEDevice peripheral) {
  Serial.println("- Connecting to peripheral device...");

  if (peripheral.connect()) {
    Serial.println("* Connected to peripheral device!");
    Serial.println(" ");
  } else {
    Serial.println("* Connection to peripheral device failed!");
    Serial.println(" ");
    return;
  }

  Serial.println("- Discovering peripheral device attributes...");
  if (peripheral.discoverAttributes()) {
    Serial.println("* Peripheral device attributes discovered!");
    Serial.println(" ");
  } else {
    Serial.println("* Peripheral device attributes discovery failed!");
    Serial.println(" ");
    peripheral.disconnect();
    return;
  }
  
  BLECharacteristic gyroCharacteristic = peripheral.characteristic(deviceServiceCharacteristicUuid);

   //obtain (x, y, z) values from central IMU 
    float x, y, z;
    float perX, perY, perZ;
    
  while (peripheral.connected()) {
    if (IMU.gyroscopeAvailable()) {
      IMU.readGyroscope(x, y, z);
      Serial.println(x);
      Serial.println(y);
      Serial.println(z);
    }
    Serial.println("-------------------------------------------------------------------------");
  
  //determine how to obtain (x, y, z) vlaues from peripheral
  if(gyroCharacteristic.written()){
    perX = posX.writeValue(x);
    Serial.println("Recieved Data From Peripheral:");
    Serial.println(perX);
    perY = posY.writeValue(y);
    Serial.println(perY);
    perZ = posZ.writeValue(z);
    Serial.println(perZ);
  }
  
  
  //combine (x, y, z) values from both central and peripheral to determine angle
  
  
  // angle from xy-plane
  atan2(sqrt(x*perX+y*perY),z*perZ);
  
  //angle from xz-plane
  sqrt(x*perX+z*perZ),y*perY;
  
  //angle from yz-plane
  sqrt(y*perY+z*perZ),x*perX;
  delay(1500);
  }
  
}
  



Could You try sending just one simple byte to verify the transmission works? Too often sending multibyte data causes trouble.

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