Unable to obtain correct attribute values and subscribe in Arduino BLE project

Hey guys, can anyone give me some advice on my problem?
My project consists of two devices that transfer data between them using BLE:

  1. Arduino NANO 33 BLE - it works as a peripheral device that has set "setAdvertisedService("9aa8d10d-d79c-4339-a84b-8599f02c8759")" and has several services associated with it that have their unique UUIDs and are set to read and notify (BLERead | BLENotify).

  2. Arduino UNO WiFi rev2 - serves as a central device that connects to 1. Arduino NANO 33 BLE.
    This Arduino UNO WiFi rev2 device can find a peripheral, connect to it, but always fails to detect attributes and set subscriptions.
    That's why I inserted an intermediate step where after discovering the attributes it lists them. That's how I figured out that it misreads them and keeps listing different values: an example of a serial port output:

21:43:40.328 -> Failed to subscribe!
21:43:40.361 -> Found 15:c9:20:c0:1c:a8 'Arduino Nano 33 BLE' 9aa8d10d-d79c-4339-a84b-8599f02c8759
21:43:40.457 -> Connected
21:43:41.644 -> Attributes discovered
21:43:42.667 -> Characteristic 0: d7dc7fe1-69c1-4f17-a418-2eaacad6aae7
21:43:42.734 -> Characteristic 1: db698115-1e2b-498f-aafd-461e855d584f
21:43:42.767 -> Characteristic 2: 70697197-a48a-4658-a64f-12e17786b999
21:43:42.832 -> Characteristic 3: b8c83044-3d8a-4ff2-84ba-b8c0dca841c8
21:43:42.899 -> Characteristic 4: f463d4a4-df3e-41dd-bee7-ec0ddc65802f
21:43:42.960 -> Characteristic 5: 6ae341a2-579a-422d-9287-5a81b838bbfe
21:43:42.992 -> Characteristic 6: 37f822cd-ae9e-4d59-93f0-8f429a50c605
21:43:43.056 -> Failed to subscribe!
21:43:43.088 -> Found 15:c9:20:c0:1c:a8 'Arduino Nano 33 BLE' 9aa8d10d-d79c-4339-a84b-8599f02c8759
21:43:43.383 -> Connected
21:43:44.573 -> Attributes discovered
21:43:45.598 -> Characteristic 0: d7dc7fe1-69c1-4f17-a418-2eaacad6aae7
21:43:45.630 -> Characteristic 1: fb698115-1e2b-498f-aafd-461e855d584f
21:43:45.693 -> Characteristic 2: 70697197-a48a-4658-a64f-12e17786b999
21:43:45.758 -> Characteristic 3: b8c83044-3d8a-4ff2-84ba-b8c0dca841c8
21:43:45.822 -> Characteristic 4: fa63d4a4-df3e-41dd-bee7-ec0ddc65802f
21:43:45.886 -> Characteristic 5: 65e341a2-579a-422d-9287-5a81b838bbfe
21:43:45.918 -> Characteristic 6: 37f822cd-ae9e-4d59-93f0-8f429a50c605
21:43:45.981 -> Failed to subscribe!
21:43:46.013 -> Found 15:c9:20:c0:1c:a8 'Arduino Nano 33 BLE' 9aa8d10d-d79c-4339-a84b-8599f02c8759
21:43:46.177 -> Connected
21:43:47.402 -> Attributes discovered
21:43:48.395 -> Characteristic 0: d7dc7fe1-69c1-4f17-a418-2eaacad6aae7
21:43:48.459 -> Characteristic 1: fb698115-1e2b-498f-aafd-461e855d584f
21:43:48.491 -> Characteristic 2: 70697197-a48a-4658-a64f-12e17786b999
21:43:48.554 -> Characteristic 3: bcc83044-3d8a-4ff2-84ba-b8c0dca841c8
21:43:48.619 -> Characteristic 4: fa63d4a4-df3e-41dd-bee7-ec0ddc65802f
21:43:48.682 -> Characteristic 5: 6ae341a2-579a-422d-9287-5a81b838bbfe
21:43:48.747 -> Characteristic 6: 37f822cd-ae9e-4d59-93f0-8f429a50c605

