diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2019-02-28 17:04:22 -0500 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2019-02-28 17:04:22 -0500 |
commit | d6869d1ec4bd24cd2c3eafa534f0849b25ec5607 (patch) | |
tree | 79e54ed27b39c31864895535d11399708d5a45c0 /arduino/libraries/Bluefruit52Lib/examples/Central/central_custom_hrm | |
parent | 614ee97bf3a2270c413527a7f35c54cbecd9e601 (diff) |
added basic code
Diffstat (limited to 'arduino/libraries/Bluefruit52Lib/examples/Central/central_custom_hrm')
-rwxr-xr-x | arduino/libraries/Bluefruit52Lib/examples/Central/central_custom_hrm/central_custom_hrm.ino | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/arduino/libraries/Bluefruit52Lib/examples/Central/central_custom_hrm/central_custom_hrm.ino b/arduino/libraries/Bluefruit52Lib/examples/Central/central_custom_hrm/central_custom_hrm.ino new file mode 100755 index 0000000..a0cef0c --- /dev/null +++ b/arduino/libraries/Bluefruit52Lib/examples/Central/central_custom_hrm/central_custom_hrm.ino @@ -0,0 +1,205 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +/* This sketch show how to use BLEClientService and BLEClientCharacteristic + * to implement a custom client that is used to talk with Gatt server on + * peripheral. + * + * Note: you will need another feather52 running peripheral/custom_HRM sketch + * to test with. + */ + +#include <bluefruit.h> + +/* HRM Service Definitions + * Heart Rate Monitor Service: 0x180D + * Heart Rate Measurement Char: 0x2A37 (Mandatory) + * Body Sensor Location Char: 0x2A38 (Optional) + */ + +BLEClientService hrms(UUID16_SVC_HEART_RATE); +BLEClientCharacteristic hrmc(UUID16_CHR_HEART_RATE_MEASUREMENT); +BLEClientCharacteristic bslc(UUID16_CHR_BODY_SENSOR_LOCATION); + +void setup() +{ + Serial.begin(115200); + while ( !Serial ) delay(10); // for nrf52840 with native usb + + Serial.println("Bluefruit52 Central Custom HRM Example"); + Serial.println("--------------------------------------\n"); + + // Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1 + // SRAM usage required by SoftDevice will increase dramatically with number of connections + Bluefruit.begin(0, 1); + + Bluefruit.setName("Bluefruit52 Central"); + + // Initialize HRM client + hrms.begin(); + + // Initialize client characteristics of HRM. + // Note: Client Char will be added to the last service that is begin()ed. + bslc.begin(); + + // set up callback for receiving measurement + hrmc.setNotifyCallback(hrm_notify_callback); + hrmc.begin(); + + // Increase Blink rate to different from PrPh advertising mode + Bluefruit.setConnLedInterval(250); + + // Callbacks for Central + Bluefruit.Central.setDisconnectCallback(disconnect_callback); + Bluefruit.Central.setConnectCallback(connect_callback); + + /* Start Central Scanning + * - Enable auto scan if disconnected + * - Interval = 100 ms, window = 80 ms + * - Don't use active scan + * - Filter only accept HRM service + * - Start(timeout) with timeout = 0 will scan forever (until connected) + */ + Bluefruit.Scanner.setRxCallback(scan_callback); + Bluefruit.Scanner.restartOnDisconnect(true); + Bluefruit.Scanner.setInterval(160, 80); // in unit of 0.625 ms + Bluefruit.Scanner.filterUuid(hrms.uuid); + Bluefruit.Scanner.useActiveScan(false); + Bluefruit.Scanner.start(0); // // 0 = Don't stop scanning after n seconds +} + +void loop() +{ + // do nothing +} + +/** + * Callback invoked when scanner pick up an advertising data + * @param report Structural advertising data + */ +void scan_callback(ble_gap_evt_adv_report_t* report) +{ + // Since we configure the scanner with filterUuid() + // Scan callback only invoked for device with hrm service advertised + // Connect to device with HRM service in advertising + Bluefruit.Central.connect(report); +} + +/** + * Callback invoked when an connection is established + * @param conn_handle + */ +void connect_callback(uint16_t conn_handle) +{ + Serial.println("Connected"); + Serial.print("Discovering HRM Service ... "); + + // If HRM is not found, disconnect and return + if ( !hrms.discover(conn_handle) ) + { + Serial.println("Found NONE"); + + // disconect since we couldn't find HRM service + Bluefruit.Central.disconnect(conn_handle); + + return; + } + + // Once HRM service is found, we continue to discover its characteristic + Serial.println("Found it"); + + Serial.print("Discovering Measurement characteristic ... "); + if ( !hrmc.discover() ) + { + // Measurement chr is mandatory, if it is not found (valid), then disconnect + Serial.println("not found !!!"); + Serial.println("Measurement characteristic is mandatory but not found"); + Bluefruit.Central.disconnect(conn_handle); + return; + } + Serial.println("Found it"); + + // Measurement is found, continue to look for option Body Sensor Location + // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.body_sensor_location.xml + // Body Sensor Location is optional, print out the location in text if present + Serial.print("Discovering Body Sensor Location characteristic ... "); + if ( bslc.discover() ) + { + Serial.println("Found it"); + + // Body sensor location value is 8 bit + const char* body_str[] = { "Other", "Chest", "Wrist", "Finger", "Hand", "Ear Lobe", "Foot" }; + + // Read 8-bit BSLC value from peripheral + uint8_t loc_value = bslc.read8(); + + Serial.print("Body Location Sensor: "); + Serial.println(body_str[loc_value]); + }else + { + Serial.println("Found NONE"); + } + + // Reaching here means we are ready to go, let's enable notification on measurement chr + if ( hrmc.enableNotify() ) + { + Serial.println("Ready to receive HRM Measurement value"); + }else + { + Serial.println("Couldn't enable notify for HRM Measurement. Increase DEBUG LEVEL for troubleshooting"); + } +} + +/** + * Callback invoked when a connection is dropped + * @param conn_handle + * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h + * https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/cores/nRF5/nordic/softdevice/s140_nrf52_6.1.1_API/include/ble_hci.h + */ +void disconnect_callback(uint16_t conn_handle, uint8_t reason) +{ + (void) conn_handle; + (void) reason; + + Serial.println("Disconnected"); +} + + +/** + * Hooked callback that triggered when a measurement value is sent from peripheral + * @param chr Pointer client characteristic that even occurred, + * in this example it should be hrmc + * @param data Pointer to received data + * @param len Length of received data + */ +void hrm_notify_callback(BLEClientCharacteristic* chr, uint8_t* data, uint16_t len) +{ + // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.heart_rate_measurement.xml + // Measurement contains of control byte0 and measurement (8 or 16 bit) + optional field + // if byte0's bit0 is 0 --> measurement is 8 bit, otherwise 16 bit. + + Serial.print("HRM Measurement: "); + + if ( data[0] & bit(0) ) + { + uint16_t value; + memcpy(&value, data+1, 2); + + Serial.println(value); + } + else + { + Serial.println(data[1]); + } +} |