ESP32 Heart Rate Monitoring
ESP32 Heart Rate Monitoring
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>> 01_Test_Pulse_Sensor_and_Get_Threshold_Value
#define PulseSensor_PIN 36
#define LED_PIN 23
int Signal; //--> Accommodates the signal value (ADC value) from the pulse sensor.
// int UpperThreshold = 520;
// int LowerThreshold = 500;
//
________________________________________________________________________________VOI
D SETUP()
void setup() {
// put your setup code here, to run once:
//
________________________________________________________________________________VOI
D LOOP()
void loop() {
// put your main code here, to run repeatedly:
delay(20);
}
//________________________________________________________________________________
//
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<
//
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>> 02_Test_OLED_and_Button
//----------------------------------------Include Library.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//----------------------------------------
//
________________________________________________________________________________VOI
D SETUP()
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();
delay(2000);
pinMode(Button_PIN, INPUT_PULLUP);
//
________________________________________________________________________________VOI
D LOOP()
void loop() {
// put your main code here, to run repeatedly:
Button_State = digitalRead(Button_PIN);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0); //--> (x position, y position)
display.print("Button : ");
display.print(Button_State);
display.display();
delay(250);
}
//________________________________________________________________________________
//
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<
//
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>> 03_ESP32_Monitoring_HeartRate_OLED
//----------------------------------------Include Library
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//----------------------------------------
#define PulseSensor_PIN 36
#define LED_PIN 23
#define Button_PIN 32
unsigned long previousMillisGetHB = 0; //--> will store the last time Millis (to
get Heartbeat) was updated.
unsigned long previousMillisResultHB = 0; //--> will store the last time Millis (to
get BPM) was updated.
const long intervalGetHB = 20; //--> Interval for reading heart rate (Heartbeat) =
20ms.
const long intervalResultHB = 1000; //--> The reading interval for the result of
the Heart Rate calculation.
int timer_Get_BPM = 0;
int PulseSensorSignal; //--> Variable to accommodate the signal value from the
sensor.
int UpperThreshold = 520; //--> Determine which Signal to "count as a beat", and
which to ignore.
int LowerThreshold = 500;
//----------------------------------------'Heart_Icon', 16x16px
// I drew this heart icon at : http://dotmatrixtool.com/
const unsigned char Heart_Icon [] PROGMEM = {
0x00, 0x00, 0x18, 0x30, 0x3c, 0x78, 0x7e, 0xfc, 0xff, 0xfe, 0xff, 0xfe, 0xee,
0xee, 0xd5, 0x56,
0x7b, 0xbc, 0x3f, 0xf8, 0x1f, 0xf0, 0x0f, 0xe0, 0x07, 0xc0, 0x03, 0x80, 0x01,
0x00, 0x00, 0x00
};
//----------------------------------------
//
________________________________________________________________________________voi
d GetHeartRate()
void GetHeartRate() {
//----------------------------------------Process of reading heart beat.
unsigned long currentMillisGetHB = millis();
if (get_BPM == true) {
timer_Get_BPM++;
// "timer_Get_BPM > 10" means taking the number of heartbeats for 10 seconds.
if (timer_Get_BPM > 10) {
timer_Get_BPM = 1;
BPMval = cntHB * 6; //--> The taken heart rate is for 10 seconds. So to get
the BPM value, the total heart rate in 10 seconds x 6.
Serial.print("BPM : ");
Serial.println(BPMval);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(20, 48); //--> (x position, y position)
display.print(": ");
display.print(BPMval);
display.setCursor(92, 48); //--> (x position, y position)
display.print("BPM");
display.display();
cntHB = 0;
}
}
}
//----------------------------------------
}
//________________________________________________________________________________
//
________________________________________________________________________________Dra
wGraph()
// Subroutines for drawing or displaying heart rate graphic signals.
void DrawGraph() {
//----------------------------------------Condition to reset the graphic display
if it fills the width of the OLED screen.
if (x > 127) {
display.fillRect(0, 0, 128, 42, BLACK);
x = 0;
lastx = 0;
}
//----------------------------------------
int ySignalMap = map(ySignal, 350, 850, 0, 40); //--> The y-axis used on OLEDs is
from 0 to 40.
y = 40 - ySignalMap;
//----------------------------------------
lastx = x;
lasty = y;
x++;
}
//________________________________________________________________________________
//
________________________________________________________________________________VOI
D SETUP()
void setup() {
// put your setup code here, to run once:
analogReadResolution(10);
pinMode(LED_PIN,OUTPUT);
pinMode(Button_PIN, INPUT_PULLUP);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(37, 0);
display.print("ESP32");
display.setCursor(13, 20);
display.print("HEARTBEAT");
display.setCursor(7, 40);
display.print("MONITORING");
display.display();
delay(2000);
display.clearDisplay();
display.drawLine(0, 43, 127, 43, WHITE); //--> drawLine(x1, y1, x2, y2, color)
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(10, 48); //--> (x position, y position)
display.print("HeartBeat");
display.display();
}
//________________________________________________________________________________
//
________________________________________________________________________________VOI
D LOOP()
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(Button_PIN) == LOW) {
delay(100);
cntHB = 0;
BPMval = 0;
x = 0;
y = 0;
lastx = 0;
lasty = 0;
get_BPM = !get_BPM;
if (get_BPM == true) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(14, 0);
display.print("Start Getting BPM");
display.setTextSize(3);
delay(500);
display.display();
delay(3000);
//----------------------------------------
display.drawLine(0, 43, 127, 43, WHITE); //--> drawLine(x1, y1, x2, y2,
color)
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(20, 48); //--> (x position, y position)
display.print(": 0 ");
display.setCursor(92, 48); //--> (x position, y position)
display.print("BPM");
display.display();
//----------------------------------------
}
else {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(42, 25);
display.print("STOP");
display.display();
delay(2000);
display.clearDisplay();
display.drawLine(0, 43, 127, 43, WHITE); //--> drawLine(x1, y1, x2, y2,
color)
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(10, 48); //--> (x position, y position)
display.print("HeartBeat");
display.display();
}
}
//
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>> 04_ESP32_Monitoring_HeartRate_OLED_Web_Server_STA
///////////////////////////////////////////////////////////////////////////////////
///////////////////////
// In this section, WiFi is used. So make sure that the ESP32 is getting enough
power supply. //
// In my tests, when using a 5V 2A power supply, this project (this section) worked
fine. //
// But if the power supply is less than that, then there is "noise" on the pulse
sensor readings. //
//
//
// If you want to change the PIN for the pulse sensor, then make sure you select
the adc PIN on "ADC1". //
// In ESP32 there are two ADC APIs, namely ADC1 and ADC2. ADC2 cannot be used if
WiFi is used. //
///////////////////////////////////////////////////////////////////////////////////
///////////////////////
//----------------------------------------Include Library
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <Arduino_JSON.h>
//----------------------------------------
// Include the web code (HTML, javascript and others) inside the char array
variable for the web server page.
#include "PageIndex.h"
unsigned long previousMillisGetHB = 0; //--> will store the last time Millis (to
get Heartbeat) was updated.
unsigned long previousMillisResultHB = 0; //--> will store the last time Millis (to
get BPM) was updated.
const long intervalGetHB = 35; //--> Interval for reading heart rate (Heartbeat) =
35ms.
const long intervalResultHB = 1000; //--> The reading interval for the result of
the Heart Rate calculation.
int timer_Get_BPM = 0;
int PulseSensorSignal; //--> Variable to accommodate the signal value from the
sensor.
int UpperThreshold = 520; //--> Determine which Signal to "count as a beat", and
which to ignore.
int LowerThreshold = 500;
// The char array variable to hold the "timestamp" data that will be sent to the
web server.
char tTime[10];
//----------------------------------------'Heart_Icon', 16x16px
// I drew this heart icon at : http://dotmatrixtool.com/
const unsigned char Heart_Icon [] PROGMEM = {
0x00, 0x00, 0x18, 0x30, 0x3c, 0x78, 0x7e, 0xfc, 0xff, 0xfe, 0xff, 0xfe, 0xee,
0xee, 0xd5, 0x56,
0x7b, 0xbc, 0x3f, 0xf8, 0x1f, 0xf0, 0x0f, 0xe0, 0x07, 0xc0, 0x03, 0x80, 0x01,
0x00, 0x00, 0x00
};
//----------------------------------------
// Initialize JSONVar
JSONVar JSON_All_Data;
//
________________________________________________________________________________voi
d GetHeartRate()
void GetHeartRate() {
//----------------------------------------Process of reading heart beat.
unsigned long currentMillisGetHB = millis();
if (get_BPM == true) {
timer_Get_BPM++;
// "timer_Get_BPM > 10" means taking the number of heartbeats for 10 seconds.
if (timer_Get_BPM > 10) {
timer_Get_BPM = 1;
tSecond += 10;
if (tSecond >= 60) {
tSecond = 0;
tMinute += 1;
}
if (tMinute >= 60) {
tMinute = 0;
tHour += 1;
}
BPMval = cntHB * 6; //--> The taken heart rate is for 10 seconds. So to get
the BPM value, the total heart rate in 10 seconds x 6.
Serial.print("BPM : ");
Serial.println(BPMval);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(20, 48); //--> (x position, y position)
display.print(": ");
display.print(BPMval);
display.setCursor(92, 48); //--> (x position, y position)
display.print("BPM");
display.display();
cntHB = 0;
}
}
}
//----------------------------------------
}
//________________________________________________________________________________
//
________________________________________________________________________________Dra
wGraph()
// Subroutines for drawing or displaying heart rate graphic signals.
void DrawGraph() {
//----------------------------------------Condition to reset the graphic display
if it fills the width of the OLED screen
if (x > 127) {
display.fillRect(0, 0, 128, 42, BLACK);
x = 0;
lastx = 0;
}
//----------------------------------------
int ySignalMap = map(ySignal, 350, 850, 0, 40); //--> The y-axis used on OLEDs is
from 0 to 40
y = 40 - ySignalMap;
//----------------------------------------
lastx = x;
lasty = y;
x++;
}
//________________________________________________________________________________
//
________________________________________________________________________________VOI
D SETUP()
void setup() {
// put your setup code here, to run once:
analogReadResolution(10);
pinMode(LED_PIN,OUTPUT);
pinMode(Button_PIN, INPUT_PULLUP);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(37, 0);
display.print("ESP32");
display.setCursor(13, 20);
display.print("HEARTBEAT");
display.setCursor(7, 40);
display.print("MONITORING");
display.display();
delay(2000);
delay(100);
Serial.println("------------");
Serial.println("WIFI STA");
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("------------");
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Connected");
display.setCursor(0, 10);
display.print("successfully.");
display.display();
delay(1000);
//::::::::::::::::::
//----------------------------------------
delay(500);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Setting Up Servers...");
display.display();
delay(1000);
if (request->hasParam(PARAM_INPUT_1)) {
BTN_Start_Get_BPM = request->getParam(PARAM_INPUT_1)->value();
Serial.println();
Serial.print("BTN_Start_Get_BPM : ");
Serial.println(BTN_Start_Get_BPM);
}
else {
BTN_Start_Get_BPM = "No message";
Serial.println();
Serial.print("BTN_Start_Get_BPM : ");
Serial.println(BTN_Start_Get_BPM);
}
request->send(200, "text/plain", "OK");
});
//----------------------------------------
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Setting Up");
display.setCursor(0, 10);
display.print("Server Completed.");
display.display();
delay(1000);
Serial.println();
Serial.println("------------");
Serial.print("ESP32 IP address : ");
Serial.println(WiFi.localIP());
Serial.println();
Serial.println("Visit the IP Address above in your browser to open the main
page.");
Serial.println("------------");
Serial.println();
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("ESP32 IP address :");
display.setCursor(0, 10);
display.print(WiFi.localIP());
display.display();
delay(3000);
display.clearDisplay();
display.drawLine(0, 43, 127, 43, WHITE); //--> drawLine(x1, y1, x2, y2, color)
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(10, 48); //--> (x position, y position)
display.print("HeartBeat");
display.display();
}
//________________________________________________________________________________
//
________________________________________________________________________________VOI
D LOOP()
void loop() {
// put your main code here, to run repeatedly:
BTN_Start_Get_BPM = "";
cntHB = 0;
BPMval = 0;
x = 0;
y = 0;
lastx = 0;
lasty = 0;
tSecond = 0;
tMinute = 0;
tHour = 0;
get_BPM = !get_BPM;
if (get_BPM == true) {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(14, 0);
display.print("Start Getting BPM");
display.setTextSize(3);
display.drawLine(0, 43, 127, 43, WHITE); //--> drawLine(x1, y1, x2, y2,
color)
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(20, 48); //--> (x position, y position)
display.print(": 0 ");
display.setCursor(92, 48); //--> (x position, y position)
display.print("BPM");
display.display();
//----------------------------------------
}
else {
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(42, 25);
display.print("STOP");
display.display();
delay(1000);
display.clearDisplay();
display.drawLine(0, 43, 127, 43, WHITE); //--> drawLine(x1, y1, x2, y2,
color)
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(10, 48); //--> (x position, y position)
display.print("HeartBeat");
display.display();
}
}
//
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>05_ESP32_Monitoring_HeartRate_OLED_Web_Server_AP
//---------------------------------------- Access Point Declaration and
Configuration.
const char* ssid = "ESP32_WS"; //--> access point name
const char* password = "helloesp32WS"; //--> access point password
IPAddress local_ip(192,168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
//----------------------------------------
delay(100);
Serial.println();
Serial.println("------------");
Serial.print("SSID name : ");
Serial.println(ssid);
Serial.print("IP address : ");
Serial.println(WiFi.softAPIP());
Serial.println();
Serial.println("Connect your computer or mobile Wifi to the SSID above.");
Serial.println("Visit the IP Address above in your browser to open the main
page.");
Serial.println("------------");
Serial.println();
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("ESP32 IP address :");
display.setCursor(0, 10);
display.print(WiFi.softAPIP());
display.display();
delay(3000);
//
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<