Actual values of the peripheral UUID attributes:
0. "d7dc7fe1-69c1-4f17-a418-2eaacad6aae7"

  1. "db698115-1e2b-498f-aafd-461e855d584f"
  2. "70697197-a48a-4658-a64f-12e17786b999"
  3. "b8c83044-3d8a-4ff2-84ba-b8c0dca841c8"
  4. "f463d4a4-df3e-41dd-bee7-ec0ddc65802f"
  5. "6ae341a2-579a-422d-9287-5a81b838bbfe"
  6. "37f822cd-ae9e-4d59-93f0-8f429a50c605"

I also determined whether the device was indeed sending the correct attribute UUIDs by using the LightBlue app, which displayed the correct UUIDs for each attribute and was able to connect and log in.

I would like to ask where the error is and why I am unable to get the correct attribute values, due to which I am probably subsequently unable to subscribe?

Can you please post an example code for both devices that shows the behavior? Sometimes the devil is in the details.

That is a good strategy. You should first make sure your peripheral device works as expected using a standard BLE app.

Yes I have tried reading this data with a standard BLE app (LightBlue) and it works. Therefore I assume the peripheral is well set up. However, I can't figure out where in the code of the central device the error is. Specifically, the error is that the device keeps getting confused when discovering Attributes and their UUIDs. Therefore I when I then enter the expected UUID it is not able to subscribe. But I don't know how to prevent this error. Could it be a hardware bug in the Arduino UNO Wifi Rev2?
Here is an example of part of the code of both devices.

1. Peripheral device (Arduino NANO 33BLE):

BLEService sensorService("9aa8d10d-d79c-4339-a84b-8599f02c8759"); // UUID pre náš BLE servis
//BME280
BLEFloatCharacteristic temperatureCharacteristic("d7dc7fe1-69c1-4f17-a418-2eaacad6aae7", BLERead | BLENotify);  // UUID pre charakteristiku temperature
BLEFloatCharacteristic humidityCharacteristic("db698115-1e2b-498f-aafd-461e855d584f", BLERead | BLENotify);     // UUID pre charakteristiku humidity
BLEFloatCharacteristic pressureCharacteristic("70697197-a48a-4658-a64f-12e17786b999", BLERead | BLENotify);     // UUID pre charakteristiku pressure
BLEFloatCharacteristic altitudeCharacteristic("b8c83044-3d8a-4ff2-84ba-b8c0dca841c8", BLERead | BLENotify);     // UUID pre charakteristiku altitude
BLEFloatCharacteristic dewpointCharacteristic("f463d4a4-df3e-41dd-bee7-ec0ddc65802f", BLERead | BLENotify);     // UUID pre charakteristiku des point
//CCS811
BLEFloatCharacteristic CO2Characteristic("6ae341a2-579a-422d-9287-5a81b838bbfe", BLERead | BLENotify);  // UUID pre charakteristiku CO2
BLEFloatCharacteristic TVOCCharacteristic("37f822cd-ae9e-4d59-93f0-8f429a50c605", BLERead | BLENotify); // UUID pre charakteristiku TVOC

void setupBLE(){
  
    // Inicializácia BLE
  if (!BLE.begin()) {
    Serial.println("Inicializácia BLE zlyhala!");
    while (1);
  }

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  // Definovanie servisu a charakteristík
  BLE.setLocalName("Arduino Nano 33 BLE");
  BLE.setAdvertisedService(sensorService);
  sensorService.addCharacteristic(temperatureCharacteristic);
  sensorService.addCharacteristic(humidityCharacteristic);
  sensorService.addCharacteristic(pressureCharacteristic);
  sensorService.addCharacteristic(altitudeCharacteristic);
  sensorService.addCharacteristic(dewpointCharacteristic);

  sensorService.addCharacteristic(CO2Characteristic);
  sensorService.addCharacteristic(TVOCCharacteristic);

  BLE.addService(sensorService);

  // Spustenie BLE reklamy
  BLE.advertise();
}

