Hello everybody.
I recently bought a Genuino 101 board (just before the end-of-life anouncement...) and I had fun using it for BLE data collection (as a BLE client) for one sensor node.
I wanted to try to connect to multiple sensor nodes (Sensortag or Sensortiles) but I have some problems I can't find the origin.
To be exact, I try to connect to multiple nodes who are identic, excepted for MAC adress (I checked it with the Android App BLE Scanner).
Here is my code (the part who interest me).
BLEDevice connecteddevices[5]; //I create an array of connected devices
int nbrconnect = 0; //number of currently connected devices
extern void subscribe_IMU(BLEDevice);
extern void read_IMU(BLEDevice);
enum state {
initial,
decouverte,
choix,
acquisition
};
state etat = initial;
// Global structure to hold the sensor data
struct DATA {
float accx;
float accy;
float accz;
};
typedef struct DATA DATA;
DATA SensorData;
//period defines the length of time between measurements in milliseconds
//long period=600000L; // 10 minutes
//long period = 30000L; // .5 minute for testing
long period = 100L; //
BLEDevice peripheral;
BLECharacteristic ConCharacteristic;
BLECharacteristic ValCharacteristic;
BLECharacteristic ConCharacteristic2; //Since I want to connect to 2 sensors, I switch when I detect the second one
BLECharacteristic ValCharacteristic2;
//BLECharacteristic IMUConCharacteristic[5]; //Another try with array of characteristics
//BLECharacteristic IMUValCharacteristic[5];
//BLEDeviceEventHandler youkou;
void setup() {
Serial.begin(9600);
while (!Serial); //Waiting for a serial line (debug)
delay(500);
Serial.println(" *** SensorTile (v2.0) ***");
Serial.println();
// initialize the BLE hardware
BLE.begin();
delay(500);
BLE.scan(false);
delay(5000); //taking time to detect nodes
}
void loop() {
long lastMillis = 0; // for period test
long nowMillis = 0; // for period test
// check if a peripheral has been discovered
//peripheral = BLE.available();
lastMillis = millis();
Serial.println(lastMillis);
//Loop for multiple sensors
while (nowMillis < 12000) { //La boucle de détection dure 12 secondes
delay(50);
peripheral = BLE.available();
Serial.println("objet BLE disponible?");
if (peripheral) {
//Serial.println("OUI");
if (peripheral.localName() == "BM2V220") { //Sensortile generic name
Serial.println(peripheral.address()); //printing MAC
Serial.print("Connection au SensorTile ...");
if (peripheral.connect()) { //if we can connect
connecteddevices[nbrconnect] = peripheral; //add the device to the array of connected devices
Serial.println("Connected");
do_discovery(connecteddevices[nbrconnect]);
subscribe_IMU(connecteddevices[nbrconnect]);
if (peripheral.connected()){
nbrconnect++; //if properly subscribed, increment
}
delay(200);
nowMillis = millis() - lastMillis;
Serial.println(nowMillis);
BLE.scan();
} else {
Serial.println("Connection échouée!");
}
}
}
else {
nowMillis = millis() - lastMillis; //temps actuel - temps du début de boucle
}
}
BLE.stopScan();
Serial.println("on coupe le scan et on interroge");
}
// discover the peripheral's attributes that we want
void do_discovery(BLEDevice peripherals) {
// discovery of attributes from IMU
Serial.print("Discovering attributes for IMU service ...");
if (peripherals.discoverAttributesByService("00000000-0001-11e1-9AB4-0002a5d5c51b")) { //changé pour ST
Serial.println("discovered");
ConCharacteristic = peripherals.characteristic("F000AA82-0451-4000-B000-000000000000"); //configuration (pas encore changé)
ValCharacteristic = peripherals.characteristic("00e00000-0001-11e1-ac36-0002a5d5c51b"); //valeurs chez ST 00e00000-0001-11e1-ac36-0002a5d5c51b
}
else {
Serial.println("Error: IMU service discovery failed.");
//peripherals.disconnect();
return;
}
}
void subscribe_IMU(BLEDevice peripherals) {
// subscribe to the IMU axl service
Serial.print("Subscribing to IMU data service ...");
if (!ValCharacteristic) {
Serial.println("Error: IMU data service characteristic not found!");
peripherals.disconnect();
return;
} else if (!ValCharacteristic.canSubscribe()) {
Serial.println("Error: IMU data service characteristic not subscribable!");
peripherals.disconnect();
return;
} else if (!ValCharacteristic.subscribe()) {
Serial.println("Error: subscription failed!");
peripherals.disconnect();
return;
} else {
Serial.println("Subscribed to IMU data service characteristic");
delay(500);
}
}
My problem is: for the second connected node, in the function do_discovery, when the discoveratributesbyservice fail. I don't understand why.
Thank you for reading.