Sleep after publish, updates?

Hi, I’m losing messages sent right before sleep. I see related topics but none recent. What’s the latest?
The doc says:
WITH_ACK flag

This flag causes Particle.publish() to return only after receiving an acknowledgement that the published event has been received by the Cloud.

my code:
Particle.publish(“Before sleep”, mainPayload, WITH_ACK);
//delay(500);
//Particle.process(); // process queues before sleep
System.sleep(config);
Particle.publish(“After sleep”, mainPayload);

If I use the delay and process, it seems to work most of the time but I’d rather have a deterministic solution and save 500 ms.
I have WITH_ACK but it doesn’t seem to wait like the doc says, I never receive a “before sleep” message at the console. Why not?
Thanks,
Rainer

@RainerR

I’d rather have a deterministic solution

In my experience using cellular data comms isn’t deterministic, WiFi is somewhat deterministic and ethernet pretty much deterministic.

I do a lot of publishes just before calling sleep on battery powered devices and don’t have this problem with WiFi or Ethernet comms.

Aware of what the docs say…but have you tried waiting for; bool success = Particle.publish("Before Sleep", mainPayload, WITH_ACK);

I use a send buffer, initial one I created myself and more recently the library from Rick - publishQueueAsyncRK https://github.com/rickkas7/PublishQueueAsyncRK This appears to work reliably and then you could simply check that the send queue was empty before calling sleep.

1 Like

Checking the result from Particle.publish(), as Armor suggested, should help. The reason is that Particle.publish() is non-blocking if you don’t check the result, which is a Future<bool>. In your case, you want it to block until acknowledged, before initiating sleep.

1 Like

Solved, that works! If I assign the response to a value, even if the value is not used subsequently, I get the message published before it sleeps; details, details. I’ll keep the send queue in mind for later. For now it’s working. Thanks very much for your quick, and effective, replies!
–Rainer