Can you turn off WiFi.listen() once it starts? and what is the purpose of WiFi.listening()? [SOLVED]

Okay, so I’m coming into this long after the fact after searching around the forum for a bit.

I’m trying to find if there is any kind of Best Practice for handling code which doesn’t depend on the Cloud, or even WiFi in general, but where I still might want to occasionally check in.

So for example, I want to run my project in a location where there might not be WiFi, so obviously I need to start with SYSTEM_MODE(MANUAL). But I might want, at intervals, to see if WiFi is available, and if so, connect. Can I do that without blocking my code for a long time? Particularly, can I check for availability of WiFi without connecting to it?

I have not tested it yet myself, but WiFi.scan() should be a hot candidate to look at, if you want to see if there is WiFi but not connect.

You don’t need SYSTEM_MODE(MANUAL), I usualy rather go for SEMI_AUTOMATIC, works just a well and with the multithreaded firmware, cards will be shuffled new again :wink:

So, SEMI_AUTOMATIC won’t block my loop from running if there’s no WiFi available?

(I would have just tested this myself before now, but I’ve been in-and-out of meetings all day)

Nope, it starts up just like MANUAL without WiFi and gives you all the freedom of MANUAL but you don’t need to bother with calling Particle.process() since it’s done automatically once it got connected (unless your code blocks).
In MANUAL you have to do all of that yourself even without your code blocking.

I am very interested in the sort of operation as well. I am excited about SYSTEM_THREAD(ENABLED). This may take large steps solving this issue.

So, here’s what I ended up doing in my Eyeblink code…

In my setup() function, I use SYSTEM_MODE(MANUAL). Then, in my loop(), I have this code, which allows me to detect when I give the MODE button on my Core a quick press:

    /**
     * On MODE button click, Particle Cloud connect/disconnect
     */
    if (HAL_Core_Mode_Button_Pressed(100)) {    // 100ms press
        // Turn cloud connection on/off
        toggleCloud();
        
        /**
         * debounce 1/2 sec before resetting button state to
         * check for system mode change
         */
        delay (500); 
        HAL_Core_Mode_Button_Reset();
    }

Of course, you could rig up an external button, instead. This is just convenient for me, right now. The toggleCloud() function is this:

/**
 * Toggle Cloud connection.
 * 
 * If we are not connected to the Particle Cloud, then try to connect.
 * 
 * If we are already connected, then disconnect.
 * 
 * Onboard RGB LED will be off when not connected.
 */
void toggleCloud() {
    
    if (WiFi.ready()) {
        // Already connected to WiFi... Check cloud?
        if (Spark.connected()) {
            // Connected to cloud. We must be wanting to disconnect?
            Spark.disconnect();
            WiFi.disconnect();
            RGB.control(true);
        } else {
            RGB.control(false);
            Spark.connect();
        }
    } else {
        RGB.control(false);
        Spark.connect();
    }
}

So, my Core starts up offline, to save power. But if I press the MODE button, it fires up WiFi and connects to the cloud. Then I can flash a new code update, or whatever. If it is already connected to the cloud when I click MODE, then it disconnects. Works great!

1 Like