void loopBLE(){
  // Spracovanie udalostí BLE
  BLE.poll();
//...code continuation
}

2. Central device (Arduino UNO WiFi rev2):


void setupBLE(){
    if (!BLE.begin()) {
    Serial.println(F("Failed to initialize BLE"));
    while (1);
  }
  // start scanning for peripherals
  BLE.scanForUuid("9aa8d10d-d79c-4339-a84b-8599f02c8759");
}
void scanBLE(){
  // check if a peripheral has been discovered
  BLEDevice peripheral = BLE.available();

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

    // stop scanning
    BLE.stopScan();

    data(peripheral);

    // peripheral disconnected, start scanning again
    BLE.scanForUuid("9aa8d10d-d79c-4339-a84b-8599f02c8759");
  }
}
void data(BLEDevice peripheral) {
  // connect to the peripheral
  if (peripheral.connect()) {
    Serial.println("Connected");
  } else {
    Serial.println("Failed to connect!");
    return;
  }
  // discover peripheral attributes
  if (peripheral.discoverAttributes()) {
    Serial.println("Attributes discovered");
    delay(1000);
    // Print discovered attributes
    BLEService service = peripheral.service("9aa8d10d-d79c-4339-a84b-8599f02c8759");
    for (int i = 0; i < service.characteristicCount(); i++) {
      BLECharacteristic characteristic = service.characteristic(i);
      Serial.print("Characteristic ");
      Serial.print(i);
      Serial.print(": ");
      Serial.println(characteristic.uuid());
      delay(10);
    }
  } else {
    Serial.println("Attribute discovery failed!");
    peripheral.disconnect();
    return;
  }

  //BME280
  BLECharacteristic temperature = peripheral.characteristic("19B10012-E8F2-537E-4F6C-D104768A1214");
  BLECharacteristic humidity    = peripheral.characteristic("19B10013-E8F2-537E-4F6C-D104768A1214");
  BLECharacteristic pressure    = peripheral.characteristic("19B10014-E8F2-537E-4F6C-D104768A1214");
  BLECharacteristic altitude    = peripheral.characteristic("19B10015-E8F2-537E-4F6C-D104768A1214");
  BLECharacteristic dewpoint    = peripheral.characteristic("19B10016-E8F2-537E-4F6C-D104768A1214");

  //CSS811
  BLECharacteristic CO2 = peripheral.characteristic("19B20022-E8F2-537E-4F6C-D104768A1214");
  BLECharacteristic TVOC    = peripheral.characteristic("19B20023-E8F2-537E-4F6C-D104768A1214");

  // subscribe to the characteristics
  if (!temperature.subscribed() || !humidity.subscribed() || !pressure.subscribed() || !altitude.subscribed() || !dewpoint.subscribed() || !CO2.subscribed() || !TVOC.subscribed()){
    if (!temperature.subscribe() || !humidity.subscribe() || !pressure.subscribe() || !altitude.subscribe() || !dewpoint.subscribe() || !CO2.subscribe() || !TVOC.subscribe()) {
      Serial.println("Failed to subscribe!");
      peripheral.disconnect();
      return;
    }
  }
//...code continuation

So today problem solved:
After swapping the functionalities of my devices, using the Arduino Wifi Rev 2 as a peripheral and the Arduino Nano 33 BLE as a central device with the same code loaded, everything works properly. Hence, my Arduino Wifi Rev2 is not working properly as a central device - it cannot read the UUID attributes transmitted by the peripherals correctly. This is clearly a hardware bug, as my code works correctly on another device. It also can't be a bug in the Arduino Nano 33 BLE, as it works correctly as a peripheral when connected via the BlueLight app. So I have no choice but to advertise this device again.

Honestly this is the 3rd defective piece of this board I have. The 1st piece came to me with a broken antenna on the BLE module. The 2nd piece had a completely non-functional BLE module and could not be initialized at all. And finally this 3rd piece... . I would definitely consider buying this board again.

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