Hello ,Everyone.
I'm very new to BLE and started studying with the example sketches from the Arduino BLE library.
I used the items below to test the transmission time.
・2 Arduino Nano33 BLE
・Central: LedControl(modified)
Peripheral: LED(modified)
・distance:<10cm
Central code:
#include <ArduinoBLE.h>
unsigned long now;
// variables for button
const int buttonPin = 2;
int oldButtonState = LOW;
int buttonState;
void setup() {
buttonState=1;
Serial.begin(9600);
while (!Serial);
// configure the button pin as input
pinMode(buttonPin, INPUT);
// initialize the Bluetooth® Low Energy hardware
BLE.begin();
//set connection interval
BLE.setConnectionInterval(0x0006, 0x0c80); // 7.5 ms minimum, 4 s maximum=0x0c80
Serial.println("Bluetooth® Low Energy Central - LED control");
// start scanning for peripherals
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
}
void loop() {
// check if a peripheral has been discovered
BLEDevice peripheral = BLE.available();
if (peripheral) {
// discovered a peripheral, print out address, local name, and advertised service
Serial.print("Found ");
Serial.print(peripheral.address());
Serial.print(" '");
Serial.print(peripheral.localName());
Serial.print("' ");
Serial.print(peripheral.advertisedServiceUuid());
Serial.println();
if (peripheral.localName() != "LED") {
return;
}
// stop scanning
BLE.stopScan();
controlLed(peripheral);
// peripheral disconnected, start scanning again
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
}
}
void controlLed(BLEDevice peripheral) {
// connect to the peripheral
Serial.println("Connecting ...");
if (peripheral.connect()) {
Serial.println("Connected");
} else {
Serial.println("Failed to connect!");
return;
}
// discover peripheral attributes
Serial.println("Discovering attributes ...");
if (peripheral.discoverAttributes()) {
Serial.println("Attributes discovered");
} else {
Serial.println("Attribute discovery failed!");
peripheral.disconnect();
return;
}
// retrieve the LED characteristic
BLECharacteristic ledCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");
if (!ledCharacteristic) {
Serial.println("Peripheral does not have LED characteristic!");
peripheral.disconnect();
return;
} else if (!ledCharacteristic.canWrite()) {
Serial.println("Peripheral does not have a writable LED characteristic!");
peripheral.disconnect();
return;
}
while (peripheral.connected()) {
// while the peripheral is connected
// read the button pin
//int buttonState = digitalRead(buttonPin);
if (oldButtonState != buttonState) {
// button changed
oldButtonState = buttonState;
if (buttonState) {
Serial.println("button pressed");
// button is pressed, write 0x01 to turn the LED on
now=micros();
ledCharacteristic.writeValue((byte)0x01);
Serial.println(micros()-now);
} else {
Serial.println("button released");
// button is released, write 0x00 to turn the LED off
now=micros();
ledCharacteristic.writeValue((byte)0x00);
Serial.println(micros()-now);
}
}
delay(1000);
if (buttonState==1){
buttonState=0;
}else if(buttonState==0){
buttonState=1;
}
}
Serial.println("Peripheral disconnected");
}
Peripheral:
#include <ArduinoBLE.h>
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
const int ledPin = LED_BUILTIN; // pin to use for the LED
void setup() {
Serial.begin(9600);
while (!Serial);
// set LED pin to output mode
pinMode(ledPin, OUTPUT);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
// set advertised local name and service UUID:
BLE.setLocalName("LED");
BLE.setAdvertisedService(ledService);
//add connection interval
BLE.setConnectionInterval(0x0006, 0x0c80); // 7.5 ms minimum, 4 s maximum=0x0c80
// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);
// 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 Bluetooth® Low Energy 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());
// while the central is still connected to peripheral:
while (central.connected()) {
// 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("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
}
}
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}
The result is here.
Bluetooth® Low Energy Central - LED control
Found b2:1d:95:b2:c2:0b 'LED' 19b10000-e8f2-537e-4f6c-d104768a1214
Connecting ...
Connected
Discovering attributes ...
Attributes discovered
button pressed
32428
button released
25867
button pressed
19854
button released
17085
button pressed
22027
button released
29878
button pressed
29876
button released
29867
button pressed
29865
button released
29838
button pressed
29860
button released
21008
button pressed
25803
button released
29868
button pressed
24067
button released
29866
button pressed
21022
button released
16051
button pressed
32070
button released
29864
button pressed
29865
button released
29859
button pressed
29869
button released
29046
button pressed
19871
button released
19845
button pressed
36847
button released
29867
button pressed
25082
button released
19859
button pressed
29863
button released
29850
button pressed
17126
button released
21006
button pressed
19873
button released
29856
button pressed
19869
button released
19147
button pressed
25016
button released
29765
button pressed
1462098
Peripheral disconnected
It costs almost 20ms~30ms to transmit 2 bytes of data.
I'm wondering is it normal?
I meant I thought it was longer than I calculated.
What I have learned is the data throughput is influenced by several connection parameters and environment connection conditions.
Since I found that the Arduino BLE library supports Bluetooth® 4.0, I guess I should use the parameters below.
MTU=23byte
Data rate=1Mbps
connection interval=7.5ms (min)
Total transmission time will be ① or ② or ③.
①Packet(header23byte+ATT payload 2byte)8bit 184us)+IFS 150us+ACK 80us+IFS 150us=564us
②Packet(41bytes8bit 328us)+IFS 150us+ACK 80us+IFS 150us=708us
③1 connection interval=7.5ms
Obviously the result(20~30ms) is 4 x or more than ①②③.
I know there should be some time for MCU to process the command.
But it is still longer than I expected.
Are there any mistakes in my code?
Is the understanding for calculation wrong?
Is it the actual speed of Arduino Nano33 BLE?
Please guide me if I have something wrong.
Thanks a lot for reading this topic.