How to use blynk in arduino uno with esp8266 for pulse rate

Hi dear please I want help with my project
I use Arduino Uno and ESP8266 wifi module and Easy pulse plugin to measure Heart beat
How I can send BPM to my phone through blynk app
I have code for pulse rate this code used

#define USE_ARDUINO_INTERRUPTS true    // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h>     // Includes the PulseSensorPlayground Library.   

//  Variables
const int PulseWire = 0;       // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int LED13 = 13;          // The on-board Arduino LED, close to PIN 13.
int Threshold = 550;           // Determine which Signal to "count as a beat" and which to ignore.
                               // Use the "Gettting Started Project" to fine-tune Threshold Value beyond default setting.
                               // Otherwise leave the default "550" value. 
                               
PulseSensorPlayground pulseSensor;  // Creates an instance of the PulseSensorPlayground object called "pulseSensor"


void setup() {   

  Serial.begin(19200);          // For Serial Monitor

  // Configure the PulseSensor object, by assigning our variables to it. 
  pulseSensor.analogInput(PulseWire);   
  pulseSensor.blinkOnPulse(LED13);       //auto-magically blink Arduino's LED with heartbeat.
  pulseSensor.setThreshold(Threshold);   

  // Double-check the "pulseSensor" object was created and "began" seeing a signal. 
   if (pulseSensor.begin()) {
    Serial.println("We created a pulseSensor Object !");  //This prints one time at Arduino power-up,  or on Arduino reset.  
  }
}



void loop() {

 int myBPM = pulseSensor.getBeatsPerMinute();  // Calls function on our pulseSensor object that returns BPM as an "int".
                                               // "myBPM" hold this BPM value now. 

if (pulseSensor.sawStartOfBeat()) {            // Constantly test to see if "a beat happened". 
 Serial.println("♥  A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
 Serial.print("BPM: ");                        // Print phrase "BPM: " 
 Serial.println(myBPM);                        // Print the value inside of myBPM. 
}

  delay(20);                    // considered best practice in a simple sketch.

}

I get the BPM in serial Monitor by this code

when I put this code to blynk code not work I use same as dht11 code

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

#define USE_ARDUINO_INTERRUPTS true    // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h>     // Includes the PulseSensorPlayground Library.   

//  Variables
const int PulseWire = 0;       // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int LED13 = 13;          // The on-board Arduino LED, close to PIN 13.
int Threshold = 550;           // Determine which Signal to "count as a beat" and which to ignore.
                               // Use the "Gettting Started Project" to fine-tune Threshold Value beyond default setting.
                               // Otherwise leave the default "550" value. 
                               
PulseSensorPlayground pulseSensor;  // Creates an instance of the PulseSensorPlayground object called "pulseSensor"

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";


char ssid[] = "xxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxxxx";

//#define EspSerialz Serial

#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // TX, RX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);


BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{

//--------------------------------------------
 pulseSensor.analogInput(PulseWire);   
  pulseSensor.blinkOnPulse(LED13);       //auto-magically blink Arduino's LED with heartbeat.
  pulseSensor.setThreshold(Threshold);   

  // Double-check the "pulseSensor" object was created and "began" seeing a signal. 
   if (pulseSensor.begin()) {
  // Serial.println("We created a pulseSensor Object !");  //This prints one time at Arduino power-up,  or on Arduino reset.  
  }


//----------------------------------------------
 int myBPM = pulseSensor.getBeatsPerMinute();  // Calls function on our pulseSensor object that returns BPM as an "int".
                                               // "myBPM" hold this BPM value now. 

if (pulseSensor.sawStartOfBeat())
{            // Constantly test to see if "a beat happened". 
 Serial.println("    ♥  A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
 Serial.print("BPM: ");                        // Print phrase "BPM: " 
 Serial.println(myBPM);                        // Print the value inside of myBPM. 
}

  delay(200);                    // considered best practice in a simple sketch.


//-------------------------------------------------
  Blynk.virtualWrite(V5, myBPM);
 Blynk.virtualWrite(V6, myBPM);

//****************************************  
 //*******************************
}

void setup()
{
  // Debug console
  Serial.begin(19200);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);


  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  Blynk.run();
  timer.run();
}

Please I want help with my project

you call your sensor once a seconde, so you don’t get any value from pulse sensor

1 Like

Thanks Alexis
How I can solve this problem

before sensor reading the Blynk App get Offline

1 Like

I think you need to use attachInterrupt on PIN A0

While I dont have any experience with this sensor. I do see a few things that may be worth looking at. In the sketch you first posted you have:

pulseSensor.analogInput(PulseWire);   
  pulseSensor.blinkOnPulse(LED13);       //auto-magically blink Arduino's LED with heartbeat.
  pulseSensor.setThreshold(Threshold);   

  // Double-check the "pulseSensor" object was created and "began" seeing a signal. 
   if (pulseSensor.begin()) {
  // Serial.println("We created a pulseSensor Object !");  //This prints one time at Arduino power-up,  or on Arduino reset.  

in the setup() , but in your second sketch you moved these items into the timed routine. I would put them back into the setup()

Additionally, in the first sketch you have delay(20), but in the second one it is delay(200) which it quite a large increase. In this case I think you could do away with this delay altogether as the timed routine is kind of doing just that, only allowing the routine to run every 1 second.

You may also want to try to reduce the timer interval from 1 second to maybe .1 seconds, although this will cause the BLYNK value to be updated just as often. So maybe consider a second timer routine for updating the BLYNK Value.

I think the PulseSensorPlayground library already attaches an interrupt to A0 when this code is used…

const int PulseWire = 0;  

pulseSensor.analogInput(PulseWire);

The problem is that the library is setting the callback function that’s called when an interrupt occurs. The library is also using a timer to calculate BPM and returning this back to the code when this library function call is made:

int myBPM = pulseSensor.getBeatsPerMinute();

There’s a bit more detail here:

I don’t think that there’s gong to be a simple solution to making this work with Blynk, unless Blynk is able to live with the if (pulseSensor.sawStartOfBeat()) statement in the void loop and use that to trigger a Blynk.virtualWrite. That would be the first thing I’d try.

The alternative is to not use the pulse sensor library, and handle the interrupts within the code, but then you’d have to keep a rolling BPM count in the code, probably using a 60 object array and a timer that’s called once per second.
The principal is the same as what I do with my rain gauge as described here:

Pete.

1 Like

How to use attachInterrupt on PIN A0 ??
I don’t have idea

it is already used in your code, but I think this code don’t work with blynk

#define USE_ARDUINO_INTERRUPTS true
#include <PulseSensorPlayground.h

A post was split to a new topic: Https://community.blynk.cc/t/turning-old-arduinos-into-perfectly-usable-iot-devices/23456/3