I have an application where WiFi is off for most of the time, and is briefly turned on to publish events. I find though that if I do not add a delay before turning the wifi off again, the events are not always successfully published. It seems to me that the Particle.publish
returns immediately, so is there some way of ensuring that all events have been published before turning WiFi off?
I currently put in a delay(50)
before turning WiFi off, but I would rather not have to hard-code this time in, but say “wait until all events published, maximum 5 seconds before turning off WiFi”
AFAIK it’s planned to add some logic to sleep and possibly other functions that might interfere with pending publishes, but currently there isn’t anything.
One workaround to make sure an event was successfully published is self-subscribing to that event and only proceede when the subscription fires. This way you could even trigger a re-publish if that timed out.
But @mdma might have some extra input.
Maybe it would even be more desirable to directly add a “finished-callback” to Particle.publish()
instead of having to put that logic into other functions that might interfere with it.