Hi cattledog, I found your reply to kaiak from 2023 and have updated the peripheral to:
//PERIPHERAL TESTING 1
#include <ArduinoBLE.h>
#include <Arduino_LSM6DS3.h>
// Set the same service UUID as in the Peripheral Device
const char* deviceServiceUuid = "12345678-1234-5678-1234-56789abcdef0";
const char* AccXCharUuid = "12345678-1234-5678-1234-56789abcdef1";
const char* AccYCharUuid = "12345678-1234-5678-1234-56789abcdef2";
const char* AccZCharUuid = "12345678-1234-5678-1234-56789abcdef3";
const char* GyroXCharUuid = "12345678-1234-5678-1234-56789abcdef4";
const char* GyroYCharUuid = "12345678-1234-5678-1234-56789abcdef5";
const char* GyroZCharUuid = "12345678-1234-5678-1234-56789abcdef6";
float x,y,z,gx,gy,gz;
double accelX=0;
double accelY=1;
double accelZ=0;
double gyroX=0;
double gyroY=0;
double gyroZ=0;
BLEService IMUService(deviceServiceUuid);
BLEDoubleCharacteristic AccXChar(AccXCharUuid, BLERead | BLENotify);
BLEDoubleCharacteristic AccYChar(AccYCharUuid, BLERead | BLENotify);
BLEDoubleCharacteristic AccZChar(AccZCharUuid, BLERead | BLENotify);
BLEDoubleCharacteristic GyroXChar(GyroXCharUuid, BLERead | BLENotify);
BLEDoubleCharacteristic GyroYChar(GyroYCharUuid, BLERead | BLENotify);
BLEDoubleCharacteristic GyroZChar(GyroZCharUuid, BLERead | BLENotify);
void setup() {
Serial.begin(9600);
while (!Serial);
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
if (!BLE.begin()) {
Serial.println("Starting Bluetooth failed!");
while (1);
}
BLE.setLocalName("IMU Peripheral");
BLE.setAdvertisedService(IMUService);
IMUService.addCharacteristic(AccXChar);
IMUService.addCharacteristic(AccYChar);
IMUService.addCharacteristic(AccZChar);
IMUService.addCharacteristic(GyroXChar);
IMUService.addCharacteristic(GyroYChar);
IMUService.addCharacteristic(GyroZChar);
BLE.addService(IMUService);
BLE.advertise();
Serial.println("IMU Peripheral (Sending Data)");
}
void loop() {
BLEDevice central = BLE.central();
if (central) {
if (central.connected()) {
Serial.println("Connected to central device");
Serial.print("Device MAC address: ");
Serial.println(central.address());
} else {
Serial.println("Disconnected from central device");
}
}
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
accelX = x;
accelY = y;
accelZ = z;
Serial.print("Acceleration - x: ");
Serial.print(x);
Serial.print(", y: ");
Serial.print(y);
Serial.print(", z: ");
Serial.println(z);
}
if (IMU.gyroscopeAvailable()) {
IMU.readGyroscope(gx, gy, gz);
gyroX = gx;
gyroY = gy;
gyroZ = gz;
Serial.print("Gyroscope - gx: ");
Serial.print(gx);
Serial.print(", gy: ");
Serial.print(gy);
Serial.print(", gz: ");
Serial.println(gz);
}
AccXChar.writeValue(accelX);
AccYChar.writeValue(accelY);
AccZChar.writeValue(accelZ);
GyroXChar.writeValue(gyroX);
GyroYChar.writeValue(gyroY);
GyroZChar.writeValue(gyroZ);
delay(3000);
}
And the Central to :
//CENTRAL TESTING 1
#include <ArduinoBLE.h>
const char* deviceServiceUuid = "12345678-1234-5678-1234-56789abcdef0";
const char* AccXCharUuid = "12345678-1234-5678-1234-56789abcdef1";
const char* AccYCharUuid = "12345678-1234-5678-1234-56789abcdef2";
const char* AccZCharUuid = "12345678-1234-5678-1234-56789abcdef3";
const char* GyroXCharUuid = "12345678-1234-5678-1234-56789abcdef4";
const char* GyroYCharUuid = "12345678-1234-5678-1234-56789abcdef5";
const char* GyroZCharUuid = "12345678-1234-5678-1234-56789abcdef6";
BLEDoubleCharacteristic AccXChar(AccXCharUuid, BLERead | BLENotify);
BLEDoubleCharacteristic AccYChar(AccYCharUuid, BLERead | BLENotify);
BLEDoubleCharacteristic AccZChar(AccZCharUuid, BLERead | BLENotify);
BLEDoubleCharacteristic GyroXChar(GyroXCharUuid, BLERead | BLENotify);
BLEDoubleCharacteristic GyroYChar(GyroYCharUuid, BLERead | BLENotify);
BLEDoubleCharacteristic GyroZChar(GyroZCharUuid, BLERead | BLENotify);
void setup() {
Serial.begin(9600);
while (!Serial);
BLE.begin();
} // setup
void loop() {
BLE.scanForUuid(deviceServiceUuid);
BLEDevice peripheral = BLE.available();
if (peripheral) {
Serial.println("Peripheral Device Found");
Serial.print("Device Name: ");
Serial.println(peripheral.localName());
Serial.print("Device MAC Address: ");
Serial.println(peripheral.address());
BLE.stopScan();
Serial.println("- Connecting to peripheral device...");
if (peripheral.connect()) {
Serial.println("Connected to Peripheral Device");
Serial.println(" ");
if (peripheral.discoverAttributes()) {
Serial.println("Attributes Discovered");
BLECharacteristic accXChar = peripheral.characteristic(AccXCharUuid);
BLECharacteristic accYChar = peripheral.characteristic(AccYCharUuid);
BLECharacteristic accZChar = peripheral.characteristic(AccZCharUuid);
BLECharacteristic gyroXChar = peripheral.characteristic(GyroXCharUuid);
BLECharacteristic gyroYChar = peripheral.characteristic(GyroYCharUuid);
BLECharacteristic gyroZChar = peripheral.characteristic(GyroZCharUuid);
while (peripheral.connected()){
if (accXChar) {
const uint8_t* bytes = accXChar.value();
double accXValue;
accXChar.readValue(&accXValue, sizeof(accXValue));
//memcpy(&accXValue, bytes, sizeof(accXValue));
Serial.print("Acceleration X: ");
Serial.println(accXValue);
}
if (accYChar) {
const uint8_t* bytes = accYChar.value();
double accYValue;
accYChar.readValue(&accYValue, sizeof(accYValue));
//memcpy(&accYValue, bytes, sizeof(accYValue));
Serial.print("Acceleration Y: ");
Serial.println(accYValue);
}
if (accZChar) {
const uint8_t* bytes = accZChar.value();
double accZValue;
accZChar.readValue(&accZValue, sizeof(accZValue));
// memcpy(&accZValue, bytes, sizeof(accZValue));
Serial.print("Acceleration Z: ");
Serial.println(accZValue);
}
if (gyroXChar) {
const uint8_t* bytes = gyroXChar.value();
double gyroXValue;
gyroXChar.readValue(&gyroXValue, sizeof(gyroXValue));
//memcpy(&gyroXValue, bytes, sizeof(gyroXValue));
Serial.print("Gyroscope X: ");
Serial.println(gyroXValue);
}
if (gyroYChar) {
const uint8_t* bytes = gyroYChar.value();
double gyroYValue;
gyroYChar.readValue(&gyroYValue, sizeof(gyroYValue));
//memcpy(&gyroYValue, bytes, sizeof(gyroYValue));
Serial.print("Gyroscope Y: ");
Serial.println(gyroYValue);
}
if (gyroZChar) {
const uint8_t* bytes = gyroZChar.value();
double gyroZValue;
gyroZChar.readValue(&gyroZValue, sizeof(gyroZValue));
//memcpy(&gyroZValue, bytes, sizeof(gyroZValue));
Serial.print("Gyroscope Z: ");
Serial.println(gyroZValue);
}
delay(1000);
}
}
} else {
Serial.println("* Connection to peripheral device failed!");
Serial.println(" ");
}
}
delay(500);
}
I am getting no data on the serial monitor for the Central device and when I connect the Peripheral to USB I get a "Failed to Initialise IMU" message. I am using the Nano 33 Sense Rev 2. Not sure if the delay value affects the central's ability to read the advertised values. I have the peripheral running off a 9V battery - for mobility.