Photon not always publishing after waking from stop mode

I am working on a project where I need the photon to periodically publish data and then go to sleep until the next publish. I am using the particle::Future return of Particle.publish to call a callback when the publish has completed, and once this callback has been called I delay for 5 seconds before entering sleep. I have the publish on a timeout so if the publish is not successful after some time the system will go to sleep again.

About 90% of the time the publish is successful and everything works fine but the other 10% of the time nothing will be published and it enters sleep. When it wakes up again it will publish the event that was supposed to be published last time, as well as the current event.

If I replace the System.sleep call with

Particle.disconnect();
delay(sleepTime);
Particle.connect();

everything works fine. I have tried sleeping with SLEEP_NETWORK_OFF and SLEEP_NETWORK_STANDBY but both cause the same issue.

If you can post the relevant parts (or all) of your code, I’m sure you can get some suggestions.
Which System Mode are you using, with or without System Threading ?

Generally, you should avoid using delay(), and use a simple millis comparison instead.

I am using SEMI_AUTOMATIC mode with System threading enabled.

The 5 second delay after publishing uses millis comparison, not delay(). I only use delay() in

Particle.disconnect();
delay(sleepTime);
Particle.connect();

to simulate sleep, this won’t be in the actual code. As for the code, I unfortunately cannot post it but I can show you what the publish function looks like:

auto result = Particle.publish(event, data, 60, PRIVATE, WITH_ACK);
result.onSuccess(Success_Callback);
result.onError(Error_Callback);

The Success_Callback simply sets a flag indicating the publish has completed. Once the flag is set, I wait 5 seconds before calling System.sleep(D0, RISING, SLEEP_NETWORK_OFF, sleepTime);
When it wakes up I wait for Particle.connected() and then publish and repeat.

With SYSTEM_THREAD(ENABLED); and SYSTEM_MODE(SEMI_AUTOMATIC); you should be able to call System.sleep(sleep period); and it will enter sleep and then exit after sleep period seconds. If you are doing a Particle.publish() then I would test it is done if (Particle.publish()) or use WITH_ACK.

After exiting sleep you may need check WiFi.ready() and possibly Particle.connected() before publishing. Checking Particle.connected() before Particle.publish() is always a good idea.

I have tried sleeping with SLEEP_NETWORK_OFF and SLEEP_NETWORK_STANDBY but both cause the same issue.

These are for the Electron not the Photon.