BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Getting BPM using Wemos D1

hey guys…
i need to measure the heart rate BPM to display on my Blynk.
but the sensor doesn’t seems to be responding according to my finger placement. if i put my finger the sensor wont showing anything, the sensor showing some result only when i try tapping the sensor, and it get random BPM.

i got code from https://github.com/vamsikikkuri/Pulse-Sensor IoT/blob/master/Pulse_Sensor_Final.ino.

code tht i use :


#define pulsePin A0
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


char auth[] = "";//yourauthtoken
char ssid[] = "Pocophone";//name of your wifi
char pass[] = "12345678";//password of wifi


int rate[10];                    
unsigned long sampleCounter = 0; 
unsigned long lastBeatTime = 0;  
unsigned long lastTime = 0, N;
int BPM = 0;
int IBI = 0;
int P = 512;
int T = 512;
int thresh = 510;  
int amp = 100;                   
int Signal;
boolean Pulse = false;
boolean firstBeat = true;          
boolean secondBeat = true;
boolean QS = false;    

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  }

void loop() {

  Blynk.run();
  if (QS == true) {
   Serial.println("BPM : "+ String(BPM));
   Blynk.virtualWrite(V1, BPM);  
   QS = false;
   } else if (millis() >= (lastTime + 2)) {
     readPulse(); 
     lastTime = millis();
   }     
}

void readPulse() {

  Signal = analogRead(pulsePin);              
  sampleCounter += 2;                           
  int N = sampleCounter - lastBeatTime;   

  detectSetHighLow();

  if (N > 250) {  
    if ( (Signal > thresh) && (Pulse == false) && (N > (IBI / 5) * 3) )
      pulseDetected();
  }

  if (Signal < thresh && Pulse == true) {  
    Pulse = false;
    amp = P - T;
    thresh = amp / 2 + T;  
    P = thresh;
    T = thresh;
  }

  if (N > 2500) {
    thresh = 510;
    P = 512;
    T = 512;
    lastBeatTime = sampleCounter;
    firstBeat = true;            
    secondBeat = true;           
  }

}

void detectSetHighLow() {

  if (Signal < thresh && N > (IBI / 5) * 3) {
    if (Signal < T) {                       
      T = Signal;                         
    }
  }

  if (Signal > thresh && Signal > P) {    
    P = Signal;                           
  }                                       

}

void pulseDetected() {
  Pulse = true;                           
  IBI = sampleCounter - lastBeatTime;     
  lastBeatTime = sampleCounter;           

  if (firstBeat) {                       
    firstBeat = false;                 
    return;                            
  }
  if (secondBeat) {                    
    secondBeat = false;                
    for (int i = 0; i <= 9; i++) {   
      rate[i] = IBI;
    }
  }

  word runningTotal = 0;                   

  for (int i = 0; i <= 8; i++) {          
    rate[i] = rate[i + 1];            
    runningTotal += rate[i];          
  }

  rate[9] = IBI;                      
  runningTotal += rate[9];            
  runningTotal /= 10;                 
  BPM = 60000 / runningTotal;         
  QS = true;

}

thank you
PS : sorry if my english bad

Please edit your post to add triple backticks before and after your code, so that it displays correctly.
Triple backticks look like this:
```

Pete.

1 Like
boolean QS = false;    // <<<<<<

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  }

void loop() {

  Blynk.run();
  if (QS == true) { // <<<<<<<<
   Serial.println("BPM : "+ String(BPM));
   Blynk.virtualWrite(V1, BPM);  
   QS = false;
   } else if (millis() >= (lastTime + 2)) {
     readPulse(); 
     lastTime = millis();
   }     
}

QS == FALSE, your code is never going to execute.

  1. You need to set QS = true somewhere.
  2. You need to remove that virtualWrite from void loop or you will flood the server connection.