#include <ArduinoBLE.
h>
#include <Wire.h>
#include <ClosedCube_MAX30205.h>
#include <MAX30105.h>
#include <heartRate.h>
#define SERVICE_UUID "e2718b81-13fd-40e1-beaa-8c1478943906"
#define CHARACTERISTIC_UUID "7f21a525-8ef5-43fa-92d2-3fb5564aefa9"
std::string value1 = "";
BLEService bleService("19B10000-E8F2-537E-4F6C-D104768A1214");
BLEStringCharacteristic stringTemp("183E", BLERead | BLEWrite, 31);
BLEStringCharacteristic stringBpm("2A19", BLERead | BLEWrite, 31);
BLEStringCharacteristic stringVolt("2a3F", BLERead | BLEWrite, 31);
BLEStringCharacteristic stringIncoming("537E", BLERead | BLEWrite, 31);
String inData = "";
ClosedCube_MAX30205 max30205;
MAX30105 particleSensor;
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;
#define vibrationMotor 7
#define buzzer 6
#define voltSens 3
int incoming;
float temp = 0.00;
float volt = 0.00;
float val = 0.00;
unsigned static long timer = 0;
unsigned static long timer1 = 0;
unsigned static long declaredTime = 30000;
unsigned static long dt = 0;
String data = "";
std::string input = "";
String a = "";
String b = "";
String c = "";
void setup() {
Serial.begin(9600);
while (!Serial);
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
BLE.setLocalName("esp32");
BLE.setAdvertisedService(bleService); // add the service UUID
bleService.addCharacteristic(stringTemp); // add the battery level
characteristic
bleService.addCharacteristic(stringBpm);
bleService.addCharacteristic(stringVolt);
bleService.addCharacteristic(stringIncoming);
pinMode(vibrationMotor, OUTPUT);
pinMode(buzzer, OUTPUT);
BLE.advertise();
Wire.begin(19, 18);
//max30205.begin(0x48);
// particleSensor.setup(); //Configure sensor with default settings
//
// particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate
sensor is running
// particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
}
void readTemp(){
temp = max30205.readTemperature();
}
void readBPM(){
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 > 20)
{
rates[rateSpot++] = (byte)beatsPerMinute;
rateSpot %= RATE_SIZE;
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);
if (irValue < 50000)
Serial.print(" No finger?");
Serial.println();
}
void getVoltage(){
val = analogRead(voltSens);
volt = val*(3.3/4095)*2;
String a1,b1,c1;
void loop() {
BLEDevice central = BLE.central();
if (central) {
if(millis() - timer1 > dt){
while(true){
if(millis() - timer < 20000){ //for 20 seconds it will active
readTemp();
readBPM();
getVoltage();
a1 = temp;
b1 = beatsPerMinute;
c1 = volt;
a = "Temp: " + a1;
b = "BPM: " +b1;
c = "Voltage: " +c1;
BLEDevice central = BLE.central();
BLE.addService(bleService);
stringTemp.writeValue(a.c_str());
stringBpm.writeValue(b.c_str());
stringVolt.writeValue(c.c_str());
if(central.connected()){
if(stringIncoming.written()){
if(stringIncoming.value()){
digitalWrite(vibrationMotor, HIGH);
digitalWrite(buzzer, HIGH);
delay(2000); //playing buzzerr and vibration motor for two seconds
digitalWrite(vibrationMotor, LOW);
digitalWrite(buzzer, LOW);
BLEDevice central = BLE.central();
}
}
}
digitalWrite(vibrationMotor, LOW);
digitalWrite(buzzer, LOW);
}
else{
timer1 = millis();
dt = declaredTime;
break;
}
}
}
else{
timer = millis();
}
}
}