Publishing events with large delay

I have encountered a problem recently. I have a long running blocking calculation/measurement on Photon and publish Cloud events before and after. It seems to work fine until the calculation time starts to exceed 15 sec - when this happens the event after is not getting published, instead I can see a flurry of Particle system events (status, diagnostics and device updates). My two or three events following the long calculation disappearing completely. System events come at a time when I expect my events to be published - almost like they are replacing them. Everything else works fine - calculation finishes and results are stored in variables. Anyone has a clue of what is going on? I am on firmware v1.4.4

Stick a few Particle.process(); statements in your calculation and see if that doesn’t fix things.

The buffer size is limited, so it’s possible that your message is getting overwritten before being sent?

1 Like

Yes, when the device doesn’t check in at least once per 10 seconds the connection is considered broken and extra steps to reestablish need to be taken.
Your events will get lost when publishing while this happens.

As already said, you can use Particle.process() during long running tasks.
Alternatively you can opt for SYSTEM_THREAD(ENABLED).

However, the generally better approach is to not have any long blocking tasks to start with. Maybe the calculation can be restructured to be less blocking.

I run Particle.process() after the measurement completes and after that do Particle.publish(). The publishing gets lost anyway.

Are you calling it repeatedly during the long running calculation?
Have you tried

No I cannot. If I would I would not need to resort to events since remote app can periodically check some published variables that state intermediate measurements.

I tried running system thread and periodically call Particle.process() but it distorts the calculations.

I am at this point more interested in guaranteed was of publishing events after long pause. Do I need issue Particle.conect() again? Also the documentation on Particle.publish() suggest that connection is reestablished automatically:

If the cloud connection is turned on and trying to connect to the cloud unsuccessfully, Particle.publish may block for 20 seconds to 5 minutes.

In my case as I say the 2 events I publish got completely lost and instead when the call to publish happens it publishes some system events.

If you really cannot break up the task, I’d suggest you actively disconnect from the cloud via Particle.disconnect() and Particle.connect() after it and then waitFor(Particle.connected, 30000) before publishing.

1 Like

I will try that however this whole thing is just ridiculous. I disable vitals publication (Particle.publishVitals(0)) and publish the last message with WITH_ACK which according to docs should block until Cloud confirmed that publication is received. This all happens after Particle.process(). And yet I still get no message published and instead spark/status event followed by spark/device/diagnostics/update event with vitals that I disabled. That honestly looks like a bug to me.

The system needs CPU time to handle network events so they don’t time out. If you don’t allow it to do that, they time out.

If you need one-on-one help with this project, feel free to contact me directly.

I understand all of that of course and the fact that it is timed out. What I don’t understand is why when the connection is restored it attempts to send a bunch of system events including vitals that I disabled and overrides the event I issued using WITH_ACK. And all of that after I explicitly issue Particle.connect() (naturally before sending my event to reconnect to cloud)?

The only way to overcome this for now is for me to use waitFor() which manages to somehow wait until the first two system event are sent but before the vitals so I am guessing my event has enough place in a buffer to fir - but that is less than ideal. In general this behavior makes publishing unreliable and undeterministic.

Yep. If you need specific help navigating these issues from someone who has done it, feel free to contact me directly.

I made it work (sort of - as I said). But I’d rather Particle provided a deterministic and clean way of doing that. Or at least disabling all the things I have not asked deviceOS to publish. I prefer for the answers (if there are any) to be here rather than in some private conversations - the whole point of the forums is that nobody has to do that twice and search instead.

1 Like