In “Semi-automatic mode” I dont find the below bold statement from the Particle docs to be true.
Once the user calls Particle.connect(), the user code will be blocked while the device attempts to negotiate a connection. This connection will block execution of loop() or setup() until either the device connects to the Cloud or an interrupt is fired that calls Particle.disconnect().
Before Particle.connect, I start a software timer which calls Particle.disconnect if its taking too long, but that does NOT interrupt Particle.connect. Particle.connect attempt to connect indefinitely.
Software Timers are no interrupts so the bold statement is still valid till proven wrong with a real interrupt.
But you might want to use
SYSTEM_THREAD(ENABLED) to buy yourself some degree of independence between system thread and your own code.
Ok, so how do you actually get it to stop attempting to connect indefinitely?
Since the connection process is not blocking 100% of the time, you still get the time to call
WiFi.disconnect() from your code - especially when using the suggested
Particle.disconnect() doesn’t stop it though. I’ve tried every combination of settings and if the Photon can’t reach particle cloud, the second I call Particle.connect(), it’s a dead Photon that needs to be flashed again. The only way to prevent it from becoming unusable is to call disconnect immediately after connect. If I wait even a second, it’s off in la-la land trying to connect and even if I turn the wifi off, it’ll still be trying to connect when I turn it back on.
Oh my, I figured out what my problem was. I was doing a waitUntil(Particle.connected), which was the primary source of my troubles. It is still true that calling Particle.disconnect doesn’t stop it from continually trying to connect, but I’m actually fine with this. My whole goal was to connect to WiFi first using credentials that are part of the firmware, so that I can do OTA updates to change passwords without needing to go open the dozens of boxes around the house, and to fire up MQTT and only then try to connect to Particle Cloud. That way, if the internet goes out all my home automation continues to operate unimpeded and the only issue I ever have is if Particle Cloud is offline (or out of business) I can’t do OTA firmware updates. If you could flash these puppies over WiFi it’d be a dream come true.
waitUntil() entirely and rather opt for
waitFor() which allows you to set a timeout.
Could you provide some test code to replicate the
Particle.disconnect() issue where calling it won’t stop the connection attempts. The way I tried it, it seemed to work as intended.
The firmware I’ve been working on is almost ready to post online, I just need to clean up a bit and I’ll push to github. Then I’ll create a branch to show this particular situation and send a link so you can see that. Thanks for jumping in to help!