Get continuously the value of a BLE service, communication between 2 arduino Nano 33 BLE Sense

Hello,

I am not done with working on this topic but here are my last codes that work for temperature and accelerometer. From now, I will start to work on the communication of a NanoBLE 33 sense with a Wio terminal to display the datas.

Peripheral

/*
 * Device: Arduino Nano 33 BLE Sense
 * Peripheral
 * The values of the integrated temperature sensor and 
 * accelerometer are sent using BLE.
 */

#include <ArduinoBLE.h>
#include <Arduino_LSM9DS1.h> //accelerometer sensor
#include <Arduino_HTS221.h> // temperature sensor

int const d_a=2; //number of decimal to keep for the accelerometer
int const d_t=2; //number of decimal to keep for the temperature

//float values read by the sensor
float xSensor=0;
float ySensor=0;
float zSensor=0;
float tSensor=37;

//integer variables to send via BLE
int xBLE=xSensor*pow(10,d_a);
int yBLE=ySensor*pow(10,d_a);
int zBLE=zSensor*pow(10,d_a);
int tBLE=tSensor*pow(10,d_t);
//int t_i=t_f*100;
//int x_i=x_f*100;
//int y_i=y_f*100;
//int z_i=z_f*100;

BLEService SensorService("1101");
BLEUnsignedIntCharacteristic XChar("2101", BLERead | BLENotify);
BLEUnsignedIntCharacteristic YChar("2102", BLERead | BLENotify);
BLEUnsignedIntCharacteristic ZChar("2103", BLERead | BLENotify);
BLEUnsignedIntCharacteristic TChar("2104", BLERead | BLENotify);

void setup() {
IMU.begin();
HTS.begin();
Serial.begin(9600); 
while (!Serial);

//  if (!HTS.begin()){
//    Serial.println("Failed to start the HTS221 sensor.");
//    while(1);
//
//  if (!IMU.begin()) {
//    Serial.println("Failed to start the LSM9DS sensor.");
//    while (1);

pinMode(LED_BUILTIN, OUTPUT);

if (!BLE.begin()) {
Serial.println("BLE failed to Initiate");
delay(500);
while (1);
}

BLE.setLocalName("Arduino XYZT (peripheral)");
BLE.setAdvertisedService(SensorService);
SensorService.addCharacteristic(XChar);
SensorService.addCharacteristic(YChar);
SensorService.addCharacteristic(ZChar);
SensorService.addCharacteristic(TChar);
BLE.addService(SensorService);

XChar.writeValue(xBLE);
YChar.writeValue(yBLE);
ZChar.writeValue(zBLE);
TChar.writeValue(tBLE);

BLE.advertise();

Serial.println("Arduino XYZT peripheral device is now active, waiting for connections...");
}


void loop() {

BLEDevice central = BLE.central();
if (central) {
Serial.print("Connected to central: ");
Serial.print("* Device MAC address: ");
Serial.println(central.address());
Serial.println(" ");

digitalWrite(LED_BUILTIN, HIGH);

while (central.connected()) {
//delay(200);
//read_sensor();
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(xSensor, ySensor, zSensor);
}
tSensor=HTS.readTemperature();

Serial.print("xSensor ");
Serial.print(xSensor);
Serial.print(" - ySensor ");
Serial.print(ySensor);
Serial.print(" - zSensor ");
Serial.print(zSensor);
Serial.print(" - tSensor ");
Serial.println(tSensor);
Serial.println("");
xBLE=xSensor*pow(10,d_a);
yBLE=ySensor*pow(10,d_a);
zBLE=zSensor*pow(10,d_a);
tBLE=tSensor*pow(10,d_t);

//int t_i=t_f*100;
//int x_i=x_f*100;
//int y_i=y_f*100;
//int z_i=z_f*100;

//writeCharacteristicValue (Tchar, Xchar, Ychar, Zchar);

Serial.print("xBLE ");
Serial.print(xBLE);
Serial.print(" - yBLE ");
Serial.print(yBLE);
Serial.print(" - zBLE ");
Serial.print(zBLE);
Serial.print(" - tBLE ");
Serial.println(tBLE);
Serial.println("");

XChar.writeValue(xBLE);
YChar.writeValue(yBLE);
ZChar.writeValue(zBLE);
TChar.writeValue(tBLE);
//Serial.println("At Main Function");
Serial.print("XChar ");
Serial.print(XChar.value());
Serial.print(" - YChar ");
Serial.print(YChar.value());
Serial.print(" - ZChar ");
Serial.print(ZChar);
Serial.print(" - TChar ");
Serial.println(TChar.value());
Serial.println("");
Serial.println("");
//Serial.println("");
//Serial.println("");

delay(1000);

}
}
else {
delay(1000);
}
digitalWrite(LED_BUILTIN, LOW);
Serial.print("Disconnected from central: ");
Serial.println(central.address());
BLE.advertise();
}

//void read_sensor(){
//if (IMU.accelerationAvailable()) {
//IMU.readAcceleration(x_f, y_f, z_f);
//}
//t_f=HTS.readTemperature();
//
//Serial.print("x_f ");
//Serial.print(x_f);
//Serial.print(" - y_f ");
//Serial.print(y_f);
//Serial.print(" - z_f ");
//Serial.print(z_f);
//Serial.print(" - t_f ");
//Serial.println(t_f);
//Serial.println("");
////x_i=x_f*pow(10,d);
////y_i=y_f*pow(10,d);
////z_i=z_f*pow(10,d);
////t_i=t_f*pow(10,d);
//
//int t_i=t_f;
//int x_i=x_f;
//int y_i=y_f;
//int z_i=z_f;
//
//Serial.print("x_i ");
//Serial.print(x_i);
//Serial.print(" - y_i ");
//Serial.print(y_i);
//Serial.print(" - z_i ");
//Serial.print(z_i);
//Serial.print(" - t_i ");
//Serial.println(t_i);
//Serial.println("");
//}

Central

/*
  BLE_Central_Device.ino

 /*
 * Device: Arduino Nano 33 BLE Sense
 * Central
 * The values of the integrated temperature sensor and 
 * accelerometer of another Nano 33 BLE are received using BLE.
 */

#include <ArduinoBLE.h>

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

  if (!BLE.begin()) {
    Serial.println("* Starting BLE module failed!");
    while (1);
  }

  BLE.setLocalName("Arduino XYZT (Central)"); 
  Serial.println("Arduino XYZT (Central)");
  Serial.println(" ");

  //BLE.advertise();
  
}

