Created
February 3, 2021 20:00
-
-
Save webdevwilson/e6cef3e3244f34a6f670c74a47116317 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ArduinoBLE.h> | |
// Heart Rate Monitor | |
#define BLE_MAC "e9:41:07:7b:ac:1d" | |
#define BLE_SVC "180d" | |
#define BLE_CHR "2a37" | |
// Kickr Core | |
//#define BLE_MAC "ec-7d-27-1c-d6-a5" | |
//#define BLE_SVC "1818" | |
#define SAMPLE_SIZE 10 | |
int ledPin = LED_BUILTIN; | |
int ledState = LOW; | |
int deviceConnected = 0; | |
unsigned short interval = 1000; | |
unsigned long previousMillis = 0; | |
unsigned long currentMillis = 0; | |
BLECharacteristic bleCharacteristic; | |
int val = 0; | |
int vals[SAMPLE_SIZE]; | |
int valIndex = 0; | |
// the setup function runs once when you press reset or power the board | |
void setup() { | |
pinMode(ledPin, OUTPUT); | |
Serial.begin(9600); | |
while (!Serial) { | |
; // wait for serial port to connect. Needed for native USB | |
} | |
if (!BLE.begin()) { | |
Serial.println("Starting BLE failed!"); | |
while (1); | |
} else { | |
Serial.println("Started BLE"); | |
} | |
BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler); | |
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); | |
} | |
void blePeripheralConnectHandler(BLEDevice central) { | |
// central connected event handler | |
Serial.print("Connected event, central: "); | |
Serial.println(central.address()); | |
} | |
void blePeripheralDisconnectHandler(BLEDevice central) { | |
// central disconnected event handler | |
Serial.print("Disconnected event, central: "); | |
Serial.println(central.address()); | |
deviceConnected = 0; | |
} | |
// the loop function runs over and over again forever | |
void loop() { | |
currentMillis = millis(); | |
if (currentMillis - previousMillis >= interval) { | |
previousMillis = currentMillis; | |
if (deviceConnected) { | |
if (bleCharacteristic.valueUpdated() && bleCharacteristic.value()) { | |
const unsigned char* v = bleCharacteristic.value(); | |
val = v[1]; | |
digitalWrite(LED_BUILTIN, HIGH); | |
} | |
updateValue(val); | |
} else { | |
// toggle led when trying to connect | |
if (ledState == LOW) { | |
ledState = HIGH; | |
} else { | |
ledState = LOW; | |
} | |
digitalWrite(ledPin, ledState); | |
deviceConnected = connectToDevice(); | |
if(deviceConnected) { | |
digitalWrite(ledPin, HIGH); | |
} | |
} | |
} | |
} | |
void updateValue(int val) { | |
vals[valIndex++] = val; | |
if (valIndex == SAMPLE_SIZE) { | |
valIndex = 0; | |
} | |
// calculate average | |
float total = 0.0; | |
for (int i = 0; i < SAMPLE_SIZE; i++) { | |
total += vals[i]; | |
} | |
float avg = total / SAMPLE_SIZE; | |
int zone; | |
if (avg <= 108.0) { | |
zone = 1; | |
} else if (avg <= 126) { | |
zone = 2; | |
} else if (avg <= 144) { | |
zone = 3; | |
} else if (avg <= 162) { | |
zone = 4; | |
} else { | |
zone = 5; | |
} | |
Serial.print("Current: "); | |
Serial.print(String(val)); | |
Serial.print(" Rolling average: "); | |
Serial.print(String(avg)); | |
Serial.print(" HR Zone: "); | |
Serial.println(String(zone)); | |
} | |
int connectToDevice() { | |
if(!BLE.scanForAddress(BLE_MAC)) { | |
Serial.println("BLE Scan failed"); | |
return 0; | |
} | |
BLEDevice peripheral = BLE.available(); | |
if (peripheral) { | |
// connect to the device | |
if(!peripheral.connected()) { | |
Serial.print("Connecting to '"); | |
Serial.print(peripheral.localName()); | |
Serial.println("' ..."); | |
BLE.stopScan(); | |
if (!peripheral.connect()) { | |
Serial.println("Failed to connect"); | |
return 0; | |
} | |
} | |
// discover peripheral attributes | |
Serial.println("Discovering attributes ..."); | |
if (!peripheral.discoverAttributes()) { | |
Serial.println("Attribute discovery failed!"); | |
peripheral.disconnect(); | |
return 0; | |
} | |
// get service | |
BLEService svc = peripheral.service(BLE_SVC); | |
if (!svc) { | |
Serial.print("Failed to find service '"); | |
Serial.print(BLE_SVC); | |
Serial.println("'"); | |
return 0; | |
} | |
// get characteristic | |
bleCharacteristic = svc.characteristic(BLE_CHR); | |
if(!bleCharacteristic) { | |
Serial.print("Failed to find characteristic '"); | |
Serial.print(BLE_SVC); | |
Serial.println("'"); | |
return 0; | |
} | |
// subscribe | |
if (!bleCharacteristic.subscribed()) { | |
Serial.println("Subscribing to characteristic"); | |
Serial.println(bleCharacteristic.valueSize()); | |
if (!bleCharacteristic.subscribe()) { | |
Serial.println("subscription failed!"); | |
return 0; | |
} else { | |
Serial.println("subscribed!"); | |
} | |
} | |
digitalWrite(LED_BUILTIN, HIGH); | |
} else { | |
Serial.println("No device found"); | |
return 0; | |
} | |
return 1; | |
} | |
void setZone(int zone) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment