MKRWIFI1010 - BLE ISSUE - .discoverAttributes() locked/frozzen

Hi,
I'm trying to use two MKR1010 for data exchange via BLE.

The Central Device is working fine when i try it with the LightBleu App (with my phone).
I can access and read the two characteristic.

But when i try to access with the second MKR1010. The connection ton the Central works fine but the progamm is locked/frozen a the .discoverAttributes() instruction.

Central code :slight_smile:

#include <ArduinoBLE.h>



const char* BLE_UUID_A1_FLOAT = "9b10000-e8f2-537e-4f6c-d104768a6666-001";
const char* BLE_UUID_A2_FLOAT = "9b10000-e8f2-537e-4f6c-d104768a6666-002";
#define CARD_ID  "DAAS_SPIDER_CARD_BLE_001"
const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a6666";

//BLEService newService("180A"); // creating the service

//BLEFloatCharacteristic floatValueCharacteristic_A1( BLE_UUID_A1_FLOAT, BLERead | BLENotify );
//BLEFloatCharacteristic floatValueCharacteristic_A2( BLE_UUID_A1_FLOAT, BLERead | BLENotify );


void setup() {
  Serial.begin(9600);    // initialize serial communication
  while (!Serial);       //starts the program if we open the serial monitor.
 
  //initialize ArduinoBLE library
  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy failed!");
    while (1);
  } else

  {
    Serial.println("Bluetooth® Low Energy Started");
    //BLE.scan();
    BLE.setLocalName("MKR_IOT2050"); 
    BLE.advertise();
}

}

 void loop() {
  // check if a peripheral has been discovered


  
  connectToPeripheral();

 }

void connectToPeripheral(){
  BLEDevice peripheral;
  
  Serial.println("- Discovering peripheral device...");

  do
  {
    BLE.scanForUuid(deviceServiceUuid);
    peripheral = BLE.available();
    Serial.print(".");
    delay(100);
  } while (!peripheral);
  
  if (peripheral) {
    Serial.println("* Peripheral device found!");
    Serial.print("* Device MAC address: ");
    Serial.println(peripheral.address());
    Serial.print("* Device name: ");
    Serial.println(peripheral.localName());
    Serial.print("* Advertised service UUID: ");
    Serial.println(peripheral.advertisedServiceUuid());
    Serial.println(" ");
    BLE.stopScan();
    
    controlPeripheral(peripheral);
  }
}

void controlPeripheral(BLEDevice peripheral) {
  Serial.println("- Connecting to peripheral device...");

  if (peripheral.connect()) {
    Serial.println("* Connected to peripheral device!");
    Serial.println(" ");
  } else {
    Serial.println("* Connection to peripheral device failed!");
    Serial.println(" ");
    return;
  }

 Serial.println("Discovering attributes ...");
 if (peripheral.discoverAttributes()) {
 Serial.println("Attributes discovered");
 } else {
   Serial.println("Attribute discovery failed!");
    peripheral.disconnect();
  return;
  }
  

  BLECharacteristic VAL_A1 = peripheral.characteristic(BLE_UUID_A1_FLOAT);
  BLECharacteristic VAL_A2 = peripheral.characteristic(BLE_UUID_A2_FLOAT);  
   
  //if (!VAL_A1) {
   // Serial.println("* Peripheral device does not have VAL_A1 characteristic!");
   // peripheral.disconnect();
   // return;
   //}

   //if (!VAL_A2) {
  //  Serial.println("* Peripheral device does not have VAL_A2 characteristic!");
   // peripheral.disconnect();
   // return;
  // }
  
  while (peripheral.connected()) {

  delay(3000);

    float floatValue1;
      VAL_A1.readValue( &floatValue1, 4 );
      Serial.print("Read A1 :");
      Serial.println( floatValue1 );

    float floatValue2;
      VAL_A2.readValue( &floatValue2, 4 );
      Serial.print("Read A2 :");
      Serial.println( floatValue2 );
 
  
  }
  Serial.println("- Peripheral device disconnected!");
}

Sensor Code

#include <ArduinoBLE.h>

const char* BLE_UUID_A1_FLOAT = "9b10000-e8f2-537e-4f6c-d104768a6666-001";
const char* BLE_UUID_A2_FLOAT = "9b10000-e8f2-537e-4f6c-d104768a6666-002";
#define CARD_ID  "DAAS_SPIDER_CARD_BLE_001"
const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a6666";

BLEService newService(deviceServiceUuid); // creating the service



BLEUnsignedCharCharacteristic randomReading("2A58", BLERead | BLENotify); // creating the Analog Value characteristic
BLEByteCharacteristic switchChar("2A57", BLERead | BLEWrite); // creating the LED characteristic


BLEFloatCharacteristic floatValueCharacteristic_A1( BLE_UUID_A1_FLOAT, BLERead | BLENotify );
BLEFloatCharacteristic floatValueCharacteristic_A2( BLE_UUID_A2_FLOAT, BLERead | BLENotify );

const int ledPin = 2;
long previousMillis = 0;


void setup() {
  Serial.begin(9600);    // initialize serial communication
  //while (!Serial);       //starts the program if we open the serial monitor.

  pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected
  pinMode(ledPin, OUTPUT); // initialize the built-in LED pin to indicate when a central is connected

  //initialize ArduinoBLE library
  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy failed!");
    while (1);
  }

  BLE.setLocalName(CARD_ID); //Setting a name that will appear when scanning for Bluetooth® devices
  
  BLE.setAdvertisedService(newService);

  newService.addCharacteristic(switchChar); //add characteristics to a service
  newService.addCharacteristic(randomReading);

  newService.addCharacteristic(floatValueCharacteristic_A1);
  newService.addCharacteristic(floatValueCharacteristic_A2);

  BLE.addService(newService);  // adding the service

  switchChar.writeValue(0); //set initial value for characteristics
  randomReading.writeValue(0);


  floatValueCharacteristic_A1.writeValue(10);
  floatValueCharacteristic_A2.writeValue(25.78);


  BLE.advertise(); //start advertising the service
  Serial.println(" Bluetooth® device active, waiting for connections...");
}

void loop() {
  
  BLEDevice central = BLE.central(); // wait for a Bluetooth® Low Energy central

  if (central) {  // if a central is connected to the peripheral
    Serial.print("Connected to central: ");
    
    Serial.println(central.address()); // print the central's BT address
    
    digitalWrite(LED_BUILTIN, HIGH); // turn on the LED to indicate the connection

    // check the battery level every 200ms
    // while the central is connected:
    while (central.connected()) {
      long currentMillis = millis();
      
      if (currentMillis - previousMillis >= 200) { // if 200ms have passed, we check the battery level
        previousMillis = currentMillis;

        int randomValue = analogRead(A1);
        randomReading.writeValue(randomValue);

        if (switchChar.written()) {
          if (switchChar.value()) {   // any value other than 0
            Serial.println("LED on");
            digitalWrite(ledPin, HIGH);         // will turn the LED on
          } else {                              // a 0 value
            Serial.println(F("LED off"));
            digitalWrite(ledPin, LOW);          // will turn the LED off
          }
        }

      }
    }
    
    digitalWrite(LED_BUILTIN, LOW); // when the central disconnects, turn off the LED
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }
}

Serial print outpout

image

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