Go into listening mode if no known networks are available

Ok, here’s the code I ended up with. It’s certainly not as compact/clean as it could be, but I’m still learning so trying to keep it as simple as possible for my benefit. Any comments/advice appreciated.

UPDATE: Moved WiFi.disconnect() and listen() calls outside of signaling for() loop as caught by @ScruffR below (thanks).

#include "Particle.h"
#include "softap_http.h"

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

                                            // INITIALIZE VARIABLES =================================

int LED = D7;                               // LED = onboard LED pin (D7).
bool newWiFi = false;                       // Flag to indicate looking for new WiFi credentials.

STARTUP(WiFi.setListenTimeout(120));         // Set max time to listen for new WiFi credentials.

                                            // SETUP ================================================

void setup() {
    pinMode (LED, OUTPUT);                  // Set LED pin to OUTPUT mode.

    WiFi.on();                              // Do manual WiFi initialization
    WiFi.connect();
    if(waitFor(WiFi.ready, 15000)) {        // Wait 15 seconds to find WiFi network
        for (int i = 0; i < 3; i++) {       // 3 Flashes = we have WiFi.
            digitalWrite(LED, HIGH);        // Send signal then...
            delay(100);
            digitalWrite(LED, LOW);
            delay(100);
        }
        Particle.connect();                 // Connect to Particle Cloud.
    }
    else {
        for (int i = 0; i < 6; i++) {       // 6 Flashes = no WiFi found after 15 sec.
            digitalWrite(LED, HIGH);        // Send signal then...
            delay(100);
            digitalWrite(LED, LOW);
            delay(100);
        }
        WiFi.disconnect();              // Disconnect WiFi.
        newWiFi = true;                 // Set flag to say we are listening for new WiFi credentials.
        WiFi.listen();                  // Listen for new WiFi credentials.
    }
}

                                            // MAIN PROGRAM LOOP =====================================

void loop() {
    if ((newWiFi) && (!WiFi.listening())) {
        System.reset();                     // If newWiFi and ListenTimeout already expired, reset.
    }

// Do Stuff()                               // Do rest of loop() code which will run while listening for new WiFi credentials.

}

What it appears to be doing successfully now is…

On power up it will manually start the WiFi connection (required since we’re not in Auto mode).

Wait 15 seconds to try to connect with the existing credentials.

Then either connect to the Particle cloud if successful, or

Disconnect the WiFi and start listening mode if no successful WiFi connection.
A newWiFi flag lets me know that I’m now acquiring new WiFi credentials via softap

And finally, a line at the top of the loop() function (which runs as soon as the setup() function is finished since we’re running SYSTEM_THREAD(ENABLED)) checks to see if we are getting newWiFi info and the WiFi listen timeout has expired, after which it resets the system so that it can try to connect again with the new information.

This prevents the new user from having to open the enclosure to enter new WiFi info when the product arrives at a new location, and does it’s own power-cycle to connect with that new data once it’s entered. Instead of system.reset() , I’m guessing I could probably just do a new WiFi.connect() at this point with the new connection info instead of power cycling the device?

BTW, the flashing messages are my trouble-shooting feedback. Since I’m programming from web IDE, I don’t have Serial.println() available to send debugging messages? Is there a better choice for these when working from web IDE?

Thanks in advance, it’s very nice to finally see it working but I know I have quite a bit of work yet to go.