Ciao a tutti.
Sto cercando di trasmettere i valori di alcune variabile dalla periferica (OPTA) al central (NANO) tramite BLE.
Il sistema funziona per alcuni cicli; poi inizia a rallentarsi fino a bloccarsi ed il central non legge più i dati.
Connettendo la periferica OPTA tramite l'applicazione nRF Connect non noto alcuna anomalia. I dati vengono regolarmente trasmessi all'app.
Aggiungo che basta resettare la periferica e il central legge nuovamente per un pò di cicli.
Infatti il central dopo un pò di cicli non trova più la periferica; Serial.println(peripheral) restituisce 0;
Allego lo skecth della periferica.
Grazie
#include <ArduinoBLE.h>
void setup() {
Serial.begin(9600);
BLE.begin();
Serial.println("Bluetooth® Low Energy Central - Peripheral Explorer");
BLE.scan(); // start scanning for peripherals ////UUID del servizio
}
void loop() {
// check if a peripheral has been discovered
BLEDevice peripheral = BLE.available();
if (peripheral) {
if (peripheral.localName() == "OPTA_1") {
// stop scanning
BLE.stopScan();
lettura_dati(peripheral);
// peripheral disconnected, start scanning again
BLE.scan();
}
}
}
void lettura_dati(BLEDevice peripheral) {
// connect to the peripheral
Serial.println("Connecting ...");
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;
}
// read and print device name of peripheral
Serial.println();
Serial.print("Device name: ");
Serial.println(peripheral.deviceName());
Serial.print("Appearance: 0x");
Serial.println(peripheral.appearance(), HEX);
Serial.println();
// loop the services of the peripheral and explore each
for (int i = 0; i < peripheral.serviceCount(); i++) {
BLEService service = peripheral.service(i);
exploreService(service);
}
Serial.println();
// we are done exploring, disconnect
Serial.println("Disconnecting ...");
peripheral.disconnect();
Serial.println("Disconnected");
}
void exploreService(BLEService service) {
// print the UUID of the service
Serial.print("Service ");
Serial.println(service.uuid());
// loop the characteristics of the service and explore each
for (int i = 0; i < service.characteristicCount(); i++) {
BLECharacteristic characteristic = service.characteristic(i);
exploreCharacteristic(characteristic);
}
}
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();
// loop the descriptors of the characteristic and explore each
for (int i = 0; i < characteristic.descriptorCount(); i++) {
BLEDescriptor descriptor = characteristic.descriptor(i);
}
}
void printData(const unsigned char data[], int length) {
for (int i = 0; i < length; i++) {
unsigned char b = data[i];
if (b < 16) {
Serial.print("0");
}
Serial.print(b, HEX);
}
}