Spark.publish stops sending events after a short while [SOLVED]

I have two Photons that need to talk to each other. I’m using Spark.publish and Spark.subscribe in both Photon for doing so. I’m sending a message of 10 bytes every two seconds.

This setup works for approx. 5 messages, then either they stop receiving or, once, they both crashed (red SOS blink).

This is my handler function.

void audioSequenceReceiver(const char *event, const char *data){
int i=0;
Serial.println("Audio sequence received: ");
while (data[i]){
  audioOutSequence[i] = data[i];
  Serial.print((int)data[i]);
  Serial.print(" - ");
  i++;
}
Serial.println("");

}

Any idea how to solve the issue? Thanks

@amicoleo, first, I would say that you need to treat a Spark.function() like an interrupt service routine and keep it as tight as possible, including not having Serial.print() call. In your function, you set a flag that is examined in loop(). Also, you may want to use strcpy(audioOutSequence, data) instead of the while(), assuming your declaration of audioOutSequence[] is sized large enough to handle the entire data!

Whats the reason behind keeping a Spark.function() tight? Can you explain?

@Carsten4207, it’s basically a callback from the system firmware. So user code gets interrupted for the duration of that call. If it delays too long, you’ll also lose your cloud connection! :smile:

1 Like

not to be argumentative, but it seems like very little happening in the OP’s callback that would affect his communication (from my experience doing a lot more inside one of these functions).

OP may wish to try backing off to every 5 seconds and see if it stabilizes, it seems to me that perhaps normal Wifi/Internet latency may be part of the issue here.

@BulldogLowell, argumentative??? Dude, this is what these discussions are all about! I should have said that the code should not be an issue but the recommendations were good practice. However, the question still remains whether the audioOutSequence[] array was adequately sized or not. :wink:

1 Like

It looks like @amicoleo could be trying to send binary data via publish/subscribe. That won’t work and the data needs to ASCII encoded in some way.

So is the published data ASCII or binary?

1 Like

@bko, no I’m sending a char array. Thanks.

@peekay123 , yeah, the SOS message was due to some problem to the size of input data and audioOutSequence.

Lowering the rate the function Spark.publish() is called I could have the two communicate constantly, the connection is no longer lost.

Cheers all!

1 Like