I have a problem. My app crashes when I use multiple RegisterFor... UUID to receive data.
I have now already tried some debugging and can clearly trace the error back to this. I have added an extra button that adds an additional RegisterForIntegers UUID to my BLE1 module. As soon as I press this button the app crashes reliably.
I have also tried this before a device connects to the BLE module.....
Is there a bug that doesn't allow this, or are there any peculiarities when I want to add multiple Characteristics UUID?
A solution that is often suggested is that I should take only one Characteristic UUID and send everything above it as a list, this is not an acceptable solution in my case!
What Make/Model of microcontroller are you using? How many physical channels does it have to send different sets of data at different times?
Of the three Characteristic UUIDs, which is for what type of data? All three could in fact be text and that might be your best approach. Your App is only controlling LEDs?
We need to see your microcontroller script.
Also - why are you splitting the device address at spaces? The BLE extension should handle addresses as listed. Does the App successfully connect?
I'm just trying to get the maximum information from you so that any Power User can chime-in as I'm mostly away from my desk until June.
....another 'also'. Try registering the 3 characteristics as one each for 3 BLE instances (i.e. BLE1, BLE2, BLE3). Do not add any of them with a button event.
Right now it's just a dummy script for easy testing. The real logic will be much more complex. In the attachment is the script... Please take into account that I add BLE characteristics after remain. Currently I have only 2 in use.
Yes, I can connect completely, the communication in both directions works, I can receive data on the app and I can send data from the app and display it on the Arduino.
The initialization of the connection to the Bluetooth module with the spaces I have done so, because this was suggested to me so and this also works without problems.
As written, I have unfortunately already do some debugging and the error occurs only as soon as I want to add a second characteristic, even if I send over this second characteristic as in the current Arduino code nothing.
Arduino Code:
#include <ArduinoBLE.h>
BLEService ledService("0000FFE0-0000-1000-8000-00805F9B34FB"); // BLE LED Service
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEIntCharacteristic switchCharacteristic("0000FFE1-0000-1000-8000-00805F9B34FB", BLERead | BLEWrite);
// Rack Position Characteristic
BLEIntCharacteristic RackPosition("0000FFE2-0000-1000-8000-00805F9B34FB", BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes
const int ledPin = LED_BUILTIN; // pin to use for the LED
int i;
int number;
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");
BLE.setAdvertisedService(ledService);
// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);
ledService.addCharacteristic(RackPosition);
// 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();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
number = 0;
// while the central is still connected to peripheral:
while (central.connected()) {
if(number<100000){
number = number+1;
}
else{
i = int(random(0, 3));
RackPosition.writeValue(i); // send 1 values
number = 0 ;
Serial.println(i);
}
// 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(switchCharacteristic.value());
digitalWrite(ledPin, HIGH); // will turn the LED on
} else { // a 0 value
Serial.println("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());
}
}
}
So i should Use different BLE Modules with the same Service UUID and an Single characteristic UUID?
Isn't it then the case that I can only connect to one BLE module at a time?
Then I can only use the current Characteristic UUID of the currently connected BLE module? Unfortunately this would not help me. That means I would have to connect to new BLE modules all the time, so I would have to connect between BLE1, BLE2 and BLE3 all the time.
Why can't I add a characteristic by button? What should speak against it?
The crash didn't tell you it was wrong? All functions need to be used in a timely manner, in order. There is no need to try to add a characteristic manually via a button.
I guess it is relevant for the app that all Characteristic UUID that are initialized, these are known from the beginning also in the transmitter.
In the Arduino code I wanted to add them only when I need them, but this is not allowed when using MIT App Inventor 2.
The following code now works and does not crash the app.
Arduino Code for Nano 33 IoT
#include <ArduinoBLE.h>
BLEService ledService("0000FFE0-0000-1000-8000-00805F9B34FB"); // BLE LED Service
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEIntCharacteristic switchCharacteristic("0000FFE1-0000-1000-8000-00805F9B34FB", BLERead | BLEWrite);
// Rack Position Characteristic
BLEIntCharacteristic RackPosition("0000FFE2-0000-1000-8000-00805F9B34FB", BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes
// Rack Position Characteristic
BLEIntCharacteristic RackPosition2("0000FFE3-0000-1000-8000-00805F9B34FB", BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes
const int ledPin = LED_BUILTIN; // pin to use for the LED
int i;
int number;
int number2;
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");
BLE.setAdvertisedService(ledService);
// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);
ledService.addCharacteristic(RackPosition);
ledService.addCharacteristic(RackPosition2);
// 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();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
number = 0;
// while the central is still connected to peripheral:
while (central.connected()) {
if(number<50000){
number = number+1;
}
else{
i = int(random(0, 3));
RackPosition.writeValue(i); // send 1 values
number = 0 ;
Serial.println(i);
}
if(number2<10000){
number2 = number2+1;
}
else{
i = int(random(0, 3));
RackPosition2.writeValue(i); // send 1 values
number2 = 0 ;
Serial.println(i);
RackPosition2.writeValue(i); // send 1 values
}
// 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(switchCharacteristic.value());
digitalWrite(ledPin, HIGH); // will turn the LED on
} else { // a 0 value
Serial.println("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());
}
}
Okay, but this wasn't the problem. I Found the problem .... and right now i am dooing this, so it's no problem to do this... if its needet you can do it.