Photon breathes green while sending pulse sensor data to thingspeak

Hi, I’m working a wearable project for measuring stress using GSR and PulseSensor and upload data to a thingspeak channel. While the @peekay123 's pulse sensor library works great and I am able to view the BPM in the Particle Dashboard using Particle.publish(). The thingspeak code while working great for four uploads (15sec interval) the photon begins to breathe green and stops upload to the thingspeak server. I put the photon manually to safe mode (purple) to reupload code.
I’ve tried such as this and by adding photon.process() in the void loop.
I’m new to IoT and would be grateful for quick help on this matter I have added the code below.

#include "ThingSpeak/ThingSpeak.h"
#include "PulseSensor_Spark/SparkIntervalTimer.h"
const int numReadings = 10;

int readings[numReadings];     
int readIndex = 0;              
int total = 0;                 
int average = 0;              

int inputPin = A0;

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

TCPClient client;
unsigned long myChannelNumber = 105763;
const char * myWriteAPIKey = "Hidden";
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;

static boolean serialVisual = false;  

extern int fadePin;
extern int fadeRate;

void setup(){
	pinMode(blinkPin,OUTPUT);        
	pinMode(fadePin,OUTPUT);         
//	Serial.begin(115200);           
	for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
  } 
    interruptSetup();               
	  ThingSpeak.begin(client);

}



//  Where the Magic Happens
void loop(){
//Particle.process();
    Serial.println(BPM);
	Particle.publish("BPM:",String(BPM));

  readings[readIndex] = analogRead(inputPin);
  total = total + readings[readIndex];
  readIndex = readIndex + 1;

  if (readIndex >= numReadings) {
    readIndex = 0;
  }

  average = total / numReadings;

   ThingSpeak.setField(1,average);
    ThingSpeak.setField(2,BPM);
   
  ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);  


}

You might want to take a look at this:
“NOTE: Currently, a device can publish at rate of about 1 event/sec, with bursts of up to 4 allowed in 1 second. Back to back burst of 4 messages will take 4 seconds to recover.”

https://docs.particle.io/reference/firmware/photon/#particle-publish-

Also, Particle.process is not going to do much if it’s commented out :wink:

1 Like

@viggi1000, as @Moors7 pointed out, there is a publish rate limit so you either need to put a delay around you publish or remove it. A flashin green LED means you lost cloud connectivity but not WiFi. As for ThingSpeak, it is assumed that the call is not blocking for any significant amount of time.

You may want to consider using SYSTEM_THREAD(ENABLED) and possibly SYSTEM_MODE(SEMI_AUTOMATIC) to manage the Photon’s connectivity. :wink:

2 Likes

Guys, sorry I meant to comment out the Particle.publish() part, The problem is with thingspeak where it initially pushes data but stops later when it starts breathing green.

@peekay123 Any pointers to where I should modify the code to include System_thread(enabled)?
Thanks for the help guys!

@viggi1000, at the top of your code, after the #includes. Follow the documentation link I provided! :wink:

1 Like

Thanks all, Will let you know how it goes!

1 Like
    // This #include statement was automatically added by the Particle IDE.
#include "ThingSpeak/ThingSpeak.h"


#include "PulseSensor_Spark/SparkIntervalTimer.h"

void interruptSetup(void);
unsigned int myChannelNumber =""; // replace with your ChannelID
const char * myWriteAPIKey = ""; // replace with your WriteAPIKey
SYSTEM_MODE(SEMI_AUTOMATIC);

TCPClient client;


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;
void setup(){
	pinMode(blinkPin,OUTPUT);         // pin that will blink to your heartbeat!
	ThingSpeak.begin(client);
	interruptSetup();                 // sets up to read Pulse Sensor signal every 2mS 
	delay(5000);
}



void loop(){
     WiFi.off();
    delay(10);
    WiFi.on();
    WiFi.connect();
    Particle.connect();    
    Particle.process();

//    Particle.publish("BPM", String(BPM));
    ThingSpeak.setField(1,BPM);
    ThingSpeak.setField(2,analogRead(A0));
    ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);  
   
    delay(10000);

 /*  if(!Particle.connected())
  {
  WiFi.off();
  delay(10);
  WiFi.on();
  WiFi.connect();
  Particle.connect();
  } */

I sort of had to drop the project due to my finals and I got back again to it a few days ago and I’m fumbling with the same problem, I’ve followed @peekay123 's advice and tested out both SYSTEM_THREAD(ENABLED) and SYSTEM_MODE(SEMI_AUTOMATIC) but both don’t work.

SYSTEM_THREAD(ENABLED) causes the data to stop being pushed about one time (No breathing green LED though) and it cant be reprogrammed making me boot it to safe mode to program.
Where as SYSTEM_MODE(SEMI_AUTOMATIC) yeilds in the same breathing green LED after pushing the data 4 times to Thingspeak, I even tried disconnecting and reconnecting the WiFi every 10 seconds but it only extends the inevitable green breathing LED to 10 data pushes to Thingspeak.

I’ve toyed around with other sensors in Thingspeak and I’ve never faced any problem of breathing green LED.
Only the pulse sensor code seems to do it :frowning:
I have no other alternatives to post this to the cloud, The ESP consumes too much power for my wearable and the pulse sensor code does not work with the MKR1000.
@peekay123, Can you please try pushing the pulse sensor data to thingspeak to see what I did wrong?

Sorry for the trouble!

@viggi1000, I’m not sure what you are trying to achieve with the WiFi.on and off stuff in loop but you need to wait for wifi to connect before trying to connect to ThingSpeak. You can do that use waitUntil():

WiFi.on();
WiFi.connect();
waitUntil(WiFi.ready);   // This waits until wifi is connected
... reset of code

If you want to also connect to the Particle cloud, you can skip WiFi.connect() and call Particle.connect() which will connect to WiFi first the to the cloud:

WiFi.on();
Particle.connect();
waitUntil(Particle.connected);
... rest of code

I can’t test sending pulse data to ThingSpeak because I neither have a pulse sensor or a ThingSpeak account!