Skip to content

Commit 406a042

Browse files
committed
ArduinoBLE Added
1 parent fd293a6 commit 406a042

File tree

3 files changed

+131
-2
lines changed

3 files changed

+131
-2
lines changed
812 KB
Loading
379 KB
Loading

content/hardware/03.nano/boards/nano-matter/tutorials/user-manual/content.md

Lines changed: 131 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -825,11 +825,13 @@ With this done, Home Assistant should be forwarding the Nano Matter sensor data
825825

826826
## Bluetooth® Low Energy
827827

828+
### Silicon Labs BLE
829+
828830
To enable Bluetooth® Low Energy communication on the Nano Matter, you must enable the "BLE" protocol stack in the Arduino IDE board configurations.
829831

830832
In the upper menu, navigate to **Tools > Protocol stack** and select **BLE(Silabs)**.
831833

832-
![Enable "BLE" Protocol Stack](assets/ble-setup-2.png)
834+
![Enable "BLE(Silabs)" Protocol Stack](assets/ble-setup-2.png)
833835

834836
For this Bluetooth® Low Energy application example, we are going to control the Nano Matter built-in LED and read the onboard button status. The example sketch to be used can be found in **File > Examples > Silicon Labs > ble_blinky**:
835837

@@ -1176,7 +1178,134 @@ Open the **Simplicity Connect** app on your smartphone, in the lower menu, navig
11761178

11771179
![Blinky demo controlling the Nano Matter via Bluetooth® Low Energy](assets/ble-blinky.gif)
11781180

1179-
***You can also manage the LED control and button status manually from the Scan tab in the lower menu.***
1181+
### Arduino BLE
1182+
1183+
Now let's do the same but using the `ArduinoBLE` library.
1184+
1185+
In the upper menu, navigate to **Tools > Protocol stack** and select **BLE(Arduino)**.
1186+
1187+
![Enable "BLE(Arduino)" Protocol Stack](assets/ble-setup-3.png)
1188+
1189+
For this Bluetooth® Low Energy application example, we are going to control the Nano Matter built-in LED and read the onboard button status, everything from the **Simplicity Connect** app.
1190+
1191+
```arduino
1192+
#include <ArduinoBLE.h>
1193+
1194+
BLEService ledService("de8a5aac-a99b-c315-0c80-60d4cbb51224"); // Bluetooth® Low Energy LED Service
1195+
1196+
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
1197+
BLEByteCharacteristic ledCharacteristic("5b026510-4088-c297-46d8-be6c736a087a", BLERead | BLEWrite);
1198+
1199+
// Bluetooth® Low Energy Push Button Characteristic - custom 128-bit UUID, read and notify by central
1200+
BLEByteCharacteristic switchCharacteristic("61a885a4-41c3-60d0-9a53-6d652a70d29c", BLERead | BLENotify);
1201+
1202+
const int ledPin = LED_BUILTIN; // pin to use for the LED
1203+
volatile bool btn_state_changed = false;
1204+
volatile uint8_t btn_state = LOW;
1205+
1206+
void setup() {
1207+
Serial.begin(9600);
1208+
while (!Serial)
1209+
;
1210+
1211+
// set LED pin to output mode
1212+
pinMode(ledPin, OUTPUT);
1213+
digitalWrite(ledPin, HIGH);
1214+
1215+
pinMode(BTN_BUILTIN, INPUT_PULLUP);
1216+
attachInterrupt(BTN_BUILTIN, &btn_handle, CHANGE);
1217+
1218+
// begin initialization
1219+
if (!BLE.begin()) {
1220+
Serial.println("starting Bluetooth® Low Energy module failed!");
1221+
1222+
while (1)
1223+
;
1224+
}
1225+
1226+
// set advertised local name and service UUID:
1227+
BLE.setLocalName("Blinky Example");
1228+
BLE.setAdvertisedService(ledService);
1229+
1230+
// add the characteristic to the service
1231+
ledService.addCharacteristic(ledCharacteristic);
1232+
ledService.addCharacteristic(switchCharacteristic);
1233+
1234+
// add service
1235+
BLE.addService(ledService);
1236+
1237+
// set the initial value for the characeristic:
1238+
ledCharacteristic.writeValue(0);
1239+
switchCharacteristic.writeValue(0);
1240+
1241+
// start advertising
1242+
BLE.advertise();
1243+
1244+
Serial.println("BLE LED Peripheral");
1245+
}
1246+
1247+
void loop() {
1248+
// listen for Bluetooth® Low Energy peripherals to connect:
1249+
BLEDevice central = BLE.central();
1250+
1251+
// if a central is connected to peripheral:
1252+
if (central) {
1253+
Serial.print("Connected to central: ");
1254+
// print the central's MAC address:
1255+
Serial.println(central.address());
1256+
1257+
// while the central is still connected to peripheral:
1258+
while (central.connected()) {
1259+
// if the remote device wrote to the characteristic,
1260+
// use the value to control the LED:
1261+
if (ledCharacteristic.written()) {
1262+
if (ledCharacteristic.value()) { // any value other than 0
1263+
Serial.println("LED on");
1264+
digitalWrite(ledPin, LOW); // will turn the LED on
1265+
} else { // a 0 value
1266+
Serial.println(F("LED off"));
1267+
digitalWrite(ledPin, HIGH); // will turn the LED off
1268+
}
1269+
}
1270+
1271+
if (btn_state_changed) {
1272+
// button state changed, update characteristics
1273+
btn_state_changed = false;
1274+
Serial.println("Button toggle");
1275+
switchCharacteristic.writeValue(btn_state);
1276+
}
1277+
}
1278+
1279+
// when the central disconnects, print it out:
1280+
Serial.print(F("Disconnected from central: "));
1281+
Serial.println(central.address());
1282+
}
1283+
}
1284+
1285+
/**************************************************************************/ /**
1286+
* Called on button state change - stores the current button state and
1287+
* sets a flag that a button state change occurred.
1288+
*****************************************************************************/
1289+
static void btn_handle() {
1290+
// The real button state is inverted - most boards have an active low button configuration
1291+
btn_state = !digitalRead(BTN_BUILTIN);
1292+
btn_state_changed = true;
1293+
}
1294+
```
1295+
1296+
As you can see, using the `ArduinoBLE` library makes everything easier and cleaner. We end up with a simple `setup()` and `loop()` sketch.
1297+
1298+
- In the `setup()` function the board outputs and inputs are set and configured alongside the BLE service and characteristics.
1299+
- In the `loop()` function we continuously ask if the peripheral is properly connected to a central and then start notifying the push button status and retrieving the app LED status.
1300+
1301+
After uploading the sketch to the Nano Matter, it is time to communicate with it via Bluetooth® Low Energy. For this, Silicon Labs has developed a **mobile app** that you can download from here:
1302+
1303+
- [Android](https://play.google.com/store/apps/details?id=com.siliconlabs.bledemo)
1304+
- [iOS](https://itunes.apple.com/us/app/silicon-labs-blue-gecko-wstk/id1030932759)
1305+
1306+
Open the **Simplicity Connect** app on your smartphone, in the lower menu, navigate to **Demo** and select **Blinky**:
1307+
1308+
![Blinky demo controlling the Nano Matter via Bluetooth® Low Energy](assets/ble-blinky-3.gif)
11801309

11811310
## Onboard User Interface
11821311

0 commit comments

Comments
 (0)