Connecting arduino to cayenne using esp8266wifi

@shramik_salgaonkar

i get this error massage

sorry, missed one line. can you add it at the top
#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math.

@shramik_salgaonkar
I did not get any reading on the serial monitor

change

const int PulseWire = 0;       // PulseSensor PURPLE WIRE connected to ANALOG PIN 0

to

const int PulseWire = A0;       // PulseSensor PURPLE WIRE connected to ANALOG PIN 0

@shramik_salgaonkar
I am getting BPM=0 only

try this code:-

#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 = A0;       // 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(9600);          // 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.

}

@shramik_salgaonkar
this is what i get

not sure what is the issue, you can google search on how to get BPM on nodemcu and once done we can move to cayenne code.

@shramik_salgaonkar
i have this code :
#define USE_ARDUINO_INTERRUPTS false
#include <PulseSensorPlayground.h>

/*
The format of our output.

Set this to PROCESSING_VISUALIZER if you’re going to run
the Processing Visualizer Sketch.
See GitHub - WorldFamousElectronics/PulseSensor_Amped_Processing_Visualizer: Processing code for pulse wave visualization

Set this to SERIAL_PLOTTER if you’re going to run
the Arduino IDE’s Serial Plotter.
*/
const int OUTPUT_TYPE = SERIAL_PLOTTER;

/*
Pinout:
PULSE_INPUT = Analog Input. Connected to the pulse sensor
purple (signal) wire.
PULSE_BLINK = digital Output. Connected to an LED (and 220 ohm resistor)
that will flash on each detected pulse.
PULSE_FADE = digital Output. PWM pin onnected to an LED (and resistor)
that will smoothly fade with each pulse.
NOTE: PULSE_FADE must be a pin that supports PWM.
If USE_INTERRUPTS is true, Do not use pin 9 or 10 for PULSE_FADE,
because those pins’ PWM interferes with the sample timer.
*/
const int PULSE_INPUT = A0;
const int PULSE_BLINK = 13; // Pin 13 is the on-board LED
const int PULSE_FADE = 5;
const int THRESHOLD = 550; // Adjust this number to avoid noise when idle

/*
samplesUntilReport = the number of samples remaining to read
until we want to report a sample over the serial connection.

We want to report a sample value over the serial port
only once every 20 milliseconds (10 samples) to avoid
doing Serial output faster than the Arduino can send.
*/
byte samplesUntilReport;
const byte SAMPLES_PER_SERIAL_SAMPLE = 10;

/*
All the PulseSensor Playground functions.
*/
PulseSensorPlayground pulseSensor;

void setup() {
/*
Use 115200 baud because that’s what the Processing Sketch expects to read,
and because that speed provides about 11 bytes per millisecond.

 If we used a slower baud rate, we'd likely write bytes faster than
 they can be transmitted, which would mess up the timing
 of readSensor() calls, which would make the pulse measurement
 not work properly.

*/
Serial.begin(115200);

// Configure the PulseSensor manager.
pulseSensor.analogInput(PULSE_INPUT);
pulseSensor.blinkOnPulse(PULSE_BLINK);
pulseSensor.fadeOnPulse(PULSE_FADE);

pulseSensor.setSerial(Serial);
pulseSensor.setOutputType(OUTPUT_TYPE);
pulseSensor.setThreshold(THRESHOLD);

// Skip the first SAMPLES_PER_SERIAL_SAMPLE in the loop().
samplesUntilReport = SAMPLES_PER_SERIAL_SAMPLE;

// Now that everything is ready, start reading the PulseSensor signal.
if (!pulseSensor.begin()) {
/*
PulseSensor initialization failed,
likely because our Arduino platform interrupts
aren’t supported yet.

   If your Sketch hangs here, try changing USE_PS_INTERRUPT to false.
*/
for(;;) {
  // Flash the led to show things didn't work.
  digitalWrite(PULSE_BLINK, LOW);
  delay(50);
  digitalWrite(PULSE_BLINK, HIGH);
  delay(50);
}

}
}

void loop() {

/*
See if a sample is ready from the PulseSensor.

 If USE_INTERRUPTS is true, the PulseSensor Playground
 will automatically read and process samples from
 the PulseSensor.

 If USE_INTERRUPTS is false, this call to sawNewSample()
 will, if enough time has passed, read and process a
 sample (analog voltage) from the PulseSensor.

/
if (pulseSensor.sawNewSample()) {
/

Every so often, send the latest Sample.
We don’t print every sample, because our baud rate
won’t support that much I/O.
*/
if (–samplesUntilReport == (byte) 0) {
samplesUntilReport = SAMPLES_PER_SERIAL_SAMPLE;

  pulseSensor.outputSample();

  /*
     At about the beginning of every heartbeat,
     report the heart rate and inter-beat-interval.
  */
  if (pulseSensor.sawStartOfBeat()) {
    pulseSensor.outputBeat();
  }
}

/*******
  Here is a good place to add code that could take up
  to a millisecond or so to run.
*******/

}

