Reading data from Pulse sensor using Particle IDE

To me it doesn't make sense, but the sample builds because it does not include the header file but only uses extern references and forward declarations.

When you remove both your #include statements your project builds without error - but I couldn't vouch for it to work as intended.

@peekay123, this is some puzzling behaviour IMO.

BTW @naveed, didn't I in some other thread point out that Spark.variable("getpos", &bpmm, INT); is deprecated and should now be written as Particle.variable("getpos", bpmm);?

1 Like

@ScruffR, that IS weird. The include file has guards to prevent multiple inclusions and you are right, if I remove the automatic includes in the app, it compiles!

@naveed, give it a shot without the automatic includes and see if the code runs as expected.

@peekay123 and @ScruffR you both were right when i removed the automatic include statements. the code is compiled without any error. It seems strange and i am wondering that how the functions in code will as its defined in header files if we remove these statements. @ScruffR you are right the code compiled and when i flashed to my device it give 0 value for BPM.I also change the Spark statements to particle and include the analogRead() the code is gievn below. I didn’t change it before as i thought to compile first the program successfully and then i will change it.

β€˜β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™ code Start here β€˜β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™

// #include statement was automatically added by the Particle IDE.
//#include<SparkIntervalTimer.h>

// This #include statement was automatically added by the Particle IDE.
// #include<PulseSensor_Spark.h>

void interruptSetup(void);
void serialOutput();
void serialOutputWhenBeatHappens();
void sendDataToSerial(char symbol, int data );
void ledFadeToBeat();
void arduinoSerialMonitorVisual(char symbol, int data );

extern int pulsePin;
extern int blinkPin;
extern volatile int BPM;;
extern volatile int Signal;;
extern volatile int IBI;
extern volatile boolean Pulse;
extern volatile boolean QS;
const int numReadings = 10;
int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0; 
int bpmm;
extern int fadePin;
extern int fadeRate;

void setup(){
     
	Particle.variable("getpos", bpmm);
	//pinMode(blinkPin,OUTPUT);         // pin that will blink to your heartbeat!
	//pinMode(fadePin,OUTPUT);          // pin that will fade to your heartbeat!
     analogRead(fadePin);
	Serial.begin(115200);             // we agree to talk fast!
	interruptSetup();                 // sets up to read Pulse Sensor signal every 2mS
	for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
    Particle.publish("getpos", String(bpmm));
  }
}

void loop(){
    
     // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = BPM;
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

  // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }

  // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits
    bpmm = average;

}

β€˜β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™ code ENDS here β€˜β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™

************* OUTPUT***********************

Can you please suggest that how to get the correct pulse rate as i am getting 0 ?

Many Thanks

Regards
Naveed

Since the code features variables marked as extern and forward declarations (aka prototypes) of these functions the compiler relies on the infos it has and trusts that during linkage the actual "objects" will be made available. And as it seems the cpp file of the library is actually compiled as a seperate module and does provide the needed "objects" to link these "dangling" calls to.

The zero output is also no surprise as I can't see you setting the bpmm variable prior your one and only Particle.publish() statement in setup().
Just because your Particle.variable() and the Particle.publish() statements share the same name getpos they are still not the same thing.

What do you get when actually requesting the Particle.variable()?

BTW, your Particle.publish()es in setup() also violate the rate limit.

@ScruffR Thanks for detailed answer. I am trying to read the senor value from pulse sensor. So the particle.Variable() is used to get value from Pulse sensor and Particle.publish () is used to publish that value on cloud.

When i write the Particle.publish in loop() it still give 0 values.

Please guide me if i am wrong.

Thanks

