Hello everyone,
I am trying to send my Heart Rate measurements from my Nano 33 BLE to my phone and trying to use the UUID="2A37" but I have problems understanding the syntax I must use.
I got my information from here:
https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.heart_rate_measurement.xml
I would like to get only my Heart Rate Measurement (bpm) and that the Sensor is connected to the wrist without the Energy expended or the RR interval. I dont understand how to use the flag, and what to send through the characteristic in order to get my results.
My code is here:
//libraries for SERIAL, MAX30105, SEN15805 (TMP117), HR algorithm
#include <Wire.h>
#include "MAX30105.h"
#include <SparkFun_TMP117.h> // Used to send and recieve specific information from our sensor
#include "heartRate.h"
//init for HR algorithm
const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred
float beatsPerMinute;
int beatAvg;
int countAvgBeat; //counter of how many beatAvg measurements we got
uint8_t sumBeat=0; // sum of all the beatAvg measurements in 10sec / every 250 measurements or in 5 sec / every 125 measurements
//uint8_t Flags= 0b10000000;
//uint16_t C2;
//STORED on Ram for BLE
int16_t stored_temp=0; // value that we sent through BLE
// The default address of the device is 0x48 = (GND)
TMP117 sensor; // Initalize sensor for temperature
//The default address of the device is 0x57
MAX30105 particleSensor; //Initialize sensor for HR and SP02
//attempt to store shit in txt file
#include <iostream> //fixed
#include <fstream>
//for precision 0.01
#include <iomanip> // std::setprecision
//BLE library
#include <ArduinoBLE.h>
//#define BLE_Temperature_Service_UUID "1234567-1111-123t-TEMP-123458ya993h" //random UUID
#define BLE_Temperature_Service_UUID "1809" //random UUID
//#define BLE_SP02_Service_UUID "3456789-2222-345s-SP02-123458ya992h" //random UUID
#define BLE_SP02_Service_UUID "1822" //random UUID
//#define BLE_HR_Service_UUID "4567891-3333-456h-HR42-123458ya995h" //random UUID
#define BLE_HR_Service_UUID "180D" //random UUID
#define BLE_Temperature_UUID "2A6E" //from 16-bit UUID NumbersDocument
#define BLE_SP02_UUID "2A5E" //from 16-bit UUID NumbersDocument
#define BLE_HR_UUID "2A37" //from 16-bit UUID NumbersDocument
//#define BLE_HR_UUID "39635fg3-4dh2-4660-bc38-6840efej55c1" //random UUID
BLEService temperatureService( BLE_Temperature_Service_UUID ); // Service for temperature measurements
BLEService spo2Service(BLE_SP02_Service_UUID); //Service for SP02 measurements
BLEService HeartRateService(BLE_HR_Service_UUID ); //Service for heart rate measurements
BLEIntCharacteristic temperatureCharacteristic( BLE_Temperature_UUID ,BLERead | BLENotify ); // remote clients will only be able to read this
BLEIntCharacteristic spo2Characteristic( BLE_SP02_UUID, BLERead | BLENotify );
BLEIntCharacteristic heartrateCharacteristic( BLE_HR_UUID , BLERead | BLENotify );
//byte array uint8_t gr[4];
void setup() {
// put your setup code here, to run once:
//MAX30105 INIT for HEARTBEAT
Serial.begin(115200);
while (!Serial);
// Initialize sensor
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println("MAX30105 was not found. Please check wiring/power. ");
while (1);
}
Serial.println("Place your index finger on the sensor with steady pressure.");
///SETUP configuration vars for HEARTBEAT///
//We need Use 6.4mA for LED drive,Red+IR, sampleRate=50, adcRange = 16384, sampleAverage = 4, pulseWidth=?
Serial.println("Initializing for HeartBeat");
particleSensor.setup(); //Configure sensor. Use 6.4mA for LED drive
particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
//END_HeartBeat//
//MAX30105 Init for SP02 algorithm
Serial.begin(115200); // initialize serial communication at 115200 bits per second:
while (!Serial);
// Initialize sensor
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println(F("MAX30105 was not found. Please check wiring/power."));
while (1);
}
///SETUP configuration vars for SP02 ///
//Default is 0x1F which gets us 6.4mA
//powerLevel = 0x02, 0.4mA - Presence detection of ~4 inch
//powerLevel = 0x1F, 6.4mA - Presence detection of ~8 inch
//powerLevel = 0x7F, 25.4mA - Presence detection of ~8 inch
//powerLevel = 0xFF, 50.0mA - Presence detection of ~12 inch
byte ledBrightness = 60; //Options: 0=Off to 255=50mA
byte sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
byte sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
//The longer the pulse width the longer range of detection you'll have
//At 69us and 0.4mA it's about 2 inches
//At 411us and 0.4mA it's about 6 inches
int pulseWidth = 411; //Options: 69 get us 15 bit res, 118 get us 16bit res, 215 get us 17 bit res, 411 get us 18bit res
int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
//END_SP02//
//TEMPERATURE CODE Init
Serial.begin(115200);
while (!Serial);
Wire.setClock(400000); // Set clock speed to be the fastest for better communication (fast mode)
//Check if TMP117 is working and start the readings
Serial.println("TMP117 Init for Readings");
if (sensor.begin() == true) // Function to check if the sensor will correctly self-identify with the proper Device ID/Address
{
Serial.println("Begin measurements");
}
else
{
Serial.println("Device failed to setup- Freezing code.");
while (1); // Runs forever
}
//END_TEMP//
//BLE
Serial.begin(9600); // initialize serial communication
while (!Serial);
if (!BLE.begin()) { // initialize BLE
Serial.println("starting BLE failed!");
while (1);
}
//set advertised local name and service
BLE.setDeviceName("Arduino Nano 33 BLE device");
BLE.setLocalName("Arduino Nano 33 BLE"); // Set name for connection
BLE.setAdvertisedService(temperatureService); // Advertise service
// BLE.setAdvertisedService(spo2Service); // Advertise service
BLE.setAdvertisedService(HeartRateService);
//BLE and characteristics for temperatureService
temperatureService.addCharacteristic(temperatureCharacteristic); // Add characteristic to service
//BLE and characteristics for spo2Service
spo2Service.addCharacteristic(spo2Characteristic);
//Characteristics for Heart Rate Service
HeartRateService.addCharacteristic(heartrateCharacteristic);
//add Service
BLE.addService(temperatureService);
BLE.addService(spo2Service);
BLE.addService(HeartRateService);
BLE.advertise(); // Start advertising
Serial.print("Peripheral device MAC: ");
Serial.println(BLE.address());
Serial.println("Waiting for connections...");
}
void loop() {
// put your main code here, to run repeatedly:
BLEDevice central = BLE.central(); //Waits for BLE Central device to connect
if (central)
{
Serial.print("Connected to central: ");
Serial.println(central.address());
while(central.connected()){
heartbeat();
uint8_t Flags= 0b00000000;
uint16_t C1 = sumBeat;
uint16_t C3 = 0x00;
uint16_t C4 = 0x00;
//static const byte HRBLE= (Flags, C2, C3, C4);
static const uint64_t HRBLE= (C4 | C3 | C1 | Flags);
Serial.println(HRBLE, BIN);
heartrateCharacteristic.writeValue(HRBLE);
}
}
}
void heartbeat() {
//Heart beat calc and prints
long irValue = particleSensor.getIR();
*
if (checkForBeat(irValue) == true)
{
//We sensed a beat!
long delta = millis() - lastBeat;
lastBeat = millis();
beatsPerMinute = 60 / (delta / 1000.0);
if (beatsPerMinute < 255 && beatsPerMinute > 33)
{
rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
rateSpot %= RATE_SIZE; //Wrap variable
//Take average of readings
beatAvg = 0;
for (byte x = 0 ; x < RATE_SIZE ; x++)
beatAvg += rates[x];
beatAvg /= RATE_SIZE;
}
}
// Serial.print("IR=");
// Serial.print(irValue);
Serial.print(", BPM=");
Serial.print(beatsPerMinute);
Serial.print(" Avg BPM=");
Serial.print(beatAvg);
sumBeat = beatAvg ;
countAvgBeat++;
Serial.println();
// if (irValue < 50000)
// Serial.print(" No finger?");
// Serial.println();
}
I tried many different things but still I only get syntax errors. The same thing applies to UUID="2A5E" for Sp02 measurements which uses a flag.
Any guidance on how we use flags and send multiple values through BLE characteristics?
Thanks in advance!