/******
Don’t add code here, because it could slow the sampling
from the PulseSensor.
******/
}
and this is the output


i don’t now which value OF the three is the BPM

in a guess, the 102 looks closet value for BPM can be :sweat_smile:
but how you will get extract that value??

@shramik_salgaonkar
:sweat_smile::sweat_smile::sweat_smile: I dont now

@shramik_salgaonkar
I have found two code the first one :
int UpperThreshold = 518;
int LowerThreshold = 490;
int reading = 0;
float BPM = 0.0;
bool IgnoreReading = false;
bool FirstPulseDetected = false;
unsigned long FirstPulseTime = 0;
unsigned long SecondPulseTime = 0;
unsigned long PulseInterval = 0;

void setup(){
  Serial.begin(9600);
}

void loop(){

  reading = analogRead(0); 

  // Heart beat leading edge detected.
  if(reading > UpperThreshold && IgnoreReading == false){
    if(FirstPulseDetected == false){
      FirstPulseTime = millis();
      FirstPulseDetected = true;
    }
    else{
      SecondPulseTime = millis();
      PulseInterval = SecondPulseTime - FirstPulseTime;
      FirstPulseTime = SecondPulseTime;
    }
    IgnoreReading = true;
  }

  // Heart beat trailing edge detected.
  if(reading < LowerThreshold){
    IgnoreReading = false;
  }  

  BPM = (1.0/PulseInterval) * 60.0 * 1000;

  Serial.print(BPM);
  Serial.println(" BPM");
  Serial.flush();

  // Please don't use delay() - this is just for testing purposes.
  delay(50);  
}

It give me this reading :


NOTE : This code give me reading every 50ms .
The second one :
int UpperThreshold = 518;
int LowerThreshold = 490;
int reading = 0;
float BPM = 0.0;
bool IgnoreReading = false;
bool FirstPulseDetected = false;
unsigned long FirstPulseTime = 0;
unsigned long SecondPulseTime = 0;
unsigned long PulseInterval = 0;
const unsigned long delayTime = 10;
const unsigned long delayTime2 = 1000;
const unsigned long baudRate = 9600;
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;

void setup(){
Serial.begin(9600);

Serial.begin(baudRate);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}

void loop(){

// Get current time
unsigned long currentMillis = millis();

// First event
if(myTimer1(delayTime, currentMillis) == 1){

reading = analogRead(0); 

// Heart beat leading edge detected.
if(reading > UpperThreshold && IgnoreReading == false){
  if(FirstPulseDetected == false){
    FirstPulseTime = millis();
    FirstPulseDetected = true;
  }
  else{
    SecondPulseTime = millis();
    PulseInterval = SecondPulseTime - FirstPulseTime;
    FirstPulseTime = SecondPulseTime;
  }
  IgnoreReading = true;
  digitalWrite(LED_BUILTIN, HIGH);
}

// Heart beat trailing edge detected.
if(reading < LowerThreshold && IgnoreReading == true){
  IgnoreReading = false;
  digitalWrite(LED_BUILTIN, LOW);
}  

// Calculate Beats Per Minute.
BPM = (1.0/PulseInterval) * 60.0 * 1000;

}

// Second event
if(myTimer2(delayTime2, currentMillis) == 1)
{
Serial.print(BPM);
Serial.println(" BPM");
Serial.flush();
}
}

// First event timer
int myTimer1(long delayTime, long currentMillis){
if(currentMillis - previousMillis >= delayTime){previousMillis = currentMillis;return 1;}
else{return 0;}
}

// Second event timer
int myTimer2(long delayTime2, long currentMillis){
if(currentMillis - previousMillis2 >= delayTime2){previousMillis2 = currentMillis;return 1;}
else{return 0;}
}
It give me this reading but when i but my finger on the sensor it give me 3000.0 BPM.


NOTE: this code give me the reading every 1 sec.

@shramik_salgaonkar
I think i should use the first one

those BPM values dont seems to be valid.

@shramik_salgaonkar
I have tried sum code thy give me the sam values as in the firs code

idk, looks like the values are wrong. Anyways you can use Cayenne-MQTT-Arduino/ESP8266.ino at master · myDevicesIoT/Cayenne-MQTT-Arduino · GitHub to send the BPM value and change the Cayenne.loop() to Cayenne.loop(10)

@shramik_salgaonkar
I well try that and send you the result

@shramik_salgaonkar
I don’t now how to add my code to https://github.com/myDevicesIoT/Cayenne-MQTT-Arduino/blob/master/examples/Connections/ESP8266/ESP8266.ino could you help me to do it