Skip to content

Commit b45d4e9

Browse files
committed
add BLE example
1 parent 7fe0a9b commit b45d4e9

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
@@ -610,6 +610,172 @@ To learn more about the Wi-Fi® capabilities of the UNO R4 WiFi, try out the [Ne
610610

611611
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.
612612

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

615781
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)