Making WiFI optional

Friends,
I’ve been trying to figure out how to make WiFi optional for my Photon projects. I.e. I want the device to look for WiFi on power up and connect if it’s there… but if no wifi is found, I want it to run anyway. The reason i need this is that the function of the thing I’m building doesn’t need Cloud connection… but I don’t want to give up the ability to reflash it wirelessly when wifi is around.

I saw several discussions about SYSTEM_MODE(SEMI_AUTOMATIC); that tell one how to connect on a button press, etc…but my device is sealed… so buttons are problematic… I’ve yet to see a complete example that tries to connect on power-up, but continues when none is available. (quite a bit of discussion here Create time out for Spark.connect()?) … but no conclusion.

here’s what I’m trying to do :
-i put the device in SEMI_AUTOMATIC mode
-In my loop, I check if WiFI is not connected, If not,
— I try a routine that Sets a TImer, then tries to connect.
—if connect is successful, I kill the timer
— if instead the timer times out, I call disconnect, in the hopes it will ‘unblock’ the attempt to connect. (see my psuedo code example below)

Short story is It doesn’t work… , the connect still appears to block… and it never returns…

Does anyone have an example of something that does this ? \I’m trying to make soething for a wedding today… so I’m kinda desperate. Thanks very much !
-jc

#include <SparkIntervalTimer.h>

SYSTEM_MODE(SEMI_AUTOMATIC);

IntervalTimer myTimer;        
volatile bool  onLine = true;

void noCon(void) {
    Particle.disconnect();
    onLine = false; 

}

void doCon(void) {
  myTimer.begin(noCon, 30000, hmSec);
    Particle.connect();
     myTimer.end();
}

void setup() {
  .. setup stuff …..
}

void loop() {
    
     if((onLine && !Particle.connected()) == true) {
      
        doCon();
    }

… loop stuff ….

}

Something like this: https://docs.particle.io/reference/firmware/photon/#waiting-for-the-system?

2 Likes

First you seem to be missing the last but one post in the thread you linked, which seemed to cure the issue for the last poster in the thread.

So go for SYSTEM_THREAD(ENABLED).
Next, you can avoid most hit and miss connections by emplying WiFi.scan() in conjunction with the stored WiFi networks available via WiFi.getCredentials().

Armed with these two functions (and their relatives found in the docs, especially WiFi.on()) you should be able to pull off what you intend to do.

3 Likes

ScruffR,
Thats was it !>. works great now… … Here’s the code in case anyone needs it

// simple example that will check wifi only at startup. if no wifi is present it runs offline.
#include <SparkIntervalTimer.h>

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED).  // prevent blocking on connect

IntervalTimer myTimer;          // set up a timer
volatile bool  onLine = true;   // only check wifi at startup 

void noCon(void) {     // timer timed out.. , kill the pending connection
    Particle.disconnect();
    onLine = false;    // don't chek again for this power on sesion 

}

void doCon(void) {    // see if we can connect. 
  myTimer.begin(noCon, 30000, hmSec);  // set a timer to clear the connect if it doesn't work in 30 seconds
    Particle.connect();      // try connecting 
     myTimer.end();         // must have worked 
}

void setup() {
  // .. setup stuff …..
}

void loop() {
    
     if((onLine && !Particle.connected()) == true) {
      // check for connection
        doCon();
    }

/ rest of loop stuff ….

}
1 Like