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 WiFi.off() 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 WiFi.off() control connection to the WiFi network. Also, if you call WiFi.off(), you don’t need to call Particle.disconnect() first – WiFi.off() will switch you completely into offline mode.
TL;DR: Particle.connect() and Particle.disconnect() control connection to the Particle cloud. WiFi.on() and WiFi.off() control connection to the WiFi network. If you want to be truly independent of WiFi, call WiFi.off()
I hope this helps someone. Happy hacking!