|
| 1 | +/* |
| 2 | + * This example shows how to use the Nicla Sense ME library to read the battery status and send it over BLE. |
| 3 | + * |
| 4 | + * When not connected over BLE, the battery status is printed over the serial port every 4 seconds. |
| 5 | + * When connected over BLE, the battery status is checked every 4 seconds and sent over BLE if a value has changed. |
| 6 | + * That is, when using the notification mechanism in the BatteryMonitor web app, which is the default. |
| 7 | + * The BatteryMonitor web app can also be configured to poll the battery status every X seconds. |
| 8 | + * |
| 9 | + * The LED colors are used to indicate the BLE connection status: |
| 10 | + * - Green: Board is ready but no BLE connection has been established yet. |
| 11 | + * - Blue: Board is connected over BLE to a device that wants to read the battery status. |
| 12 | + * - Red: A device that was previously connected over BLE has disconnected. |
| 13 | + * |
| 14 | + * Instructions: |
| 15 | + * 1. Upload this sketch to your Nicla Sense ME board. |
| 16 | + * 2. Open the BatteryMonitor web app (index.html) in a browser that supports Web Bluetooth (Chrome, Edge, Opera, etc.). |
| 17 | + * 3. Connect to your Nicla Sense ME board by clicking the "Connect" button. |
| 18 | + * |
| 19 | + * Initial author: Sebastian Romero @sebromero |
| 20 | + */ |
| 21 | + |
1 | 22 | #include "Nicla_System.h"
|
2 | 23 | #include <ArduinoBLE.h>
|
3 | 24 |
|
@@ -242,8 +263,10 @@ void setup()
|
242 | 263 |
|
243 | 264 | void loop()
|
244 | 265 | {
|
245 |
| - //BLE.poll(); // Implicit when calling BLE.connected(). Uncomment when only using BLERead |
| 266 | + //BLE.poll(); // Implicit when calling BLE.connected(). Uncomment when only using BLERead (polling mechanism) |
246 | 267 |
|
| 268 | + // Check if a BLE device is connected and handle battery updates |
| 269 | + // via the notification mechanism. |
247 | 270 | if (BLE.connected()) {
|
248 | 271 | bool newBatteryLevelAvailable = updateBatteryLevel();
|
249 | 272 | bool newBatteryStatusAvailable = updateBatteryStatus();
|
|
0 commit comments