You can request the value of the Particle.variable() via the console too (https://console.particle.io/devices/ then click on the device name)
Or you get it via this endpoint
https://api.particle.io/v1/devices/<yourDeviceID>/getpos?access_token=<yourAccessToken>

I’d also try to get the raw readings first before going into averaging to see if you get anything at all.

@ScruffR Ok Thanks and I also tried to get value via this endpoint as you mention and get the following output. is still shows 0.

{β€œcmd”:β€œVarReturn”,β€œname”:β€œgetpos”,β€œresult”:0,β€œcoreInfo”:{β€œlast_app”:"",β€œlast_heard”:β€œ2018-01-17T11:43:50.393Z”,β€œconnected”:true,β€œlast_handshake_at”:β€œ2018-01-17T11:40:02.888Z”,β€œdeviceID”:"*********************",β€œproduct_id”:88}}

Thanks

@naveed, I suggest you go back to the original example to test. Then modify the example to suit your needs including adding Particle.variable() and Particle.publish().

@peekay123 Thanks for suggestion, yes i run the original example and it compiles without any error. Then add the Particle.variable() and Particle.publish() to get the values and publish it but it gives 0. I am not sure what i am doing wrong as i am reading value from A0 of the sensor pen as shown in first figure of this thread.

I also wrote simple code to make sure about raw signal values coming from the pulse sensor and it give me the following output.
*************** code*************************

int sensorValue = 0;
double sensorPin = A0;
// variable to store the value coming from the sensor
double count=0;

void setup() {
  // declare the ledPin as an OUTPUT:
  //pinMode(sensorPin, INPUT);
  
  Particle.variable("ECG",sensorPin);
  //Serial.begin(9600);
}

void loop() {
    Particle.publish("BPM", String(sensorPin));
}

*********** CODE END*******************

β€˜β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™β€™ OUTPUT’’’’’’’’’’’’’’’’’’’’’’’’’’’’’

What you suggest where should i add the Particle.variable() and Particle.publish() to get the desired output.

Thanks

Naveed

This is incorrect. SensorPin should be typed as an int, not a double. Also, there’s no where in that code where you read the A0 pin.

@Ric, corrected. Thanks

@naveed, what I was asking is, does your sensor work with the example code, not just compile. Did you actually run that code and look at the output on a serial monitor on your PC?

If the above is no, then I suggest you get that working first. Then you need to understand the actual code. You will notice a function called serialOutputWhenBeatHappens() which, without modification prints a message to the serial monitor for every time a heartbeat is detected, which could be quite fast. You will notice in your code that you call Particle.publish() VERY FAST in loop(), which will easily violate the one-publish-per-second limit (or four in one second with a pause of 4 seconds).

Using Particle.variable() doesn’t have this problem since nothing is published, it is only queried. You could easily create a string Particle.variable() to contain both the BPM and the IBI. Or create a Particle.variable() for each. You can then query these variables as @ScruffR had indicated above.

Don’t rush slapping code together. Take the time to read documentation and experiment. Then come back for more constructive help.

1 Like

@peekay123 , Ok Thanks for your detailed answer. I will try to explore it in more in depth for understanding the code and its output.

Could you please explain that how i can see the output using serial monitor as i read the documentation and search for internet but unable to find any help. I am using Red Bear duo board for flashing the code. so how to can i look at the output on a serial monitor on my PC?

Thanks

Regards
Naveed

@peekay123, Thanks for valuable suggestions. you were right as per you suggestion i try to see the output on serial monitor and run a simple example to check that the serial monitor is working properly and it works fine but when i run the pulse sensor code it doesn’t show any value on serial monitor as shown in the image.

i am thinking it may be because of two problems.
First, I am using red bear duo board for flashing the code , so i am thinking that it may be because of using red bear duo board instead of photon ?
Secondly, as I have removed the automatic libraries and may because of that the code is not properly executed.

Please correct me if i am wrong and if you could suggest what to do to get the desired output?

Thanks

@ScruffR @peekay123 , As discussed earlier in discussion that i was working on reading heart data from pulse sensor. I was able to read the raw sensor data using serial monitor and also publish it as given in figure

but BPM is not calculated and exploring the code i think that threshold, P and T value is kept very high in the header file PulseSenor_Spark.h which is 2048. However,I think if these values change to 512 or 1024 then it will calculate BPM?. please correct me if i am wrong that how is this the problem and secondly how i will change this value and try the output.

Thanks and wait for your response.

Naveed

@naveed, add extern volatile int thresh; with the other extern declarations in your code. Then you can set it to whatever value you want in setup().

I need to get one of these sensors and create a proper class library for it. Have you looked at the MAX30100 Pulse Oximeter and Heart-Rate Sensor? It is well priced and does a lot more!

@peekay123 Thanks very much for your reply i will try to add this into my code and see how it give the output.

Have you looked at the MAX30100 Pulse Oximeter and Heart-Rate Sensor?

No, i didn’t look it but i will explore it and will try to use it. Thanks for your suggestion.

Regards
Naveed

1 Like

@peekay123 I have add extern volatile int thresh; and also set it into setup() and get the output on serial monitor as shown in image


The value start with S is rawSignal , the value start with B is heartbeat and Q is IBI value. The value is not recalculating and give constant value after each rawSignal value start with S.

I am also sharing the Revision for checking the code for checking https://go.particle.io/shared_apps/5a734700c427270736000a6d

Can you please suggest what i am doing wrong that i am not getting the correct values?

THanks

Regards
Naveed

@naveed, go back to the example code, without any modifications to see if you getting the correct values. If everything looks good, we can go from there.

@peekay123 Yes , I change it to the original example code and now it only gives the rawSignal values in serial monitor .