Best Strategy For Running Code When Cloud/Wifi Connectivy is lost?

I’ve been reading thru the threads on solutions posted throughout the past year, and seems like there are so many options.

I just basically need to continuously call a function to retrieve the next item in an array with or without connectivity. Currently, the code is blocked from running while flashing green/blue.

What is the best, optimal solution?

thx
Sabri

System mode manual is the first step. This allows loop() to be called even when you’re not connected to Wi-Fi or the cloud.
https://docs.particle.io/reference/firmware/photon/#manual-mode

You probably also want to enable system threading. It’s not required with manual mode, but loop runs more regularly with it enabled.
https://docs.particle.io/reference/firmware/photon/#system-thread

You need to be a little careful about what you call from loop, as well. For example, you probably only want to Particle.publish() when you’re actually connected to the cloud. With all of these things done, I’ve found that loop() runs very smoothly even as you connect and disconnect and reconnect from the cloud and Wi-Fi.

Thanks for the quick response!

i’m only creating a particle function which gets called when a service detects a tweeted hashtag, so no publishing.

when connectivity is lost i want to be able to fallback to calling an array of predetermined values.

@rickkas7, I’d rather suggest SEMI_AUTOMATIC than MANUAL otherwise you need to take care of Particle.process() while being cloud bound too (MANUAL is good once you are a bit more familiar with the under-the-hood behaviour tho’).
SEMI_AUTOMATIC on the other hand is a start-and-forget mode (just drop out of loop() often enough).

@sansoy, was there a question between the lines or are you happy with Rick’s answer already?

@ScruffR i’m new to this platform and we have a very high visibility event happening this monday so a little stressed :wink: I just need the code to continue running in case we lose connectivity. I’ll first try semi_automatic and may throw questions at you if i have problems. the docs are pretty straight forward on implementation. thx

1 Like

Also consider Rick’s suggestion to use SYSTEM_THREAD(ENABLED) for extra indepenence from WiFi/cloud issues.

you guys are rockstars! here’s the basic code, which i successfully tested with my wifi hotspot on and off. note: running with automatic system mode.

SYSTEM_THREAD(ENABLED);

void setup() { 
    Particle.function("tone",toneToggle);
}

void loop(){
  if (Spark.connected() == false) {
    String randNumber = String(random(0, 4));
    toneToggle(randNumber);
  } 
}
1 Like

@sansoy, you should use Particle.connected() instead of the deprecated Spark.connected() :wink:

1 Like

I think this code will produce some odd sounds when losing connection, since you will call the function rather quickly over and over again :ear: :gun:

1 Like

updated to Particle.connected and works fine.

This code i posted is more like pseudo code. I have other checks in place.

Been successfully testing the past couple hours, and havent heard any sounds. not sure what sounds your are referring to.

Since you didn't show the implementation I just guessed from the function name, if you keep toggling between different tones that quickly I just thought that'd not be the what you planned :wink:

got it! and i meant color tones…lol.

seriously thank you all for your help.
i love this product.

Stay tuned for the big event reveal next tuesday!
s

3 Likes