void loop() {
  BLE.scan();
 BLEDevice peripheral = BLE.available();

 //Serial.println("- Discovering peripheral device...");

//  do
//  {
//    BLE.scanForUuid("1101");
//    peripheral = BLE.available();
//  } while (!peripheral);

  if (peripheral) {
    Serial.print("Found ");
    Serial.print(peripheral.address());
    Serial.print(" '");
    Serial.print(peripheral.localName());
    Serial.print("' ");
    Serial.print(peripheral.advertisedServiceUuid());
    Serial.println();
    
//    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();

    if (peripheral.localName() == "Arduino XYZT (peripheral)") {
      // stop scanning
      BLE.stopScan();
      
      if (peripheral.connect()) {
       Serial.println("Connected");
       } else {
       Serial.println("Failed to connect!");
       return;
      }
      
     // discover peripheral attributes
      Serial.println("Discovering attributes ...");
      if (peripheral.discoverAttributes()) {
      Serial.println("Attributes discovered");
      } else {
      Serial.println("Attribute discovery failed!");
      peripheral.disconnect();
      return;
      }
      
     // read and print device name of peripheral
     Serial.println();
     Serial.print("Device name: ");
     Serial.println(peripheral.deviceName());
     Serial.print("Appearance: 0x");
     Serial.println(peripheral.appearance(), HEX);
     Serial.println();
     }

  while (peripheral.connect()) {
  
    //BLECharacteristic TChar = peripheral.characteristic( "2104" );
      BLEService service = peripheral.service("1101");
    Serial.print("Service ");
    Serial.print(service.uuid());
    
    //BLECharacteristic characteristic = service.characteristic("2104");
    //readCharacteristicValue(characteristic);
    BLECharacteristic Tchar = service.characteristic("2104");
    //readCharacteristicValue(characteristic);
    Serial.print("\tTemperature characteristic ");
    Serial.print(Tchar.uuid());
    // check if the characteristic is readable
    if (Tchar.canRead()) {
      //read the characteristic value
      int16_t temperature = 0;
      Tchar.readValue(&temperature,2);
      Serial.print(", temperature = ");
      Serial.println((float)temperature/100.0);
    }

    
    BLECharacteristic Xchar = service.characteristic("2101");
    //readCharacteristicValue(characteristic);
    Serial.print("\tX characteristic ");
    Serial.print(Xchar.uuid());
    // check if the characteristic is readable
    if (Xchar.canRead()) {
      //read the characteristic value
      int32_t x = 0;
      Xchar.readValue(&x,4);
      Serial.print(", x = ");
      Serial.println((float)x/100.0);
    }

    BLECharacteristic Ychar = service.characteristic("2102");
    //readCharacteristicValue(characteristic);
    Serial.print("\tY characteristic ");
    Serial.print(Ychar.uuid());
    // check if the characteristic is readable
    if (Ychar.canRead()) {
      //read the characteristic value
      int32_t y = 0;
      Ychar.readValue(&y,4);
      Serial.print(", y = ");
      Serial.println((float)y/100.0);
    }

    BLECharacteristic Zchar = service.characteristic("2103");
    //readCharacteristicValue(characteristic);
    Serial.print("\tZ characteristic ");
    Serial.print(Zchar.uuid());
    // check if the characteristic is readable
    if (Zchar.canRead()) {
      //read the characteristic value
      int32_t z = 0;
      Zchar.readValue(&z,4);
      Serial.print(", z = ");
      Serial.println((float)z/100.0);
    }

    
    delay(1000);
    }


    
//    } else {
//    Serial.println("* Connection to peripheral device failed!");
//    Serial.println(" ");
//    return;
    }
    //Serial.println("Disconnected from peripheral: ");
}

void readCharacteristicValue(BLECharacteristic characteristic) {
  // print the UUID and properties of the characteristic
  Serial.print("\tCharacteristic ");
  Serial.print(characteristic.uuid());
  // check if the characteristic is readable
  if (characteristic.canRead()) {
    // read the characteristic value
    characteristic.read();

    if (characteristic.valueLength() > 0) {
      Serial.print(", value = ");
      //characteristic.value() returns byte array
      unsigned long bitShiftData = 0;
      bitShiftData = (uint32_t)characteristic.value()[3] << 24 | (uint32_t)characteristic.value()[2] << 16 | (uint32_t)characteristic.value()[1] << 8 | characteristic.value()[0];
      Serial.println(bitShiftData, DEC);
      Serial.print(bitShiftData / 100);
      Serial.print('.');
      if (bitShiftData % 100 < 10)
        Serial.print('0');
      Serial.println(bitShiftData % 100);
    }
  }
}

The peripheral code could be cleaner if I had managed to write a proper writeCharacteristicValue function.

Sometimes the reconnection works and sometimes it doesn't. Also sometimes the Attributes discovery fails, and the program stop so I still have to work to make it run until it works. I haven't tried to send the acceleration datas using floats yet.

I have 2 major issues so far:

Another thing is that I get weird values with this part of the peripheral code, so I am not sure how the value function works.

Serial.print("XChar ");
Serial.print(XChar.value());
Serial.print(" - YChar ");
Serial.print(YChar.value());
Serial.print(" - ZChar ");
Serial.print(ZChar);
Serial.print(" - TChar ");
Serial.println(TChar.value());
Serial.println("");

Thanks again @cattledog, still have a lot to do but you have helped me a lot with this first step !