Figuring out Particle.disconnect() and

Hey all,
I just spent a while debugging an issue with Particle.disconnect(), and I want to share my findings here:

I work on an application where I want the photon to be able to work without any wifi connection. There’s not a great way (as far as I know – correct me if I’m wrong!) to try to connect to a wifi network and fall back gracefully to offline operation if there’s no network present. So we put in a physical switch to tell the photon to switch between “online” and “offline” modes.

All my code is run in SYSTEM_MODE(SEMI_AUTOMATIC) to keep the photon from automatically trying to connect to a network. In the main loop, the program reads the switch’s state. If it changes to ‘online’, it calls Particle.connect() to connect the photon. If it changes to ‘offline’, it calls Particle.disconnect to disconnect from the wifi network.

And here’s where I ran into a bug:
I was debugging my code in a room with wifi. When I tried to switch to offline mode, I called Particle.disconnect(), and the light on my photon changed from breathing cyan to a solid green. The program continued to run normally, and in my initial enthusiasm, I thought that I had gotten things working nicely.

However, if I took my photon outside, away from the wifi, the solid green light on the photon would change to a blinking green, as it tried to connect to wifi. This would block the program from running. Oh no!

It took me a while to realize that Particle.disconnect() didn’t make me independent from wifi. I had to call instead. There’s a distinction between connecting to the WiFi network and connecting to the Particle cloud. Particle.connect() and Particle.disconnect() control connection to the Particle cloud. WiFi.on() and control connection to the WiFi network. Also, if you call, you don’t need to call Particle.disconnect() first – will switch you completely into offline mode.

TL;DR: Particle.connect() and Particle.disconnect() control connection to the Particle cloud. WiFi.on() and control connection to the WiFi network. If you want to be truly independent of WiFi, call

I hope this helps someone. Happy hacking!


1 Like

Hi Alex,

I’m trying to always use my photon without wifi. I cant wok out the code needed to do this.
Would you be able to share yours so I can see if that will work?


@aimee, if you never need WiFi or Cloud, run your Photon with SYSTEM_MODE(SEMI_AUTOMATIC) or SYSTEM_MODE(MANUAL).

@peekay123 thanks! does using SYSTEM_MODE(SEMI_AUTOMATIC) mean that when it can it will connect to wifi and when it cant connect it won’t use wifi but the system will still run?

First, this is not a bug but by design and documented as such.

And next

There are also WiFi.connect() and WiFi.disconnect()

@aimee, yes and no. You will need to make your code connection-aware so you don’t try to do wifi or cloud commands when not connected. You will also need to use SYSTEM_THREAD(ENABLED) if you don’t want your code to be blocked when the connection is dropped and the system firmware attempts reconnecting. There are lots of topics discussing different approaches to this, including some great ones from @rickkas7.

However, this statement seems to suggest you NEVER want to be online.

ok, i do want to be able to use wifi but only when there is wifi to connect to otherwise i just want the code to run. how do i do that? what code is needed for that?

You need to have WiFi.on() and I’d try WiFi.scan() for WiFi only and for internet and cloud access and when availble call the respective xxx.connect() function.
I’d also suggest SYSTEM_THREAD(ENABLED) and SYSTEM_MODE(SEMI_AUTOMATIC) as @peekay123 already did.