Event Publishing Inconsistent

I am currently working on a project that involves an IoT button based on a Photon. The code is extremely simple.

int LED = D7;
int BUTTON = D2;

void callCloudService(void);

void setup() {
    pinMode(LED, OUTPUT);
    pinMode(BUTTON, INPUT);
    
    // Go into a low power mode until the button is pressed
    System.sleep(BUTTON, RISING);
}

void loop() {
    // Make a cloud service call
    callCloudService();
    
    // Go back to sleep
    System.sleep(BUTTON, RISING);
}


void callCloudService(){
    pinSetFast(LED);
    while(!Particle.publish("bring_the_heat", PRIVATE));
    pinResetFast(LED);
}

The behavior I am seeing when I press the button is the Photon wakes up and connects to the cloud and I see a very very brief flash of the LED which I expect. However the event only gets published about once or twice per 10 button presses. It’s very inconsistent. Sometimes it will work a few times in a row and sometimes I will press it 10 or 15 times before the event actually fires according to monitoring the stream both in the Console as well as via a terminal window. Here is the Console log after pressing the button 5 times after initial boot.

I notice that in the scenarios where it works I also get a device_went_offline event. Am I doing something wrong (or not doing something) in the code that would cause this behavior?

How well does it work if you're not sleeping and having to wait for the WiFi to connect to the Particle servers?

Also, note the max publish rates taken from the Docs:

Calling Particle.publish() when the device is not connected to the cloud will not result in an event being published. This is indicated by the return success code of false.

For the time being there exists no way to access a previously published but TTL-unexpired event.

NOTE: Currently, a device can publish at the 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.

What system version are you running?
In previous versions you needed to have a 5sec wait loop to allow the publish to get delivered before cutting the connection.
When Particle.publish() returns true that’s only signalling that the event was queued but doesn’t tell if it was delivered already.

Thanks @ScruffR and @RWB. I’m definitely not hitting the publish rate limits. I’m running 0.5.3. I tried looking through the code and it looked like it was calling a blocking_send which made me think it would actually deliver the message before returning. I didn’t dig all the way in. So is this fixed in a pre-release version that I could update to or is the best option to throw a 5 second delay in?

Thanks for the help.

I’d try a 5sec wait to see if this fixes the problem first, to see if this actually is the cause - after that you could try 0.6.0-rc.2 which definetly has the fix on board (but I thought 0.5.3 already had that too).
I’d also not use a delay(5000) but rather this

  for (uint32_t ms=millis(); millis()-ms < 5000; Particle.process());

There is an open issue tho’ that might also play a role here

Thanks @ScruffR I’ll try the 5 second delay on 0.5.3 to confirm but then I’ll update to 0.6.0-rc.2. I’ll post results here and mark as solved if all goes as expected.

2 Likes

Let us know!

@KyleG @ScruffR Sorry for the delay in testing this. I confirmed that adding the code suggested by @ScruffR fixes the issue and allows the publish to succeed on 0.5.3. I then updated to 0.6.0-rc.2 and removed the delay and it still doesn’t work. I’m fine leaving the delay in for now but if it’s believed that this issue is fixed in the latest pre-release version of the firmware I am not seeing that behavior.

2 Likes

Yup, that’s the reason for above issue.
The fix is already in 0.5.3, but it’s not the full cure to the problem. Obviously it’s a combination of factors at play here and fix only removed a part of it.