|
| 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 | + */ |
| 18 | +#include <CurieBle.h> |
| 19 | + |
| 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 | + */ |
| 24 | + |
| 25 | +/* BLE Peripheral Device (this Intel Curie device) */ |
| 26 | +BlePeripheral blePeripheral; |
| 27 | + |
| 28 | +/* BLE Battery Service */ |
| 29 | +BleService battSvc("180F"); |
| 30 | + |
| 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; |
| 39 | + |
| 40 | +void setup() { |
| 41 | + Serial.begin(115200); |
| 42 | + |
| 43 | + pinMode(13, OUTPUT); |
| 44 | + |
| 45 | + /* 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); |
| 61 | + |
| 62 | + /* 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 */ |
| 65 | + blePeripheral.begin(); |
| 66 | + Serial.println("Bluetooth device active, waiting for connections..."); |
| 67 | +} |
| 68 | + |
| 69 | +void loop() { |
| 70 | + BleCentral central = blePeripheral.central(); |
| 71 | + |
| 72 | + if (central) { |
| 73 | + // central connected to peripheral |
| 74 | + Serial.print(F("Connected to central: ")); |
| 75 | + Serial.println(central.address()); |
| 76 | + |
| 77 | + digitalWrite(13, HIGH); |
| 78 | + |
| 79 | + while (central.connected()) { |
| 80 | + // central still connected to peripheral |
| 81 | + |
| 82 | + unsigned long currentMillis = millis(); |
| 83 | + |
| 84 | + if (currentMillis - previousMillis >= 200) { |
| 85 | + updateBatteryLevel(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + digitalWrite(13, LOW); |
| 90 | + |
| 91 | + // central disconnected |
| 92 | + Serial.print(F("Disconnected from central: ")); |
| 93 | + Serial.println(central.address()); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +void updateBatteryLevel() { |
| 98 | + /* Read the current voltage level on the A0 analog input pin. |
| 99 | + * This is used here to simulate the charge level of a "battery". |
| 100 | + * The following tutorial shows how a potentiometer could be used |
| 101 | + * to vary the voltage on an analog input pin: |
| 102 | + * https://www.arduino.cc/en/Tutorial/Potentiometer |
| 103 | + */ |
| 104 | + uint8_t battLvl = map(analogRead(A0), 0, 1023, 0, 100); |
| 105 | + |
| 106 | + if (battLvl != oldBattLvl) { |
| 107 | + Serial.print("Battery Level % is now: "); |
| 108 | + Serial.println(battLvl); |
| 109 | + |
| 110 | + /* If the voltage level has changed, we update the value of the |
| 111 | + * Battery Level BLE characteristic. Because we have enabled |
| 112 | + * notifications for this characteristic, the remote device can |
| 113 | + * receive automatic updates when this value is changed. */ |
| 114 | + battLvlChar.setValue(battLvl); |
| 115 | + oldBattLvl = battLvl; |
| 116 | + } |
| 117 | +} |
| 118 | + |
0 commit comments