Skip to content

Commit 2500523

Browse files
authored
Merge pull request #1451 from arduino/Hannes7eicher/R4-BLE-Examples
[HWU4M-71] Add BLE example
2 parents 176a16d + b45d4e9 commit 2500523

File tree

1 file changed

+166
-0
lines changed
  • content/hardware/02.hero/boards/uno-r4-wifi/tutorials/cheat-sheet

1 file changed

+166
-0
lines changed

content/hardware/02.hero/boards/uno-r4-wifi/tutorials/cheat-sheet/cheat-sheet.md

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,172 @@ To learn more about the Wi-Fi® capabilities of the UNO R4 WiFi, try out the [Ne
622622

623623
Thanks to the ESP32 module, the UNO R4 WiFi has Bluetooth® LE and Bluetooth® 5 capabilities, at a speed of up to 2 Mbps. The ESP32 module has a built in trace-antenna, meaning that you do not need an external one to use the connectivity features of the board. However, this trace antenna is shared with the Bluetooth® module, which means that you cannot use Bluetooth® and Wi-Fi® at the same time.
624624

625+
Below is an example sketch scans for bluetooth devices:
626+
627+
```arduino
628+
#include <ArduinoBLE.h>
629+
630+
void setup() {
631+
Serial.begin(9600);
632+
while (!Serial);
633+
634+
// begin initialization
635+
if (!BLE.begin()) {
636+
Serial.println("starting Bluetooth® Low Energy module failed!");
637+
638+
while (1);
639+
}
640+
641+
Serial.println("Bluetooth® Low Energy Central - Peripheral Explorer");
642+
643+
// start scanning for peripherals
644+
BLE.scan();
645+
}
646+
647+
void loop() {
648+
// check if a peripheral has been discovered
649+
BLEDevice peripheral = BLE.available();
650+
651+
if (peripheral) {
652+
// discovered a peripheral, print out address, local name, and advertised service
653+
Serial.print("Found ");
654+
Serial.print(peripheral.address());
655+
Serial.print(" '");
656+
Serial.print(peripheral.localName());
657+
Serial.print("' ");
658+
Serial.print(peripheral.advertisedServiceUuid());
659+
Serial.println();
660+
661+
// check for peripheral's name
662+
if (peripheral.localName() == "<PERIPHERAL_NAME>") {
663+
// stop scanning
664+
BLE.stopScan();
665+
666+
explorerPeripheral(peripheral);
667+
668+
// peripheral disconnected, we are done
669+
while (1) {
670+
// do nothing
671+
}
672+
}
673+
}
674+
}
675+
676+
void explorerPeripheral(BLEDevice peripheral) {
677+
// connect to the peripheral
678+
Serial.println("Connecting ...");
679+
680+
if (peripheral.connect()) {
681+
Serial.println("Connected");
682+
} else {
683+
Serial.println("Failed to connect!");
684+
return;
685+
}
686+
687+
// discover peripheral attributes
688+
Serial.println("Discovering attributes ...");
689+
if (peripheral.discoverAttributes()) {
690+
Serial.println("Attributes discovered");
691+
} else {
692+
Serial.println("Attribute discovery failed!");
693+
peripheral.disconnect();
694+
return;
695+
}
696+
697+
// read and print device name of peripheral
698+
Serial.println();
699+
Serial.print("Device name: ");
700+
Serial.println(peripheral.deviceName());
701+
Serial.print("Appearance: 0x");
702+
Serial.println(peripheral.appearance(), HEX);
703+
Serial.println();
704+
705+
// loop the services of the peripheral and explore each
706+
for (int i = 0; i < peripheral.serviceCount(); i++) {
707+
BLEService service = peripheral.service(i);
708+
709+
exploreService(service);
710+
}
711+
712+
Serial.println();
713+
714+
// we are done exploring, disconnect
715+
Serial.println("Disconnecting ...");
716+
peripheral.disconnect();
717+
Serial.println("Disconnected");
718+
}
719+
720+
void exploreService(BLEService service) {
721+
// print the UUID of the service
722+
Serial.print("Service ");
723+
Serial.println(service.uuid());
724+
725+
// loop the characteristics of the service and explore each
726+
for (int i = 0; i < service.characteristicCount(); i++) {
727+
BLECharacteristic characteristic = service.characteristic(i);
728+
729+
exploreCharacteristic(characteristic);
730+
}
731+
}
732+
733+
void exploreCharacteristic(BLECharacteristic characteristic) {
734+
// print the UUID and properties of the characteristic
735+
Serial.print("\tCharacteristic ");
736+
Serial.print(characteristic.uuid());
737+
Serial.print(", properties 0x");
738+
Serial.print(characteristic.properties(), HEX);
739+
740+
// check if the characteristic is readable
741+
if (characteristic.canRead()) {
742+
// read the characteristic value
743+
characteristic.read();
744+
745+
if (characteristic.valueLength() > 0) {
746+
// print out the value of the characteristic
747+
Serial.print(", value 0x");
748+
printData(characteristic.value(), characteristic.valueLength());
749+
}
750+
}
751+
Serial.println();
752+
753+
// loop the descriptors of the characteristic and explore each
754+
for (int i = 0; i < characteristic.descriptorCount(); i++) {
755+
BLEDescriptor descriptor = characteristic.descriptor(i);
756+
757+
exploreDescriptor(descriptor);
758+
}
759+
}
760+
761+
void exploreDescriptor(BLEDescriptor descriptor) {
762+
// print the UUID of the descriptor
763+
Serial.print("\t\tDescriptor ");
764+
Serial.print(descriptor.uuid());
765+
766+
// read the descriptor value
767+
descriptor.read();
768+
769+
// print out the value of the descriptor
770+
Serial.print(", value 0x");
771+
printData(descriptor.value(), descriptor.valueLength());
772+
773+
Serial.println();
774+
}
775+
776+
void printData(const unsigned char data[], int length) {
777+
for (int i = 0; i < length; i++) {
778+
unsigned char b = data[i];
779+
780+
if (b < 16) {
781+
Serial.print("0");
782+
}
783+
784+
Serial.print(b, HEX);
785+
}
786+
}
787+
```
788+
789+
If you want to learn more about Bluetooth LE check out our article [here](/learn/communication/bluetooth).
790+
625791
### Programming the ESP32 (Advanced)
626792

627793
The ESP32 module and the Renesas RA4M1-chip are part of a sophisticated USB-Serial system that is highly flexible and adaptive to allow for HID features while still keeping the ability to program both the main MCU, and the ESP32, if you so wish. By default, the ESP32's is used mainly as a radio module using Wi-Fi® and Bluetooth®.

0 commit comments

Comments
 (0)