0% found this document useful (0 votes)
34 views19 pages

ESP32 Heart Rate Monitoring

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views19 pages

ESP32 Heart Rate Monitoring

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 19

//

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>> 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:

Serial.begin(115200); //--> Set's up Serial Communication at certain speed.


Serial.println();
delay(2000);

// Set the ADC resolution. "analogReadResolution(10);" meaning the ADC resolution


is set at 10 bits (the ADC reading value is from 0 to 1023).
analogReadResolution(10);

// Set LED_PIN as Output.


pinMode(LED_PIN,OUTPUT);
}
//________________________________________________________________________________

//
________________________________________________________________________________VOI
D LOOP()
void loop() {
// put your main code here, to run repeatedly:

Signal = analogRead(PulseSensor_PIN); //--> Read the PulseSensor's value. Assign


this value to the "Signal" variable.

Serial.println(Signal); //--> Send the Signal value to Serial.

// if(Signal > UpperThreshold){ //--> If the signal is above


"520"(UpperThreshold), then "turn-on" the LED.
// digitalWrite(LED_PIN,HIGH);
// }

// if(Signal < LowerThreshold){


// digitalWrite(LED_PIN,LOW); //--> Else, the sigal must be below
"LowerThreshold", so "turn-off" the LED.
// }

delay(20);
}
//________________________________________________________________________________
//
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<
//
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>> 02_Test_OLED_and_Button
//----------------------------------------Include Library.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//----------------------------------------

// Defines the Pins of the Button.


#define Button_PIN 32

//----------------------------------------Configure OLED screen size in pixels.


#define SCREEN_WIDTH 128 //--> OLED display width, in pixels
#define SCREEN_HEIGHT 64 //--> OLED display height, in pixels
//----------------------------------------

//----------------------------------------Declaration for an SSD1306 display


connected to I2C (SDA, SCL pins).
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//----------------------------------------

// Variable declaration for buttons.


byte Button_State;

//
________________________________________________________________________________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);

//----------------------------------------SSD1306_SWITCHCAPVCC = generate display


voltage from 3.3V internally.
// Address 0x3C for 128x32 and Address 0x3D for 128x64.
// But on my 128x64 module the 0x3D address doesn't work. What works is the 0x3C
address.
// So please try which address works on your module.
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); //--> Don't proceed, loop forever
}
//----------------------------------------
}
//________________________________________________________________________________

//
________________________________________________________________________________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

//----------------------------------------Configure OLED screen size in pixels


#define SCREEN_WIDTH 128 //--> OLED display width, in pixels
#define SCREEN_HEIGHT 64 //--> OLED display height, in pixels
//----------------------------------------

//----------------------------------------Declaration for an SSD1306 display


connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//----------------------------------------

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;

int cntHB = 0; //--> Variable for counting the number of heartbeats.


boolean ThresholdStat = true; //--> Variable for triggers in calculating
heartbeats.
int BPMval = 0; //--> Variable to hold the result of heartbeats calculation.

int x=0; //--> Variable axis x graph values to display on OLED


int y=0; //--> Variable axis y graph values to display on OLED
int lastx=0; //--> The graph's last x axis variable value to display on the OLED
int lasty=0; //--> The graph's last y axis variable value to display on the OLED

// Boolean variables to start and stop getting BPM values.


bool get_BPM = false;

//----------------------------------------'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 (currentMillisGetHB - previousMillisGetHB >= intervalGetHB) {


previousMillisGetHB = currentMillisGetHB;

PulseSensorSignal = analogRead(PulseSensor_PIN); //--> Read the PulseSensor's


value. Assign this value to the "Signal" variable.

if (PulseSensorSignal > UpperThreshold && ThresholdStat == true) {


if (get_BPM == true) cntHB++;
ThresholdStat = false;
digitalWrite(LED_PIN,HIGH);
}

if (PulseSensorSignal < LowerThreshold) {


ThresholdStat = true;
digitalWrite(LED_PIN,LOW);
}

DrawGraph(); //--> Calling the DrawGraph() subroutine.


}
//----------------------------------------

//----------------------------------------The process for getting the BPM value.


unsigned long currentMillisResultHB = millis();

if (currentMillisResultHB - previousMillisResultHB >= intervalResultHB) {


previousMillisResultHB = currentMillisResultHB;

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.fillRect(20, 48, 108, 18, BLACK);

display.drawBitmap(0, 47, Heart_Icon, 16, 16, WHITE); //-->


display.drawBitmap(x position, y position, bitmap data, bitmap width, bitmap
height, color)
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(": ");
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;
}
//----------------------------------------

