User application running while attempting to connect to Particle Cloud

Reading the Photon docs I see that if I know I won't have an Internet connection, I can put the Photon in manual mode which will allow my application to run without a connection. This is OK, but from what I've been able to gather if I'm in manual mode and call Spark.connect(), my application will halt until a connection is made:

After you call Spark.connect(), your loop will not be called again until the device finishes connecting to the Cloud

Will this call actually block forever or will it eventually timeout and return control to my application? I'm fine if it gives up connecting after X seconds and I can return to my code...but that is not how I'm reading things. Ideally I'd execute my application logic doing cool things locally and whenever I have a connection I'll push up alarm states or data I've collected while offline.

I've read some other posts about a task to be completed to add threading so the cloud connection runs on a separate thread...that is ideal of course but I'm still curious if I can weave online/offline logic into my app without having to worry about execution halting due to a failure to connect.

If there a way to configure the Photon to automatically enter into listen mode if it is not able to establish a connection to a known Wi-Fi network? The buttons on the Photon aren't accessible in the enclosure I'm using, so when I take it places to demo it and use the local access point I end up having to unscrew things and open the enclosure. It would be great if I could achieve this behavior using auto mode, but if I have to use manual mode that would be ok as well. Looking at the docs I see WiFi.connect() and WiFi.listen() look promising but this part is worrisome

If there are credentials stored, this will try the available credentials until connection is successful.


Why not just try it out :wink: ?

But AFAIK Spark.connect() does not block forever - at least this was my experience on the Core.
Have a look here: Create time out for Spark.connect()?

On the other hand if it does block, you can still use the code of above post to cancel a connection attempt.
Just be aware, that - on the Photon - SparkIntervalTimer library is not yet working (properly) and that Spark.connect() does/did not implicitly perform a WiFi.connect() - as reported by some users.

At least in semi automatic mode, the spark.connect does not seem to block, and you have to check spark.connected later to make sure you are connected if its vital.

Documentation is a bit hit and miss at the moment, with wrong/inaccurate information thrown in for good measure.

In my experience using my test code Spark.connect() does call WiFi.connect() but not WiFi.on() and yes the call returns.

As we are expecting the release of the completely overhauled version of the docs next week (first week in August) have a look at if will suite you better (at the mo still beta ;-))

The beta docs have the same text.

When the user calls Spark.connect(), the user code will be blocked, and the device will attempt to negotiate a connection. This connection will block until either the device connects to the Cloud or an interrupt is fired that calls Spark.disconnect().

Thanks for the reference to the other post, I missed it when searching for previous info on the subject.

Sounds like throwing it in SEMI_AUTOMATIC mode and coding up the logic to bring up the connection will work fine. I’ll definitely give that a try.

Darn, already an issue that was not addressed :wink: - sorry for that :blush:

I only looked at the description of the system modes in "Guide" and there was not word if this.

1 Like