program so that it returns ecg data instead of heart beat in BPM.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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() );
}

Expert Solution
steps

Step by step

Solved in 5 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY