BLE 33 as Peripheral fail to write on Server Char

Hi,

I'm using a Nano 33 BLE Sense as BLE peripheral (client BLE) which writes every random time (between 60 to 90 seconds) over a BLE GATT Server with 6 characteristics made on a Raspberry Pi3.

Unfortunately, when the BLE 33 Nano connects to the server, can only find two of the four chars that I need. It can write on this two chars (temperature and humidity), while it can't write on pressure and proximity chars). I can't explain why. Also, sometimes - let's say 20% of the times - it fail to discover the GATT server attributes, ending the connection without writing on any chars.

Do you have any idea?

Here you can find my code:

#include <Arduino_HTS221.h>
#include <Arduino_LPS22HB.h>
#include <Arduino_APDS9960.h>
#include <ArduinoBLE.h>

//readings variables
String pressure;
String temperature;
String humidity;
String proximity;

//rand
long randDelay;

void setup() {
Serial.begin(115200);
randomSeed(analogRead(0));

// initialize the BLE hardware
BLE.begin();

// Sensor Init
if (!HTS.begin()) {
Serial.println("Failed to initialize humidity temperature sensor!");
while (1);
}
if (!BARO.begin()) {
Serial.println("Failed to initialize pressure sensor!");
while (1);
}
if (!APDS.begin()) {
Serial.println("Error initializing APDS9960 sensor!");
}

//Configure WDT.
NRF_WDT->CONFIG = 0x01; // Configure WDT to run when CPU is asleep
NRF_WDT->CRV = 2359296; // CRV = timeout * 32768 + 1
NRF_WDT->RREN = 0x01; // Enable the RR[0] reload register
NRF_WDT->TASKS_START = 1; // Start WDT
}

void loop() {
// start scanning for peripherals
BLE.scanForUuid("c8e459b1-85b8-4816-99b4-3c1a34b53437");
// check if a peripheral has been discovered
BLEDevice peripheral = BLE.available();

// Reload the WDTs RR[0] reload register
NRF_WDT->RR[0] = WDT_RR_RR_Reload;

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();

// write data to the right characteritcs
writeReadings(peripheral);
randDelay = random(60, 90);
Serial.println(randDelay);
delay(randDelay*1000);
}
}

void exploreCharacteristic(BLECharacteristic characteristic) {
// print the UUID and properties of the characteristic
Serial.print("\tCharacteristic ");
Serial.print(characteristic.uuid());
Serial.print(", properties 0x");
Serial.print(characteristic.properties(), HEX);

// check if the characteristic is readable
if (characteristic.canRead()) {
// read the characteristic value
characteristic.read();

if (characteristic.valueLength() > 0) {
// print out the value of the characteristic
Serial.print(", value 0x");
printData(characteristic.value(), characteristic.valueLength());
}
}
Serial.println();
}

void printData(const unsigned char data[], int length) {
for (int i = 0; i < length; i++) {
unsigned char b = data*;*

  • if (b < 16) {*

  • Serial.print("0");*

  • }*

  • Serial.print(b, HEX);*

  • }*
    }
    void writeReadings(BLEDevice peripheral) {

  • // connect to the peripheral*

  • Serial.println("Connecting ...");*

  • // check if connected*

  • 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;*

  • }//*

  • // retrieve data characteristics*

  • BLECharacteristic tempChar = peripheral.characteristic("460240d0-e610-4fd5-8a95-a885a30f0660");*

  • delay(50);*

  • BLECharacteristic humChar = peripheral.characteristic("460240d0-e610-4fd5-8a95-a885a30f0661");*

  • delay(50);*

  • BLECharacteristic prChar = peripheral.characteristic("460240d0-e610-4fd5-8a95-a175a30f0662");*

  • delay(50);*

  • BLECharacteristic prxChar = peripheral.characteristic("460240d0-e610-4fd5-8a95-a175a30f0665");*

  • delay(50);*

  • if (peripheral.connected()) {*

  • //Read from sensors*

  • pressure = String(BARO.readPressure());*

  • temperature = String(HTS.readTemperature());*

  • humidity = String(HTS.readHumidity());*

  • proximity = String(APDS.readProximity());*

  • // print readings*

  • Serial.println("Last read was: T="+temperature+" C, RH="+humidity+" %, Pr="+pressure+" kPa, Proximity="+proximity);*

  • //exploreCharacteristic(humChar);*

  • //exploreCharacteristic(prxChar);*

  • // update Char value*

  • tempChar.writeValue(temperature.c_str());*

  • delay(50);*

  • humChar.writeValue(humidity.c_str());*

  • delay(50);*

  • prChar.writeValue(pressure.c_str());*

  • delay(50);*

  • prxChar.writeValue(proximity.c_str());*

  • delay(50);*

  • }*

  • peripheral.disconnect();*

  • Serial.println("Peripheral disconnected");*
    }
    [/quote]
    The server has no Issues, since I can write on it on all GATT chars using one ESP32 and my mobile Phone
    Thank you

What do you use as BLE server on your Raspberry Pi? If it is something quick to setup I could run some tests.