//----------------------------------------Process signal data to be displayed on


OLED in graphic form.
int ySignal = PulseSensorSignal;
if (ySignal > 850) ySignal = 850;
if (ySignal < 350) ySignal = 350;

int ySignalMap = map(ySignal, 350, 850, 0, 40); //--> The y-axis used on OLEDs is
from 0 to 40.

y = 40 - ySignalMap;
//----------------------------------------

//----------------------------------------Displays the heart rate graph.


display.writeLine(lastx,lasty,x,y,WHITE);
display.display();
//----------------------------------------

lastx = x;
lasty = y;

x++;
}
//________________________________________________________________________________

//
________________________________________________________________________________VOI
D SETUP()
void setup() {
// put your setup code here, to run once:

Serial.begin(115200); //--> Set's up Serial Communication at certain speed.


Serial.println();
delay(2000);

analogReadResolution(10);

pinMode(LED_PIN,OUTPUT);
pinMode(Button_PIN, INPUT_PULLUP);

//---------------------------------------- SSD1306_SWITCHCAPVCC = generate


display voltage from 3.3V internally.
// Address 0x3C for 128x32 and Address 0x3D for 128x64.
// But on my 128x64 module the 0x3D address doesn't work. What works is the 0x3C
address.
// So please try which address works on your module.
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); //--> Don't proceed, loop forever
}
//----------------------------------------

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

for (byte i = 3; i > 0; i--) {


display.setTextColor(WHITE);
display.setCursor(50, 20);
display.print(i);
display.display();
delay(1000);
display.setTextColor(BLACK);
display.setCursor(50, 20);
display.print(i);
display.display();
}

delay(500);

//----------------------------------------Displays BPM value reading


information
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);

display.setCursor(0, 12); //--> (x position, y position)


display.print(" Please wait");

display.setCursor(0, 22); //--> (x position, y position)


display.print(" 10 seconds");

display.setCursor(0, 32); //--> (x position, y position)


display.print(" to get");

display.setCursor(0, 42); //--> (x position, y position)


display.print(" the BPM value");

display.display();
delay(3000);
//----------------------------------------

//----------------------------------------Displays the initial display of BPM


value
display.clearDisplay(); //--> for Clearing the display

display.drawBitmap(0, 47, Heart_Icon, 16, 16, WHITE); //-->


display.drawBitmap(x position, y position, bitmap data, bitmap width, bitmap
height, color)

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

GetHeartRate(); //--> Calling the GetHeartRate() subroutine.


}
//________________________________________________________________________________
//
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<

//
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>> 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"

// Defines the PIN used.


#define PulseSensor_PIN 36
#define LED_PIN 23
#define Button_PIN 32

//----------------------------------------Configure OLED screen size in pixels


#define SCREEN_WIDTH 128 //--> OLED display width, in pixels
#define SCREEN_HEIGHT 64 //--> OLED display height, in pixels
//----------------------------------------

//----------------------------------------Declaration for an SSD1306 display


connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//----------------------------------------

//----------------------------------------SSID and PASSWORD of your WiFi network.


const char* ssid = "REPLACE_WITH_YOUR_SSID"; //--> Your wifi name
const char* password = "REPLACE_WITH_YOUR_PASSWORD"; //--> Your wifi password
//----------------------------------------

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;

int cntHB = 0; //--> Variable for counting the number of heartbeats.


boolean ThresholdStat = true; //--> Variable for triggers in calculating
heartbeats.
int BPMval = 0; //--> Variable to hold the result of heartbeats calculation.

int x=0; //--> Variable axis x graph values to display on OLED


int y=0; //--> Variable axis y graph values to display on OLED
int lastx=0; //--> The graph's last x axis variable value to display on the OLED
int lasty=0; //--> The graph's last y axis variable value to display on the OLED

// Boolean variables to start and stop getting BPM values.


bool get_BPM = false;

// Variables for the "timestamp" of the BPM reading.


byte tSecond = 0;
byte tMinute = 0;
byte tHour = 0;

// 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
};
//----------------------------------------

// The variables used to check the parameters passed in the URL.


// Look in the "PageIndex.h" file.
// xhr.open("GET", "BTN_Comd?BTN_Start_Get_BPM=" + state, true);
// For example :
// BTN_Comd?BTN_Start_Get_BPM=START
// PARAM_INPUT_1 = START
const char* PARAM_INPUT_1 = "BTN_Start_Get_BPM";

