Fire and forget, non-blocking Particle Publish

According to this documentation:

https://docs.particle.io/reference/firmware/photon/#system-functions

A call to Particle.publish via the application code in the main loop will block even with the System thread enabled. I cannot have this be true as I’m running a critical system (a high powered heater may be on and I can’t just block the logic that checks whether to turn it off or not). Is there any option here for publishing an update to the cloud without waiting for the response? Perhaps by running Particle.publish in a hardware thread? Effectively I am logging and I want to just ‘fire and forget’. I don’t care if it succeeds or not.

1 Like

What do you consider as critical blocking periode?

The return value of Particle.publish() only tells you whether the event could be enqueued in the message queue, which will happen in the millisecond range.
It does not tell you whether the event was successfully delivered to the cloud (unless you run with WITH_ACK flag IIRC).

BTW, mission critical setups should always have a HW fallback too.

Thanks for replying. My real world experience doesn’t match with the idea of a few milliseconds though or I am fundamentally misunderstanding something?

SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);
//SYSTEM_MODE(MANUAL);

//#define WIFINAME "SSID"
//#define PASSWORD "PASSWORD"

void setup() {
  //WiFi.clearCredentials();
  //WiFi.setCredentials(WIFINAME, PASSWORD);
  Serial.begin(9600);
  Particle.connect();
 }

void loop() {
  delay(5000);
  uint32_t freemem = System.freeMemory();
  Particle.publish("wifi","all is well, memory:" + String(freemem));
  Serial.println("tic");
}

In testing we run this and take our photon and laptop and walk to the edge of our WiFi range. During that time the tic’s go from printing every 5 seconds to about every 15-30 seconds. If all Particle.publish was doing was putting it on a queue, then this shouldn’t increase in length. If it is blocking then this would be expected. Can you help me understand?

Maybe @rickkas7 can provide more specific background info and an evidential description, but with your setup you are not actually testing the timing of Particle.publish() alone, but the overall behaviour of the system. And despite the SYSTEM_THREAD(ENABLED) instruction you will probably see some lag imposed by the system thread on your own code due to single threaded sections in the system dealing with poor signal issues.

However, there are some things you could/should change.

  • you should check Particle.connected() before attempting to publish
  • you could publish with NO_ACK
  • you could place the actual publish instruction inside a microseconds() “stop-watch” to get the time for only that “one” instruction