program so that it returns ecg data instead of heart beat in BPM.
Edit this program so that it returns ecg data instead of heart beat in BPM.
#include <ArduinoBLE.h>
#define BLE_UUID_HEART_RATE_SERVICE "180D"
#define BLE_UUID_HEART_RATE_MEASURMENT "2A37"
#define ECG_SERV_UUID 0x2D0D
#define ECG_MEAS_UUID 0x2D37
#define ECG_NUM_CHANS_UUID 0x2D38
#define ECG_SAMPLE_SETS_UUID 0x2D39
#define ECG_COMMAND_UUID 0x2D3A
#define HRM_VALUE_FORMAT_8BIT 0
#define HRM_VALUE_FORMAT_16BIT 1
#define HRM_SENSOR_CONTACT_NOT_DETECTED ( 2 << 1 )
#define HRM_SENSOR_CONTACT_DETECTED ( 3 << 1 )
typedef struct
{
uint8_t flags;
uint8_t heartRate;
} heart_rate_measurment_t;
union heart_rate_measurment_u
{
struct
{
heart_rate_measurment_t values;
};
uint8_t bytes[ sizeof( heart_rate_measurment_t ) ];
};
union heart_rate_measurment_u heartRate = { .values = { .flags = 0, .heartRate = 0 } };
#define BLE_DEVICE_NAME "Arduino HRM"
#define BLE_LOCAL_NAME "Arduino HRM"
BLEService heartRateService( BLE_UUID_HEART_RATE_SERVICE );
BLECharacteristic heartRateCharacteristic( BLE_UUID_HEART_RATE_MEASURMENT, BLERead | BLENotify, sizeof heartRate.bytes );
#define BLE_LED_PIN LED_BUILTIN
#define HEART_BEAT_PIN A0
#define SENSOR_UPDATE_INTERVAL (1000)
typedef struct
{
uint8_t heartRate;
bool sensorContact = false;
bool updated = false;
} sensor_data_t;
sensor_data_t sensorData;
void setup()
{
Serial.begin( 9600 );
Serial.println("----------------------------");
Serial.println(" SenSe " );
Serial.println(" Polar H10 pulsmonitor");
Serial.println("----------------------------");
pinMode( HEART_BEAT_PIN, INPUT );
if ( !setupBleMode() )
{
Serial.println( "Feil ved oppstart!" );
while ( 1 );
}
else
{
Serial.println( "Oppstart vellykket. Venter på tilkobling til Polar H10." );
}
}
void loop()
{
bleTask();
if ( sensorTask() )
{
printTask();
}
}
void printTask()
{
Serial.print( "PULS: " );
Serial.print( sensorData.heartRate );
Serial.println( " bpm" );
}
bool sensorTask()
{
static uint32_t previousMillis = 0;
uint32_t currentMillis = millis();
if ( currentMillis - previousMillis < SENSOR_UPDATE_INTERVAL )
{
return false;
}
previousMillis = currentMillis;
uint16_t heartRate = analogRead( HEART_BEAT_PIN );
sensorData.heartRate = ( uint8_t ) map( heartRate, 0, 1023, 40, 210 );
if ( sensorData.heartRate < 60 )
{
sensorData.sensorContact = false;
}
else
{
sensorData.sensorContact = true;
}
sensorData.updated = true;
return true;
}
bool setupBleMode()
{
if ( !BLE.begin() )
{
return false;
}
// set advertised local name and service UUID
BLE.setDeviceName( BLE_DEVICE_NAME );
BLE.setLocalName( BLE_LOCAL_NAME );
BLE.setAdvertisedService( heartRateService );
// add service
BLE.addService( heartRateService );
// set BLE event handlers
BLE.setEventHandler( BLEConnected, blePeripheralConnectHandler );
BLE.setEventHandler( BLEDisconnected, blePeripheralDisconnectHandler );
// start advertising
BLE.advertise();
return true;
}
void bleTask()
{
const uint32_t BLE_UPDATE_INTERVAL = 10;
static uint32_t previousMillis = 0;
uint32_t currentMillis = millis();
if ( currentMillis - previousMillis >= BLE_UPDATE_INTERVAL )
{
previousMillis = currentMillis;
}
if ( sensorData.updated )
{
heartRate.values.heartRate = sensorData.heartRate;
if ( sensorData.sensorContact )
{
heartRate.values.flags = ( heartRate.values.flags) | HRM_SENSOR_CONTACT_DETECTED;
}
else
{
heartRate.values.flags = ( heartRate.values.flags) | HRM_SENSOR_CONTACT_NOT_DETECTED;
}
heartRateCharacteristic.writeValue( heartRate.bytes, sizeof heartRate.bytes );
sensorData.updated = false;
}
}
void blePeripheralConnectHandler( BLEDevice central )
{
digitalWrite( BLE_LED_PIN, LOW );
Serial.print( F( "Connected to central: " ) );
Serial.println( central.address() );
}
void blePeripheralDisconnectHandler( BLEDevice central )
{
digitalWrite( BLE_LED_PIN, HIGH );
Serial.print( F( "Disconnected from central: " ) );
Serial.println( central.address() );
}
Step by step
Solved in 5 steps