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.
#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());
}
}