State of running code when Device goes offline

I often see my device go offline and then come back online a little while later. During that time, what happens to the running code. Does it just continue running but all functions that need the Wifi are ignored, or does everything just wait until the Wifi connectivity is resumed?

That depends on your SYSTEM_MODE() and whether you use SYSTEM_THREAD(ENABLED) or not.
If you don’t use either, your code will block.

That’s a widely discussed topic you will be able to search for on the board.

The thing is I do some calculations and then update a webserver with my results. I don’t mind if the webserver update fails because I can update it on the next loop, but I don’t want the code to get stuck if the WiFi connection goes down.

Hence the reference to keywords that would help that.

And also the hint about searching the forum (e.g. for no wifi)

3 Likes

Can I use SYSTEM_THREAD(ENABLED) with the default SYSTEM_MODE.
I don't want to manually manage the Cloud and WiFi connectivity. Equally, if it's not available, I don't want my code getting blocked waiting for it to connect. If it's there, I'll use it, if not I'll try the next time.

Particle.connected() returns true if you are connected to the cloud

so like this:

if(Particle.connected())
{
  // send your stuff
}

Thanks all, I got the bit about check if the Particle is connected before doing any http function. The question I had was if I use SYSTEM_THREAD(ENABLED) with the default SYSTEM_MODE(), will the Cloud connection be restored if it drops, in the background, without me having to explicitly try and make it connect.

Yes, it should be - unless you told the system not to.
But if you only need WiFi but not the Particle cloud functions you may still want to use SEMI_AUTOMATIC.
You don’t have to do a lot of managing with that either.
Just

void setup() {
  Wifi.on();
  WiFi.connect();
}

And for code that requires WiFi, just check for if (WiFi.ready()) { ... }.
Not too difficult IMHO.

I read Cloud connectivity as Internet connectivity as opposed to WiFi connected, just simply being connected to my WiFi. No?

Is there a way I can check a valid Internet connection? And not just WiFi connection.

Nope, Particle cloud is more than internet connectivity, but true, if your WiFi loses internet you can still see WiFi.ready() but on the other hand when you see WiFi.ready() but !Particle.connected() you may still be able to access the internet.

To check internet without Particle cloud, you try to WiFi.ping() a remote address.

But if I have Cloud connection I should have Internet too. Right? I am lazy, since Particle.Connected exists I’m using that to check Internet connectivity.