Hello!
I created a code to send acceleration data via BLE using seeed xiao ble nrf52840 sense under Arduino 2.3.2 and Seeed nRF52 mbed-enabled Boards Boards environment. In the code below, I cannot change the BLE advertising interval, it remains at the default of 100 ms even after setting it with BLE.setAdvertisingInterval();. Any ideas for improvement?
#include <ArduinoBLE.h> // Arduino BLE library
#include “LSM6DS3.h”
#include “Wire.h”
//Create an instance of class LSM6DS3
LSM6DS3 xIMU(I2C_MODE, 0x6A); //I2C device address 0x6A
/* Online GUID / UUID Generator:
64cf715d-f89e-4ec0-b5c5-d10ad9b53bf2
*/
// UUid for Service
const char* UUID_serv = “64cf715d-f89e-4ec0-b5c5-d10ad9b53bf2”;
// UUids for accelerometer characteristics
const char* UUID_ax = “64cf715e-f89e-4ec0-b5c5-d10ad9b53bf2”;
const char* UUID_ay = “64cf715f-f89e-4ec0-b5c5-d10ad9b53bf2”;
const char* UUID_az = “64cf7160-f89e-4ec0-b5c5-d10ad9b53bf2”;
const char* UUID_wx = “64cf7161-f89e-4ec0-b5c5-d10ad9b53bf2”;
const char* UUID_wy = “64cf7162-f89e-4ec0-b5c5-d10ad9b53bf2”;
const char* UUID_wz = “64cf7163-f89e-4ec0-b5c5-d10ad9b53bf2”;
// BLE Service
BLEService myService(UUID_serv);
// BLE Characteristics
BLEFloatCharacteristic chAX(UUID_ax, BLERead|BLENotify);
BLEFloatCharacteristic chAY(UUID_ay, BLERead|BLENotify);
BLEFloatCharacteristic chAZ(UUID_az, BLERead|BLENotify);
BLEFloatCharacteristic chWX(UUID_wx, BLERead|BLENotify);
BLEFloatCharacteristic chWY(UUID_wy, BLERead|BLENotify);
BLEFloatCharacteristic chWZ(UUID_wz, BLERead|BLENotify);
void setup()
{
// Serial.begin(115200);
// while (!Serial);
// Serial.println(“Seeed XIAO BLE Sense IMU-Acc-Gyro Data Logger”);
bool err=false;
pinMode(LEDR, OUTPUT); // onboard led red
pinMode(LEDB, OUTPUT); // onboard led blue
digitalWrite(LEDB, HIGH); // led blue off
digitalWrite(LEDR, LOW); // led red on
// init IMU
if (xIMU.begin() != 0) {
// Serial.println("Device error");
err = true;
} else {
// Serial.println("Device OK!");
}
// init BLE
if (!BLE.begin())
{
// Serial.println("BLE: failed");
err=true;
}
// Serial.println(“BLE: ok”);
// error: flash led forever
if (err)
{
// Serial.println("Init error. System halted");
while(1)
{
digitalWrite(LEDR, LOW);
delay(500);
digitalWrite(LEDR, HIGH); // led on
delay(500);
}
}
// Set BLE name
BLE.setLocalName(“IMU-Acc-Gyro DataLogger”);
BLE.setDeviceName(“XIAO-BLE-Sense”);
// Set AdvertisingInterval
BLE.setAdvertisingInterval(160); // 10 ms ,16 * 0.625 ms, units of 0.625 ms
// Set advertised Service
BLE.setAdvertisedService(myService);
// Add characteristics to the Service
myService.addCharacteristic(chAX);
myService.addCharacteristic(chAY);
myService.addCharacteristic(chAZ);
myService.addCharacteristic(chWX);
myService.addCharacteristic(chWY);
myService.addCharacteristic(chWZ);
// add service to BLE
BLE.addService(myService);
// characteristics initial values
chAX.writeValue(0);
chAY.writeValue(0);
chAZ.writeValue(0);
chWX.writeValue(0);
chWY.writeValue(0);
chWZ.writeValue(0);
// start advertising
BLE.advertise();
// Serial.println(“Advertising started”);
// Serial.println(“Bluetooth device active, waiting for connections…”);
}
void loop()
{
// listen for BLE centrals devices
BLEDevice central = BLE.central();
// central device connected?
if (central)
{
digitalWrite(LEDR, HIGH);
digitalWrite(LEDB, LOW); // turn on the blue led
// Serial.print("Connected to central: ");
// Serial.println(central.address()); // central device MAC address
// while the central is still connected to peripheral:
while (central.connected())
{
chAX.writeValue(xIMU.readFloatAccelX());
chAY.writeValue(xIMU.readFloatAccelY());
chAZ.writeValue(xIMU.readFloatAccelZ());
chWX.writeValue(xIMU.readFloatGyroX());
chWY.writeValue(xIMU.readFloatGyroY());
chWZ.writeValue(xIMU.readFloatGyroZ());
// delay(1);
// Serial.print(xIMU.readFloatAccelX(), 4);
// Serial.print(',');
// Serial.print(xIMU.readFloatAccelY(), 4);
// Serial.print(',');
// Serial.print(xIMU.readRawAccelZ(), 4);
// Serial.print(',');
// Serial.print(xIMU.readFloatAccelZ(), 4);
// Serial.print(xIMU.readFloatGyroX(), 4);
// Serial.print(',');
// Serial.print(xIMU.readFloatGyroY(), 4);
// Serial.print(',');
// Serial.print(xIMU.readFloatGyroZ(), 4);
// Serial.println(); // call function for updating value to send to central
} // still here while central connected
// central disconnected:
digitalWrite(LEDB, HIGH);
// Serial.print("Disconnected from central: ");
// Serial.println(central.address());
} // no central
else
{
// Blink the red LED when no central device is connected
digitalWrite(LEDR, HIGH);
delay(500);
digitalWrite(LEDR, LOW);
delay(500);
}
}