Skip to content

[nano33ble] Multiple concurrent connections #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
polldo opened this issue Aug 17, 2020 · 2 comments
Open

[nano33ble] Multiple concurrent connections #108

polldo opened this issue Aug 17, 2020 · 2 comments
Labels
help wanted Assistance from the community is especially welcome type: imperfection Perceived defect in any part of project

Comments

@polldo
Copy link
Contributor

polldo commented Aug 17, 2020

ArduinoBLE library supports up to ATT_MAX_PEERS multiple concurrent connections.

#if defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_PORTENTA_H7_M7)
#define ATT_MAX_PEERS 7
#elif DM_CONN_MAX
#define ATT_MAX_PEERS DM_CONN_MAX // Mbed + Cordio
#else
#define ATT_MAX_PEERS 3
#endif

However, using the latest versions of the nano33ble core there are problems when trying to handle multiple connections.

SETUP:
ArduinoBLE version: 1.1.3-10-g89fd740 89fd740 , in branch 'multi-connection' https://github.com/arduino-libraries/ArduinoBLE/tree/multi-connection

Test sketches

  • Central, connecting to more peripherals:
#include <ArduinoBLE.h>

void setup() {
  Serial.begin(115200);
  while(!Serial);
  BLE.begin();
  BLE.debug(Serial);
  BLE.scanForName("peripheral");
}

void connectionLoop() 
{
  // If not already connected to 2 peripherals, try to connect to a new found peripheral.
  if (BLE.peripheralCount() < 2) {

    BLEDevice peripheral = BLE.available();
    if (peripheral) {
      BLE.stopScan();

      if (!peripheral.connect()) {
        Serial.println("Failed to connect!");
        return;
      }

      if (!peripheral.discoverAttributes()) {
        Serial.println("Attribute discovery failed!");
        peripheral.disconnect();
        return;
      }
    }
  }
}

auto timeRef = millis();
void loop() {
  connectionLoop();

  if (millis() - timeRef >= 5000) {
    timeRef = millis();

    int periphCount = BLE.peripheralCount();
    Serial.print(" Peripheral connected: ");
    Serial.println(periphCount);

    if (periphCount < 2) {
      BLE.scanForName("peripheral");
    }

    // Loop through all connected peripherals
    for (int periphIdx = 0; periphIdx < periphCount; periphIdx++) {
      BLEDevice peripheral = BLE.peripheral(periphIdx);
      if (peripheral) {
        BLECharacteristic batteryLevelChar = peripheral.characteristic("2A19");

        if (!batteryLevelChar) {
          Serial.println("Peripheral does not have battery level characteristic!");
          peripheral.disconnect();
        } else {
          Serial.print("Peripheral connected, value: ");
          batteryLevelChar.read();
          Serial.println(*batteryLevelChar.value());
        }
      }
    }
  }
}
  • Peripherals:
#include <ArduinoBLE.h>

BLEService batteryService("180F");
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", BLERead | BLEWrite); 

void setup() {
  Serial.begin(115200);    
  pinMode(LED_BUILTIN, OUTPUT); 
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1);
  }

  // Configure peripheral
  BLE.setLocalName("peripheral");
  BLE.setAdvertisedService(batteryService); 
  batteryService.addCharacteristic(batteryLevelChar); 
  BLE.addService(batteryService);
  BLE.advertise();

  Serial.println("peripheral configured - central connected: 0");
}

auto timeRef = millis();
void loop() {
  static uint8_t battery = 0;
  BLE.poll();
  if (millis() - timeRef >= 3000) {
    timeRef = millis();
    if (BLE.centralCount() > 0) {
      digitalWrite(LED_BUILTIN, HIGH);
      batteryLevelChar.writeValue(battery);
      battery++;
    } else {
      digitalWrite(LED_BUILTIN, LOW);
    }
  }
}
@polldo
Copy link
Contributor Author

polldo commented Sep 7, 2020

The problem was introduced here arduino/ArduinoCore-nRF528x-mbedos@01adfdc , by setting the WSF_MS_PER_TICK to 10.
I created a new branch in my fork of the nRF core, here https://github.com/Polldo/ArduinoCore-nRF528x-mbedos/commits/ble-multiconnection , that follows the core version 1.1.6. It reverts the problematic commit and adds another fix for enabling multiple connections.
It seems to work, any help in testing and reporting feedback is very much appreciated.

@iactiva
Copy link

iactiva commented May 13, 2021

There seems branch 'multi-connection' is no longer working with Arduino Mbed boards v.2.0.0, both deprecated and Nano versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Assistance from the community is especially welcome type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

2 participants