// Variable declaration to hold data from the web server.


String BTN_Start_Get_BPM = "";

// Initialize JSONVar
JSONVar JSON_All_Data;

// Create AsyncWebServer object on port 80


AsyncWebServer server(80);

// Create an Event Source on /events


AsyncEventSource events("/events");

//
________________________________________________________________________________voi
d GetHeartRate()
void GetHeartRate() {
//----------------------------------------Process of reading heart beat.
unsigned long currentMillisGetHB = millis();

if (currentMillisGetHB - previousMillisGetHB >= intervalGetHB) {


previousMillisGetHB = currentMillisGetHB;

PulseSensorSignal = analogRead(PulseSensor_PIN); //--> Read the PulseSensor's


value. Assign this value to the "Signal" variable.

if (PulseSensorSignal > UpperThreshold && ThresholdStat == true) {


if (get_BPM == true) cntHB++;
ThresholdStat = false;
digitalWrite(LED_PIN,HIGH);
}

if (PulseSensorSignal < LowerThreshold) {


ThresholdStat = true;
digitalWrite(LED_PIN,LOW);
}

DrawGraph(); //--> Calling the DrawGraph() subroutine.

// Enter the data into JSONVar(JSON_All_Data).


JSON_All_Data["heartbeat_Signal"] = PulseSensorSignal;
JSON_All_Data["BPM_TimeStamp"] = tTime;
JSON_All_Data["BPM_Val"] = BPMval;
JSON_All_Data["BPM_State"] = get_BPM;

// Create a JSON String to hold all data.


String JSON_All_Data_Send = JSON.stringify(JSON_All_Data);

// Send Event to browser with JSON_All_Data Every 35 milliseconds.


events.send(JSON_All_Data_Send.c_str(), "allDataJSON" , millis());
}
//----------------------------------------

//----------------------------------------The process for getting the BPM value.


// This timer or Millis is for reading the heart rate and calculating it to get
the BPM value.
// To get the BPM value based on the heart beat reading for 10 seconds.
unsigned long currentMillisResultHB = millis();

if (currentMillisResultHB - previousMillisResultHB >= intervalResultHB) {


previousMillisResultHB = currentMillisResultHB;

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

sprintf(tTime, "%02d:%02d:%02d", tHour, tMinute, tSecond);

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.fillRect(20, 48, 108, 18, BLACK);

display.drawBitmap(0, 47, Heart_Icon, 16, 16, WHITE); //-->


display.drawBitmap(x position, y position, bitmap data, bitmap width, bitmap
height, color)
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(": ");
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;
}
//----------------------------------------

//----------------------------------------Process signal data to be displayed on


OLED in graphic form
int ySignal = PulseSensorSignal;

if (ySignal > 850) ySignal = 850;


if (ySignal < 350) ySignal = 350;

int ySignalMap = map(ySignal, 350, 850, 0, 40); //--> The y-axis used on OLEDs is
from 0 to 40

y = 40 - ySignalMap;
//----------------------------------------

//----------------------------------------Displays the heart rate graph


display.writeLine(lastx,lasty,x,y,WHITE);
display.display();
//----------------------------------------

lastx = x;
lasty = y;

x++;
}
//________________________________________________________________________________

//
________________________________________________________________________________VOI
D SETUP()
void setup() {
// put your setup code here, to run once:

Serial.begin(115200); //--> Set's up Serial Communication at certain speed.


Serial.println();
delay(2000);

analogReadResolution(10);

pinMode(LED_PIN,OUTPUT);
pinMode(Button_PIN, INPUT_PULLUP);

sprintf(tTime, "%02d:%02d:%02d", tHour, tMinute, tSecond);

//---------------------------------------- SSD1306_SWITCHCAPVCC = generate


display voltage from 3.3V internally.
// Address 0x3C for 128x32 and Address 0x3D for 128x64.
// But on my 128x64 module the 0x3D address doesn't work. What works is the 0x3C
address.
// So please try which address works on your module.
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); //--> Don't proceed, loop forever
}
//----------------------------------------

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

//---------------------------------------- Set Wifi to STA mode


Serial.println();
Serial.println("-------------");
Serial.println("WIFI mode : STA");
WiFi.mode(WIFI_STA);
Serial.println("-------------");
//----------------------------------------

