Hey guys, can anyone give me some advice on my problem?
My project consists of two devices that transfer data between them using BLE:
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).
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:
Actual values of the peripheral UUID attributes:
0. "d7dc7fe1-69c1-4f17-a418-2eaacad6aae7"
"db698115-1e2b-498f-aafd-461e855d584f"
"70697197-a48a-4658-a64f-12e17786b999"
"b8c83044-3d8a-4ff2-84ba-b8c0dca841c8"
"f463d4a4-df3e-41dd-bee7-ec0ddc65802f"
"6ae341a2-579a-422d-9287-5a81b838bbfe"
"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?
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.