[NICLA SENSE ME] fluctuating execution time of writeValue() function (BLE)

Hello!

I'm experiencing a problem with BLE on NICLA SENSE ME.
(NICLA SENSE ME -> PC through Notify)

The issue is that the transmission rate is not consistent. I captured the packets using Wireshark and it showed that packets arrived inconsistently. The interval between the packets varied from 1ms to 50ms.

So I logged the execution time of my Arduino code and I found that the behavior of writeValue() is a bit strange.

The below is the execution time of writeValue() . The execution time fluctuates but somehow it seems that there's a kind of a pattern (2 short ones followed by a long one)

(unit in microsecond)

09:44:08.211 -> 30
09:44:08.211 -> 30
09:44:08.211 -> 29
09:44:08.244 -> 16465
09:44:08.244 -> 29
09:44:08.244 -> 29
09:44:08.277 -> 26197
09:44:08.277 -> 29
09:44:08.277 -> 29
09:44:08.344 -> 18755
09:44:08.344 -> 29
09:44:08.344 -> 29
09:44:08.377 -> 24714
09:44:08.377 -> 29
09:44:08.377 -> 29
09:44:08.443 -> 24819
09:44:08.443 -> 29
09:44:08.443 -> 29
09:44:08.476 -> 26612
09:44:08.476 -> 29
09:44:08.476 -> 29
09:44:08.543 -> 24605
09:44:08.543 -> 30
09:44:08.543 -> 29
09:44:08.576 -> 33216
09:44:08.576 -> 29
09:44:08.576 -> 29
09:44:08.663 -> 25791
09:44:08.663 -> 29
09:44:08.663 -> 29
09:44:08.663 -> 25248

So my questions are

  • Has anyone had a similar issue like this?
  • Is there a way to send data constantly over BLE? (as fast as possible)

Thank you!

This is my code:

#include "Nicla_System.h"
#include "Arduino_BHY2.h"
#include <ArduinoBLE.h>

#define BLE_SENSE_UUID(val) ("19b10000-" val "-537e-4f6c-d104768a1214")

const int VERSION = 0x00000000;

BLEService service(BLE_SENSE_UUID("0000"));
BLECharacteristic quaternionCharacteristic(BLE_SENSE_UUID("0003"), BLENotify, 4 * sizeof(float));     

SensorQuaternion quaternion(SENSOR_ID_RV);

String name;
const long interval = 10;          
unsigned long previousMillis = 0;


void setup(){
  Serial.begin(115200);
  Serial.println("Start");

  nicla::begin();
  nicla::leds.begin();
  nicla::leds.setColor(green);

  //Sensors initialization
  BHY2.begin();
  quaternion.begin();

  if (!BLE.begin()){

    while (1)
      ;
  }

  String address = BLE.address();

  address.toUpperCase();

  name = "jsBLESense-";
  name += address[address.length() - 5];
  name += address[address.length() - 4];
  name += address[address.length() - 2];
  name += address[address.length() - 1];

  BLE.setLocalName(name.c_str());
  BLE.setDeviceName(name.c_str());
  BLE.setAdvertisedService(service);

  service.addCharacteristic(quaternionCharacteristic);
  
  BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
  BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
  
  BLE.addService(service);
  BLE.advertise();
}

void loop(){
  while(BLE.connected()){
    BHY2.update();

    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      
      if(quaternionCharacteristic.subscribed()){
        float x, y, z, w;
        x = quaternion.x();
        y = quaternion.y();
        z = quaternion.z();
        w = quaternion.w();
        float quaternionValues[] = {x,y,z,w};
        unsigned long start = micros();
        quaternionCharacteristic.writeValue(quaternionValues, sizeof(quaternionValues));
        unsigned long end = micros();
        unsigned long delta = end - start;
        Serial.println(delta);

      }
      
    }
  }
}


void blePeripheralConnectHandler(BLEDevice central){
  nicla::leds.setColor(blue);  
}
void blePeripheralDisconnectHandler(BLEDevice central){
  nicla::leds.setColor(red);
}

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