I was wondering if it’s possible to wait for the ACK even if publishing with the NO_ACK flag set.
As far as I can tell the ACK is received by the electron even after publishing with NO_ACK set - the difference being that the particle.publish command does not block waiting for the ACK.
What I would like to do is send the data and then wait for the ACK afterwards:
Particle.publish(webhook, TX_DATA, PRIVATE, NO_ACK);
time = millis();
while (time < 20000)
{
//wait for ACK here
time = millis();
}
This would allow me to check other parameters while still being sure my data reached the cloud.
There is no public version of Particle.publish that allows for checking for success/failure and is non-blocking.
Fortunately, at least in 0.7.0 and later, the insides are actually asynchronous and thus I was able to build a handy class to enable asynchronous callback functions for publish!
Use the following. Keep in mind that a WITH_ACK publish can actually take a lot longer than 20 seconds to complete in situations where the cloud connection may have died.
auto ok = Particle.publish(webhook, TX_DATA, PRIVATE, WITH_ACK);
uint32_t t0 = millis();
while(!ok.isDone() && (millis() - t0) < 20000)
{
// you can do something else here or just go on with your normal processing
// and check back in occasionally
delay(1);
}
Log.info("publish %s", ok.isSucceeded() ? "succeeded" : "failed");