|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 Intel Corporation. All rights reserved. |
| 3 | + * See the bottom of this file for the license terms. |
| 4 | + */ |
| 5 | + |
| 6 | +/* |
| 7 | + * Sketch: Genuino101CurieBLEHeartRateMonitor.ino. |
| 8 | + * |
| 9 | + * Description: |
| 10 | + * This sketch example partially implements the standard Bluetooth Low-Energy |
| 11 | + * Heart Rate service. For more information: |
| 12 | + * https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx |
| 13 | + * |
| 14 | + */ |
| 15 | + |
| 16 | +#include <CurieBLE.h> |
| 17 | + |
| 18 | +BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming) |
| 19 | +BLEService heartRateService("180D"); // BLE Heart Rate Service |
| 20 | + |
| 21 | +// BLE Heart Rate Measurement Characteristic" |
| 22 | +BLECharacteristic heartRateChar("2A37", // standard 16-bit characteristic UUID |
| 23 | + BLERead | BLENotify, 2); // remote clients will be able to get notifications if this characteristic changes |
| 24 | + // the characteristic is 2 bytes long as the first field needs to be "Flags" as per BLE specifications |
| 25 | + // https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml |
| 26 | + |
| 27 | +int oldHeartRate = 0; // last heart rate reading from analog input |
| 28 | +long previousMillis = 0; // last time the heart rate was checked, in ms |
| 29 | + |
| 30 | +void setup() { |
| 31 | + Serial.begin(9600); // initialize serial communication |
| 32 | + pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected |
| 33 | + |
| 34 | + /* Set a local name for the BLE device |
| 35 | + This name will appear in advertising packets |
| 36 | + and can be used by remote devices to identify this BLE device |
| 37 | + The name can be changed but maybe be truncated based on space left in advertisement packet */ |
| 38 | + blePeripheral.setLocalName("HeartRateSketch"); |
| 39 | + blePeripheral.setAdvertisedServiceUuid(heartRateService.uuid()); // add the service UUID |
| 40 | + blePeripheral.addAttribute(heartRateService); // Add the BLE Heart Rate service |
| 41 | + blePeripheral.addAttribute(heartRateChar); // add the Heart Rate Measurement characteristic |
| 42 | + |
| 43 | + /* Now activate the BLE device. It will start continuously transmitting BLE |
| 44 | + advertising packets and will be visible to remote BLE central devices |
| 45 | + until it receives a new connection */ |
| 46 | + blePeripheral.begin(); |
| 47 | + Serial.println("Bluetooth device active, waiting for connections..."); |
| 48 | +} |
| 49 | + |
| 50 | +void loop() { |
| 51 | + // listen for BLE peripherals to connect: |
| 52 | + BLECentral central = blePeripheral.central(); |
| 53 | + |
| 54 | + // if a central is connected to peripheral: |
| 55 | + if (central) { |
| 56 | + Serial.print("Connected to central: "); |
| 57 | + // print the central's MAC address: |
| 58 | + Serial.println(central.address()); |
| 59 | + // turn on the LED to indicate the connection: |
| 60 | + digitalWrite(13, HIGH); |
| 61 | + |
| 62 | + // check the heart rate measurement every 200ms |
| 63 | + // as long as the central is still connected: |
| 64 | + while (central.connected()) { |
| 65 | + long currentMillis = millis(); |
| 66 | + // if 200ms have passed, check the heart rate measurement: |
| 67 | + if (currentMillis - previousMillis >= 200) { |
| 68 | + previousMillis = currentMillis; |
| 69 | + updateHeartRate(); |
| 70 | + } |
| 71 | + } |
| 72 | + // when the central disconnects, turn off the LED: |
| 73 | + digitalWrite(13, LOW); |
| 74 | + Serial.print("Disconnected from central: "); |
| 75 | + Serial.println(central.address()); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +void updateHeartRate() { |
| 80 | + /* Read the current voltage level on the A0 analog input pin. |
| 81 | + This is used here to simulate the heart rate's measurement. |
| 82 | + */ |
| 83 | + int heartRateMeasurement = analogRead(A0); |
| 84 | + int heartRate = map(heartRateMeasurement, 0, 1023, 0, 100); |
| 85 | + if (heartRate != oldHeartRate) { // if the heart rate has changed |
| 86 | + Serial.print("Heart Rate is now: "); // print it |
| 87 | + Serial.println(heartRate); |
| 88 | + const unsigned char heartRateCharArray[2] = { 0, (char)heartRate }; |
| 89 | + heartRateChar.setValue(heartRateCharArray, 2); // and update the heart rate measurement characteristic |
| 90 | + oldHeartRate = heartRate; // save the level for next comparison |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/* |
| 95 | + Copyright (c) 2015 Intel Corporation. All rights reserved. |
| 96 | +
|
| 97 | + This library is free software; you can redistribute it and/or |
| 98 | + modify it under the terms of the GNU Lesser General Public |
| 99 | + License as published by the Free Software Foundation; either |
| 100 | + version 2.1 of the License, or (at your option) any later version. |
| 101 | +
|
| 102 | + This library is distributed in the hope that it will be useful, |
| 103 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 104 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 105 | + Lesser General Public License for more details. |
| 106 | +
|
| 107 | + You should have received a copy of the GNU Lesser General Public |
| 108 | + License along with this library; if not, write to the Free Software |
| 109 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 110 | +*/ |
| 111 | + |
| 112 | + |
0 commit comments