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

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