arduino-libraries / ArduinoBLE

ArduinoBLE library for Arduino
GNU Lesser General Public License v2.1
313 stars 205 forks source link

(Nano.33 BLE Sense) problem with one Central and multi ( up to 10 ) peripherals #205

Open AR-kazemzadeh opened 3 years ago

AR-kazemzadeh commented 3 years ago

Hi Dear, I'm trying to connect multi ( up to 10 ) Nano.33 BLE Senes together with BLE, but I can connect two peripherals with ArduinoBLE.H v1.2.1 and Mbed OS 2.5.2. I do this test with Portenta H7 as Central and Nano.33 BLE Senes as peripherals, but I can't connect more than two peripherals. After trying Link By @polldo changes, I can connect with Portenta H7 as central to 3 Nano.33 BLE Senes as peripherals, but it's not Stable. I should reset ten times so I can connect to 3 peripherals. And I can't connect to more than two peripherals with Nano.33 BLE Senes as Central and peripherals. Below you can find a simplification of my code for the central and the peripheral (other peripherals are equal but with the name changed). Best Regards

Central:


#include 

//----------------------------------------------------------------------------------------------------------------------
// BLE UUIDs
//----------------------------------------------------------------------------------------------------------------------

// https://www.bluetooth.com/specifications/gatt/services//
// https://www.bluetooth.com/specifications/gatt/characteristics/

// define LED service & characteristic UUID
#define BLE_UUID_LED                             "19B10000-E8F2-537E-4F6C-D104768A1214" 
#define BLE_UUID_LED_CHAR                        "19B10001-E8F2-537E-4F6C-D104768A1214" 

// define max number of peripherals we want Scan 
#define BLE_MAX_PERIPHERALS 15  
// max interval for find peripherals
#define BLE_SCAN_INTERVALL 10000

// array of scaned peripherals
BLEDevice peripherals[BLE_MAX_PERIPHERALS];
// array of scaned peripherals characteristic
BLECharacteristic ledCharacteristics[BLE_MAX_PERIPHERALS];
// array of Led state for each peripherals
byte state[BLE_MAX_PERIPHERALS] ; 

//  final number of connected peripherals
int peripheralsConnected = 0;

// array of valid name of peripherals
String validperipheralNames[7] = { "LED_1" ,"LED_2" ,"LED_3" ,"LED_4" ,"LED_5" ,"LED_6" ,"LED_7" } ;

// set of central 
void setup()
{
  Serial.begin( 9600 );
  while ( !Serial );
  BLE.begin();

  BLE.scanForUuid( BLE_UUID_LED );  // scaning for finding peripherals with the input service UUID

  int peripheralCounter = 0;  // count founded peripherals 
  unsigned long startMillis = millis();

  // Continue until either the number of repetitions or the number of peripherals reaches its maximum
  while ( millis() - startMillis < BLE_SCAN_INTERVALL && peripheralCounter < BLE_MAX_PERIPHERALS )
  {
    //Serial.println("wile is started");
    BLEDevice peripheral = BLE.available();  // return one BLEDevice( peripherals)
    //Serial.println("Serching started");
    Serial.println(BLE.available());
    if ( peripheral ) { // if peripherals exist 
      if (isValidPeripheral(peripheral.localName())) // check name of founded peripheral
     {
         if( !is_prepheral_repeated( peripheral, peripheralCounter) ){// If device was not found before

           peripherals[peripheralCounter] = peripheral; //  add peripheral to array of founded peripherals
           peripheralCounter++;  // increase founded peripherlas number 
         }
      }
    }
 // Serial.println("wile is End");
  }

  BLE.stopScan(); // stop scanning for new peripherals 
  Serial.println ("Stooooooooooooooooooooooooop scaaaaaaaaaaaaan") ;
  connect_to_peripherals ( peripheralCounter ) ;
  Serial.println("connect_to_peripherals");
}

void loop()
{
  // change the led state of all peripherals 
  for ( int i = 0; i < peripheralsConnected; i++ )
  {  
    controlLed(i ) ;
    delay(1000);
  }
}

boolean is_prepheral_repeated( BLEDevice peripheral ,int peripheralCounter ) {

    Serial.println( peripheral.localName() ) ;
    boolean peripheralAlreadyFound = false;  //boolean use for detect duplication of peripheral

    for ( int i = 0; i < peripheralCounter; i++ ) // Checks this peripheral has already been found or not 
    {
        if ( peripheral.address() == peripherals[i].address() )
        {
          peripheralAlreadyFound = true;
          break ;
        }
    }
    return peripheralAlreadyFound ; 

}

void connect_to_peripherals (int peripheralCounter ) {

  for ( int i = 0; i < peripheralCounter; i++ ) // for each founded peripherals 
  {
    Serial.println(peripherals[i].localName()) ;
    peripherals[i].connect() ; // connect to peripheral
    peripherals[i].discoverAttributes();
    BLECharacteristic ledCharacteristic = peripherals[i].characteristic(BLE_UUID_LED_CHAR); // get charactestic with input charactestic UUID
    if ( ledCharacteristic ) // if characteristic exist
    {
      ledCharacteristics[i] = ledCharacteristic; // save it in characteristic array
      ledCharacteristics[i].subscribe();
      state[i] = 0 ; // add default value for peripherals led state
    }
    else
    {
      Serial.println("no character..."); // if characteristic not exist print this
    }
  }
  peripheralsConnected = peripheralCounter;
}

// check input name is valid peripherals name
boolean isValidPeripheral( String fullname ) {
  for( int i= 0 ; i < 7 ; i++){
    if( fullname == validperipheralNames[i]) {
        return true ;
    }
  }
   return false ;
}

// change led state
void controlLed(  int index ) {
        //Serial.print( peripherals[index].localName() ) ;
        ledCharacteristics[index].writeValue((byte)state[index]); //write state value to LED of peripherals
        // toggle state
        if(state[index] == 1){
          Serial.println( " release button " ) ;
          state[index] = 0;
        }else{
           Serial.println( " press button " ) ;
          state[index] = 1;
        }
}  

peripheral:

#include 

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
//BLEService ledService("1101"); // BLE LED Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
//BLEByteCharacteristic switchCharacteristic("2101", BLERead | BLEWrite|BLENotify);

const int ledPin = LED_BUILTIN; // pin to use for the LED

void setup() {
  Serial.begin(9600);
  //while (!Serial);

  // set LED pin to output mode
  pinMode(ledPin, OUTPUT);

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");

    while (1);
  }

  // set advertised local name and service UUID:
  BLE.setLocalName("LED_3");
  BLE.setAdvertisedService(ledService);

  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);

  // add service
  BLE.addService(ledService);

  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("BLE LED Peripheral");
}

void loop() {
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();
  Serial.println("Not connected...");
  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the LED:
      if (switchCharacteristic.written()) {
        if (switchCharacteristic.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
        }
      }

    }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}
Existentialist-Robot commented 2 years ago

Any update on this? Presently, I am using four Nano BLE 33 boards and also maxing out at subscribing 2 peripherals from a central, I can connect to and discover attributes from the third peripheral, but not subscribe.

I am using ArduinoBLE library version 1.3.1 and Mbed OS Nano Board version 3.4.1