delay(100);

//---------------------------------------- Connect to Wi-Fi (STA).


display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Connecting...");
display.display();
delay(1000);

Serial.println("------------");
Serial.println("WIFI STA");
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);

//:::::::::::::::::: The process of connecting ESP32 with WiFi Hotspot / WiFi


Router.
// The process timeout of connecting ESP32 with WiFi Hotspot / WiFi Router is 20
seconds.
// If within 20 seconds the ESP32 has not been successfully connected to WiFi,
the ESP32 will restart.
// I made this condition because on my ESP32, there are times when it seems like
it can't connect to WiFi, so it needs to be restarted to be able to connect to
WiFi.

int connecting_process_timed_out = 20; //--> 20 = 20 seconds.


connecting_process_timed_out = connecting_process_timed_out * 2;
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
if (connecting_process_timed_out > 0) connecting_process_timed_out--;
if (connecting_process_timed_out == 0) {
delay(1000);
ESP.restart();
}
}

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

//---------------------------------------- Handle Web Server


Serial.println();
Serial.println("Setting Up the Main Page on the Server.");
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send_P(200, "text/html", MAIN_page);
});
//----------------------------------------

//---------------------------------------- Handle Web Server Events


Serial.println();
Serial.println("Setting up event sources on the Server.");
events.onConnect([](AsyncEventSourceClient * client) {
if (client->lastId()) {
Serial.printf("Client reconnected! Last message ID that it got is: %u\n",
client->lastId());
}
// send event with message "hello!", id current millis
// and set reconnect delay to 10 second
client->send("hello!", NULL, millis(), 10000);
});
//----------------------------------------

//---------------------------------------- Send a GET request to


<ESP_IP>/BTN_Comd?BTN_Start_Get_BPM=<inputMessage1>
server.on("/BTN_Comd", HTTP_GET, [] (AsyncWebServerRequest * request) {
//::::::::::::::::::
// GET input value on <ESP_IP>/BTN_Comd?BTN_Start_Get_BPM=<inputMessage1>
// PARAM_INPUT_1 = inputMessage1
// BTN_Tare = PARAM_INPUT_1
//::::::::::::::::::

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");
});
//----------------------------------------

//---------------------------------------- Adding event sources on the Server.


Serial.println();
Serial.println("Adding event sources on the Server.");
server.addHandler(&events);
//----------------------------------------

//---------------------------------------- Starting the Server.


Serial.println();
Serial.println("Starting the Server.");
server.begin();
//----------------------------------------

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:

if (digitalRead(Button_PIN) == LOW || BTN_Start_Get_BPM == "START" ||


BTN_Start_Get_BPM == "STOP") {
delay(100);

BTN_Start_Get_BPM = "";
cntHB = 0;
BPMval = 0;
x = 0;
y = 0;
lastx = 0;
lasty = 0;

tSecond = 0;
tMinute = 0;
tHour = 0;

sprintf(tTime, "%02d:%02d:%02d", tHour, tMinute, tSecond);

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

for (byte i = 3; i > 0; i--) {


display.setTextColor(WHITE);
display.setCursor(50, 20);
display.print(i);
display.display();
delay(1000);
display.setTextColor(BLACK);
display.setCursor(50, 20);
display.print(i);
display.display();
}
//----------------------------------------Displays the initial display of BPM
value
display.clearDisplay(); //--> for Clearing the display

display.drawBitmap(0, 47, Heart_Icon, 16, 16, WHITE); //-->


display.drawBitmap(x position, y position, bitmap data, bitmap width, bitmap
height, color)

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

GetHeartRate(); //--> Calling the GetHeartRate() subroutine.


}
//________________________________________________________________________________
//
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<

//
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>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);
//----------------------------------------

//---------------------------------------- Set Wifi to AP mode


Serial.println();
Serial.println("-------------");
Serial.println("WIFI mode : AP");
WiFi.mode(WIFI_AP);
Serial.println("-------------");
//----------------------------------------

delay(100);

//---------------------------------------- Setting up ESP32 to be an Access Point.


Serial.println();
Serial.println("-------------");
Serial.println("Setting up ESP32 to be an Access Point.");
WiFi.softAP(ssid, password); //--> Creating Access Points
delay(1000);
Serial.println("Setting up ESP32 softAPConfig.");
WiFi.softAPConfig(local_ip, gateway, subnet);
Serial.println("-------------");
//----------------------------------------

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);
//
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<

You might also like