Connecting arduino to cayenne using esp8266wifi

@shramik_salgaonkar
thank you
i also want to ask you if i want to connect dht11 to the platform how can i do that without stopping the BPM sensor from sending the data to the platform

did the above code work? you can send both data.

@shramik_salgaonkar
it did not work ,it show 0 value

can you share the code which you used.

@shramik_salgaonkar
#define pulsePin A0
#define CAYENNE_PRINT Serial
#define CAYENNE_DEBUG
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid = “amin”;
char wifiPassword = “amin2019”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “4eaa6630-fe8f-11ea-883c-638d8ce4c23d”;
char password = “e26fb00b05e52f473d82b331a0918f9d2d346b91”;
char clientID = “0b74d5c0-fff8-11ea-a67f-15e30d90bbf4”;

const int postingInterval = 10 * 1000;

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 = 512;
int amp = 100;
int Signal;
boolean Pulse = false;
boolean firstBeat = true;
boolean secondBeat = true;
boolean QS = false;

void setup() {
Serial.begin(115200);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop() {
Cayenne.loop();
if (QS == true) {
Serial.println("BPM: " + String(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 = 512;
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;
Serial.println(BPM);
QS = true;

}
CAYENNE_OUT_DEFAULT()
{
Cayenne.virtualWrite(0, BPM);
}
this is the code i used

Did you try this?

@shramik_salgaonkar
I tried it and I get these results

which code is it? do you just need the raw analog values?

@shramik_salgaonkar
this code:

int PulseSensorPurplePin = 0; // Pulse Sensor PURPLE WIRE connected to ANALOG PIN 0
int LED13 = 13; // The on-board Arduion LED

int Signal; // holds the incoming raw data. Signal value can range from 0-1024
int Threshold = 550; // Determine which Signal to “count as a beat”, and which to ingore.

// The SetUp Function:
void setup() {
pinMode(LED13,OUTPUT); // pin that will blink to your heartbeat!
Serial.begin(9600); // Set’s up Serial Communication at certain speed.

}

// The Main Loop Function
void loop() {

Signal = analogRead(PulseSensorPurplePin); // Read the PulseSensor’s value.
// Assign this value to the “Signal” variable.

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

if(Signal > Threshold){ // If the signal is above “550”, then “turn-on” Arduino’s on-Board LED.
digitalWrite(LED13,HIGH);
} else {
digitalWrite(LED13,LOW); // Else, the sigal must be below “550”, so “turn-off” this LED.
}

delay(10);

}

?

@shramik_salgaonkar
No,I need to have the BPM as a value

then there is an example for BPM in the library.

@shramik_salgaonkar
I used 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 i get the next result

was it so difficult to find the basic BPM code? PulseSensorPlayground/Getting_BPM_to_Monitor.ino at master · WorldFamousElectronics/PulseSensorPlayground · GitHub

@shramik_salgaonkar
when i up lode this code to the nodemcu i get this readings

press the reset button on the nodemcu.

@shramik_salgaonkar
when i pressed it i get this

are you sure the code and serial monitor baud rate are 9600? can you try uploading the code again.

@shramik_salgaonkar
yes,i am sure
this is the 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 = 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(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(":heart: 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.

try this:

#include <PulseSensorPlayground.h>     // Includes the PulseSensorPlayground Library.   

//  Variables
const int PulseWire = 0;       // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
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.setThreshold(Threshold);   
 Serial.println("START ");                        // Print phrase "BPM: " 
  // 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 
 Serial.print("BPM: ");                        // Print phrase "BPM: " 
  Serial.println(myBPM);                        // Print the value inside of myBPM. 


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.

}