Connecting to cloud, publishing variable and going offline again

I have the following working code:

Particle.connect();
waitFor(Particle.connected, 10000);
Particle.publish(“event”,“1”,10000,PRIVATE);
digitalWrite(led, HIGH);
delay(10000);
WiFi.off();

The delay(10000) is the only way I’ve found to get it to work. There seems to be some delay between publishing the event and the cloud contacting the device to retrieve ir. Is there a better way to do this i.e. wait for event to be published function?

Andy

Have you updated your device to 0.5.3 or later?
From that version on, the delay (5sec should suffice too) should not be required anymore (at least that is true for System.sleep(), not sure about WiFi.off())

BTW, the TTL parameter is not yet used by the cloud and 10000 sec would be rather long too.

Yes it’s a brand new device.

It publishes within 3 seconds of the command executing. The 10 seconds is just overkill but I had assumed that Particle.publish would be blocking until it actually published.

Having said, that I never really liked publish as my app requires that history is stored and accessible. Since writing the initial message I’ve found that I can send a http request to my web server and no longer need to use publish.

Andy

PS I was aware the TTL didn’t do anything, 10000 was a random choice. Well semi-random, I already had values of 10000 in the code so I just used the same.

Dropping a note here to tell others that a delay is still required except in certain cases, see: