|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2015 Intel Corporation. All rights reserved. |
3 |
| - * |
4 |
| - * This library is free software; you can redistribute it and/or |
5 |
| - * modify it under the terms of the GNU Lesser General Public |
6 |
| - * License as published by the Free Software Foundation; either |
7 |
| - * version 2.1 of the License, or (at your option) any later version. |
8 |
| -
|
9 |
| - * This library is distributed in the hope that it will be useful, |
10 |
| - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
| - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
| - * Lesser General Public License for more details. |
13 |
| -
|
14 |
| - * You should have received a copy of the GNU Lesser General Public |
15 |
| - * License along with this library; if not, write to the Free Software |
16 |
| - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 |
| - */ |
| 2 | + Copyright (c) 2015 Intel Corporation. All rights reserved. |
| 3 | +
|
| 4 | + This library is free software; you can redistribute it and/or |
| 5 | + modify it under the terms of the GNU Lesser General Public |
| 6 | + License as published by the Free Software Foundation; either |
| 7 | + version 2.1 of the License, or (at your option) any later version. |
| 8 | +
|
| 9 | + This library is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + Lesser General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU Lesser General Public |
| 15 | + License along with this library; if not, write to the Free Software |
| 16 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | +*/ |
18 | 18 | #include <CurieBle.h>
|
19 | 19 |
|
20 | 20 | /*
|
21 |
| - * This sketch example partially implements the standard Bluetooth Low-Energy "Battery" service. |
22 |
| - * For more information: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx |
23 |
| - */ |
| 21 | + This sketch example partially implements the standard Bluetooth Low-Energy Battery service. |
| 22 | + For more information: https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx |
| 23 | +*/ |
24 | 24 |
|
25 |
| -/* BLE Peripheral Device (this Intel Curie device) */ |
26 |
| -BLEPeripheral blePeripheral; |
| 25 | +/* */ |
| 26 | +BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming) |
| 27 | +BLEService batteryService("180F"); // BLE Battery Service |
27 | 28 |
|
28 |
| -/* BLE Battery Service */ |
29 |
| -BLEService battSvc("180F"); |
| 29 | +// BLE Battery Level Characteristic" |
| 30 | +BLEUnsignedCharCharacteristic batteryLevelChar("2A19", // standard 16-bit characteristic UUID |
| 31 | + BLERead | BLENotify); // remote clients will be able to |
| 32 | +// get notifications if this characteristic changes |
30 | 33 |
|
31 |
| -/* BLE Battery Level Characteristic */ |
32 |
| -BLEUnsignedCharCharacteristic battLvlChar("2A19", /* standard 16-bit characteristic UUID */ |
33 |
| - BLERead | BLENotify /* remote clients will be able to get notifications if this characteristic changes */ |
34 |
| - ); |
35 |
| - |
36 |
| -/* Variable to keep track of last battery level reading from analog input */ |
37 |
| -uint8_t oldBattLvl = 0; |
38 |
| -unsigned long previousMillis = 0; |
| 34 | +int oldBatteryLevel = 0; // last battery level reading from analog input |
| 35 | +long previousMillis = 0; // last time the battery level was checked, in ms |
39 | 36 |
|
40 | 37 | void setup() {
|
41 |
| - Serial.begin(9600); |
42 |
| - |
43 |
| - pinMode(13, OUTPUT); |
| 38 | + Serial.begin(9600); // initialize serial communication |
| 39 | + pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected |
44 | 40 |
|
45 | 41 | /* Set a name for the BLE device
|
46 |
| - * We give it an arbitrary name which will appear in advertising packets |
47 |
| - * and can be used by remote peers to identify this BLE device |
48 |
| - * The name can be changed but must not exceed 20 characters in length */ |
49 |
| - blePeripheral.setLocalName("AE_BATTMON"); |
50 |
| - blePeripheral.setAdvertisedServiceUuid(battSvc.uuid()); |
51 |
| - |
52 |
| - /* Add the BLE Battery service, and include the UUID in BLE advertising data */ |
53 |
| - blePeripheral.addAttribute(battSvc); |
54 |
| - |
55 |
| - /* This service will have just one characteristic that reflects the current |
56 |
| - * percentage-charge level of the "battery" */ |
57 |
| - blePeripheral.addAttribute(battLvlChar); |
58 |
| - |
59 |
| - /* Set an initial value for this characteristic; refreshed later the loop() function */ |
60 |
| - battLvlChar.setValue(oldBattLvl); |
| 42 | + This name will appear in advertising packets |
| 43 | + and can be used by remote devices to identify this BLE device |
| 44 | + The name can be changed but must not exceed 20 characters in length */ |
| 45 | + blePeripheral.setLocalName("BatteryMonitorSketch"); |
| 46 | + blePeripheral.setAdvertisedServiceUuid(batteryService.uuid()); // add the service UUID |
| 47 | + blePeripheral.addAttribute(batteryService); // Add the BLE Battery service |
| 48 | + blePeripheral.addAttribute(batteryLevelChar); // add the battery level characteristic |
| 49 | + batteryLevelChar.setValue(oldBatteryLevel); // initial value for this characteristic |
61 | 50 |
|
62 | 51 | /* Now activate the BLE device. It will start continuously transmitting BLE
|
63 |
| - * advertising packets and thus become visible to remote BLE central devices |
64 |
| - * (e.g smartphones) until it receives a new connection */ |
| 52 | + advertising packets and will be visible to remote BLE central devices |
| 53 | + until it receives a new connection */ |
65 | 54 | blePeripheral.begin();
|
66 | 55 | Serial.println("Bluetooth device active, waiting for connections...");
|
67 | 56 | }
|
68 | 57 |
|
69 | 58 | void loop() {
|
| 59 | + // listen for BLE peripherals to connect: |
70 | 60 | BLECentral central = blePeripheral.central();
|
71 | 61 |
|
| 62 | + // if a central is connected to peripheral: |
72 | 63 | if (central) {
|
73 |
| - // central connected to peripheral |
74 |
| - Serial.print(F("Connected to central: ")); |
| 64 | + Serial.print("Connected to central: "); |
| 65 | + // print the central's MAC address: |
75 | 66 | Serial.println(central.address());
|
76 |
| - |
| 67 | + // turn on the LED to indicate the connection: |
77 | 68 | digitalWrite(13, HIGH);
|
78 | 69 |
|
| 70 | + // check the battery level every 200ms |
| 71 | + // as long as the central is still connected: |
79 | 72 | while (central.connected()) {
|
80 |
| - // central still connected to peripheral |
81 |
| - |
82 |
| - unsigned long currentMillis = millis(); |
83 |
| - |
| 73 | + long currentMillis = millis(); |
| 74 | + // if 200ms have passed, check the battery level: |
84 | 75 | if (currentMillis - previousMillis >= 200) {
|
85 | 76 | previousMillis = currentMillis;
|
86 | 77 | updateBatteryLevel();
|
87 | 78 | }
|
88 | 79 | }
|
89 |
| - |
| 80 | + // when the central disconnects, turn off the LED: |
90 | 81 | digitalWrite(13, LOW);
|
91 |
| - |
92 |
| - // central disconnected |
93 |
| - Serial.print(F("Disconnected from central: ")); |
94 |
| - Serial.println(central.address()); |
| 82 | + Serial.print("Disconnected from central: "); |
| 83 | + Serial.println(central.address()); |
95 | 84 | }
|
96 | 85 | }
|
97 | 86 |
|
98 | 87 | void updateBatteryLevel() {
|
99 | 88 | /* Read the current voltage level on the A0 analog input pin.
|
100 |
| - * This is used here to simulate the charge level of a "battery". |
101 |
| - * The following tutorial shows how a potentiometer could be used |
102 |
| - * to vary the voltage on an analog input pin: |
103 |
| - * https://www.arduino.cc/en/Tutorial/Potentiometer |
104 |
| - */ |
105 |
| - uint8_t battLvl = map(analogRead(A0), 0, 1023, 0, 100); |
106 |
| - |
107 |
| - if (battLvl != oldBattLvl) { |
108 |
| - Serial.print("Battery Level % is now: "); |
109 |
| - Serial.println(battLvl); |
110 |
| - |
111 |
| - /* If the voltage level has changed, we update the value of the |
112 |
| - * Battery Level BLE characteristic. Because we have enabled |
113 |
| - * notifications for this characteristic, the remote device can |
114 |
| - * receive automatic updates when this value is changed. */ |
115 |
| - battLvlChar.setValue(battLvl); |
116 |
| - oldBattLvl = battLvl; |
| 89 | + This is used here to simulate the charge level of a battery. |
| 90 | + */ |
| 91 | + int battery = analogRead(A0); |
| 92 | + int batteryLevel = map(battery, 0, 1023, 0, 100); |
| 93 | + |
| 94 | + if (batteryLevel != oldBatteryLevel) { // if the battery level has changed |
| 95 | + Serial.print("Battery Level % is now: "); // print it |
| 96 | + Serial.println(batteryLevel); |
| 97 | + batteryLevelChar.setValue(batteryLevel); // and update the battery level characteristic |
| 98 | + oldBatteryLevel = batteryLevel; // save the level for next comparison |
117 | 99 | }
|
118 | 100 | }
|
0 commit comments