BLE Central Device slow reading

Hi,

I just purchased a Arduino Nano 33 IOT to use as a Bluetooth LE central device to read data from a Ultrasonic anemometer (www.calypsoinstruments.com).

With the help from the examples I’m able to connect and subscribe the characteristic that sends the wind data:

BLECharacteristic DataInCharacteristic = ULTRASONIC.characteristic("2a39");

DataInCharacteristic.read();
TimerStart = millis(); // to test the update frequency
printData(DataInCharacteristic.value(), DataInCharacteristic.valueLength());

Serial.print("TimerStart = ");Serial.println(TimerStart);

void printData(const byte data[], int length) {
  for (int i = 0; i < length; i++) {
       byte b = data[i]; 
        dataW[i] = data[i];    
  }
}

Updating the wind information takes 400 milliseconds where the device sends at a standard 4 Hz.

This (Ultrasonic) update frequency can be changed to 8 Hz but the update frequency in the serial monitor remains at 2,5 Hz. Also looking a little bit behind the reality.

Is there a method to read the data at the same speed as the anemometer is sending? In Android I know “oncharacteristicchanged” is a callback that triggers when the result changes, unfortunately I could not find information, or an example, for this methode with the Arduino BLE library.

Hope someone can help. Thanks in advance.

I cannot help. I don't do snippets. http://snippets-r-us.com

Thats also a way to tell a newbie he’s doing it wrong. Almost the entire code is from the example, but here it is:

#include <ArduinoBLE.h>

byte dataW[10];
unsigned long TimerStart;

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

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");

    while (1);
  }

  Serial.println("BLE Central - Ultrasonic");
  Serial.println("Make sure to turn on the device.");

  // start scanning for ULTRASONIC
  BLE.scan();
}

void loop() {
  // check if a ULTRASONIC has been discovered
  BLEDevice ULTRASONIC = BLE.available();

  if (ULTRASONIC) {
    // discovered a ULTRASONIC, print out address, local name, and advertised service
    Serial.print("Found ");
    Serial.print(ULTRASONIC.address());
    Serial.print(" '");
    Serial.print(ULTRASONIC.localName());
    Serial.print("' ");
    Serial.print(ULTRASONIC.advertisedServiceUuid());
    Serial.println();

    // Check if the ULTRASONIC is a SensorTag, the local name will be:
    // "CC2650 SensorTag"
    if (ULTRASONIC.localName() == "ULTRASONIC") {
      // stop scanning
      BLE.stopScan();

      connectUltrasonic(ULTRASONIC);

      // ULTRASONIC disconnected, start scanning again
      BLE.scan();
    }
  }
}

void connectUltrasonic(BLEDevice ULTRASONIC) {
  // connect to the ULTRASONIC
  Serial.println("Connecting ...");
  if (ULTRASONIC.connect()) {
    Serial.println("Connected");
  } else {
    Serial.println("Failed to connect!");
    return;
  }

  // discover ULTRASONIC attributes
  Serial.println("Discovering service 0x180d ...");
  if (ULTRASONIC.discoverService("180d")) {
    Serial.println("Service discovered");
  } else {
    Serial.println("Attribute discovery failed.");
    ULTRASONIC.disconnect();

    while (1);
    return;
  }

  // retrieve characteristic
  BLECharacteristic DataInCharacteristic = ULTRASONIC.characteristic("2a39");

  // subscribe to the simple key characteristic
  Serial.println("Subscribing to DataInCharacteristic characteristic ...");
  if (!DataInCharacteristic) {
    Serial.println("no DataInCharacteristic characteristic found!");
    ULTRASONIC.disconnect();
    return;
  } else if (!DataInCharacteristic.canSubscribe()) {
    Serial.println("DataInCharacteristic characteristic is not subscribable!");
    ULTRASONIC.disconnect();
    return;
  } else if (!DataInCharacteristic.subscribe()) {
    Serial.println("subscription failed!");
    ULTRASONIC.disconnect();
    return;
  } else {
    Serial.println("Subscribed");
  }

  while (ULTRASONIC.connected()) {
    // while the ULTRASONIC is connected

      ///////  //exploreCharacteristic(DataInCharacteristic);
    DataInCharacteristic.read();
    TimerStart = millis();
    printData(DataInCharacteristic.value(), DataInCharacteristic.valueLength());
    Serial.print("TimerStart = ");Serial.println(TimerStart);
  }

  Serial.println("Ultrasonic disconnected!");
}

void printData(const byte data[], int length) {
  for (int i = 0; i < length; i++) {
    byte b = data[i]; //
    dataW[i] = data[i];
    }
}

and a short result from the serial monitor (hope I’m doing it right this time :wink: ):

Found 'ULTRASONIC' 180d
Connecting ...
Connected
Discovering service 0x180d ...
Service discovered
Subscribing to DataInCharacteristic characteristic ...
Subscribed
TimerStart = 14375
TimerStart = 14775
TimerStart = 15175
TimerStart = 15575

What I see shown for the serial monitor doesn't seem to match well with the code. In any case, I am sorry but I do not know BLE very well.

You should probably look at setEventHandler(), setAdvertisingInterval(), and setConnectionInterval() in
https://www.arduino.cc/en/Reference/ArduinoBLE

Good Luck!

In any case, the slave cannot go any faster than the master allows, and vice versa.

I think I'm asking to read instead of using notification to retrieve the value.

Tried "setEventHandler" without succes. Then the value is 1 but the length is 0 where, with read() the length was 10.