Description
Hi,
I'm currently trying to connect 4 Arduino MKR Wifi 1010 (that will acts as peripheral devices) to a single Arduino Nano 33 IoT (as central).
Until 3 devices, everything works smoothly, at 4 it looks like the BLE module of the central stops working... the peripherals disconnects and the central play dead.
I looked at the various treads here, and tried to implement the various suggestion but it is still not working...
In particular I:
- Saw the comment about modifying the ATT_MAX_PEERS in the ATT.h file, but after updating the ArduinoBLE to the latest version I saw that this change was implemented. My file looks like that in Adjust ATT_MAX_PEERS definition and increase max number of BLE connections for MKR WiFi 1010 #131
- Saw the comment about modifying the nina firmware to increase the max number of connection, as shown by @giulcioffi. For this, what I did was update the Wifinina firmware using the procedure shown here. I now have installed the version 1.4.5, which has the sdkconfig file modified with the MAX_CONN increased to 7.
- Tried to implement the multi-connection modification by @polldo shown here, but still no luck.
Few remarks:
- I updated only the NINA firmware of the central device, not that of the peripherals as I think it should not matter.
- The suggestion shown here Allow more than 3 concurrent peripheral connections in central mode #116 looks to me that are already implemented in the version 1.4.5 of the Firmware that I have uploaded. Is this correct?
What am I missing? I also tried to use an MKR as central and the 3 MKR + 1 Nano as peripherals, but as expected no changes.
Below you can find a simplification of my code for the central and the peripheral (other peripherals are equal but with the name changed). With 3 devices it works fine, they receive data, disconnect and reconnect without any issues. As soon as the 4th one connects, the code crashes quite soon.
Central:
#include <ArduinoBLE.h>
//Defining time interval and number of devices
#define BLE_MAX_PERIPHERALS 4
#define BLE_SCAN_INTERVALL 10000
#define BLE_SCAN_new_devices 10000
// BLE variables
BLEDevice peripherals[BLE_MAX_PERIPHERALS];
BLECharacteristic positionCharacteristics[BLE_MAX_PERIPHERALS];
// variables
uint16_t position[BLE_MAX_PERIPHERALS];
bool peripheralsConnected[BLE_MAX_PERIPHERALS] = { 0 };
bool peripheralsToConnect[BLE_MAX_PERIPHERALS] = { 0 };
bool ok = true;
int peripheralCounter = 0;
//Time variable
unsigned long control_time;
void setup() {
Serial.begin(9600);
while (!Serial);
// initialize the BLE hardware
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
Serial.println("BLE started");
peripheralCounter = 0;
}
void loop() {
// start scanning for peripherals
BLE.scanForUuid("19B10000-E8F2-537E-4F6C-D104768A1214");
Serial.println("Scan ongoing");
unsigned long startMillis = millis();
//Until timeout or max devices found
while ( millis() - startMillis < BLE_SCAN_INTERVALL && peripheralCounter < BLE_MAX_PERIPHERALS ) {
BLEDevice peripheral = BLE.available();
if ( peripheral ) {
//If device has name of interest, is not already in the list to be connected or in the list of connected devices
if ( peripheral.localName() == "Device_0" && !peripheralsToConnect[0] && !peripheralsConnected[0]) {
peripherals[0] = peripheral;
peripheralCounter++;
peripheralsToConnect[0]=true;
}
if ( peripheral.localName() == "Device_1" && !peripheralsToConnect[1] && !peripheralsConnected[1]) {
peripherals[1] = peripheral;
peripheralCounter++;
peripheralsToConnect[1]=true;
}
if ( peripheral.localName() == "Device_2" && !peripheralsToConnect[2] && !peripheralsConnected[2]) {
peripherals[2] = peripheral;
peripheralCounter++;
peripheralsToConnect[2]=true;
}
if ( peripheral.localName() == "Device_3" && !peripheralsToConnect[3] && !peripheralsConnected[3]) {
peripherals[3] = peripheral;
peripheralCounter++;
peripheralsToConnect[3]=true;
}
}
}
Serial.print("Device found: ");
Serial.println(peripheralCounter);
BLE.stopScan();
//Connecting to all devices found which are not already connected
for ( int i = 0; i < BLE_MAX_PERIPHERALS; i++ ) {
if(peripheralsToConnect[i]){
peripherals[i].connect();
peripherals[i].discoverAttributes();
BLECharacteristic positionCharacteristic = peripherals[i].characteristic("19B10001-E8F2-537E-4F6C-D104768A1214");
if ( positionCharacteristic ){
positionCharacteristics[i] = positionCharacteristic;
positionCharacteristics[i].subscribe();
}
peripheralsConnected[i]=true;
peripheralsToConnect[i]=false;
}
}
control_time=millis();
ok=true;
while (ok) {
if(peripheralCounter < BLE_MAX_PERIPHERALS) {
//if not all devices connected, redo search after BLE_SCAN_new_devices time
if(millis()-control_time>BLE_SCAN_new_devices) {
ok=false;
Serial.println("Looking for other devices");
}
}
for ( int i = 0; i < BLE_MAX_PERIPHERALS; i++ ) {
//If the i_th device is supposed to be connected
if(peripheralsConnected[i]) {
//Check if it disconnected in the meantime
if(!peripherals[i].connected()) {
ok=false;
peripheralsConnected[i]=false;
peripheralCounter--;
Serial.print("Device ");
Serial.print(i);
Serial.println(" disconnected.");
}
else {
//if it did not disconnect, check for updates in position characteristic
if (positionCharacteristics[i].valueUpdated()) {
positionCharacteristics[i].readValue(position[i]);
Serial.print("Posizione_");
Serial.print(i);
Serial.print(": ");
Serial.println(position[i]);
}
}
}
}
}
//Something disconnected or we reached limit time, redo scan to reconnect to devices
}
Peripherals:
#include <ArduinoBLE.h>
BLEService device_service("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE Service
// BLE position Characteristic - custom 128-bit UUID, read and notify.
BLEUnsignedIntCharacteristic positionCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);
// pin to use for the built-in LED
const int ledPin = LED_BUILTIN;
// Variables
int16_t pos_giunto =0;
int i;
//Time variables
unsigned long previous_timer;
unsigned long timer;
void setup() {
// set LED pin to output mode
pinMode(ledPin, OUTPUT);
// begin initialization
if (!BLE.begin()) {
while (1){
//If BLE fails to start, lonk blink
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
}
else {
digitalWrite(ledPin, HIGH);
}
//set connection interval to be the fastest possible (every 7.5ms)
BLE.setConnectionInterval(6, 6);
// set advertised local name and service UUID:
BLE.setLocalName("Device_0");
BLE.setAdvertisedService(device_service);
// add the characteristic to the service
device_service.addCharacteristic(positionCharacteristic);
// add service
BLE.addService(device_service);
// set the initial value for the characeristic:
positionCharacteristic.writeValue((uint16_t)0xAAAA);
// start advertising
BLE.advertise();
//timer for sending new data every 7.5ms
timer=7500;
}
void loop() {
// listen for BLE peripherals to connect:
// Fast blink when looking for central
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(100);
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
//Fixed on led when connected
digitalWrite(ledPin, HIGH);
//Inizialization
previous_timer=micros();
pos_giunto=0;
// while the central is still connected to peripheral:
while (central.connected()) {
//Increase position variable of 1 every 7.5ms and send over bluetooth
//At 2500 restart from 0;
if (pos_giunto==2500) {
pos_giunto=0;
}
if(micros() - previous_timer >= timer){
pos_giunto++;
positionCharacteristic.writeValue(pos_giunto);
previous_timer = micros();
}
}
//Central disconnected, turn off led
digitalWrite(ledPin, LOW);
}
}
Thank you in advance for your suggestions and help.