Automatically search for new Wifi

Hi !
I was wondering if there is a way to get the Photon to get in Listening Mode automatically if there is no known Wifi network after a minute.

If this impossible, is there a way to make a external button that would have the same functionality as the Setup button ?

Finally, is there any way to run a code if the spark is offline ?

Thanks !

Well, there’s this: https://docs.particle.io/reference/firmware/photon/#wifi-listen-
And this: https://docs.particle.io/reference/firmware/photon/#-code-waituntil-code-
And this: https://docs.particle.io/reference/firmware/photon/#wifi-connect-
And this: https://docs.particle.io/reference/firmware/photon/#system-modes
And this: https://docs.particle.io/reference/firmware/photon/#system-thread
Also, this: https://docs.particle.io/support/troubleshooting/mode-switching/photon/

Then again, it also has a search function, but simply browsing through it wouldn’t hurt either :wink:
Let me know if that helps or if you require further information.

2 Likes

Yes, there are even solder pads underneath that allow for a secondary SETUP button - also in the docs :wink:

1 Like

@Moors7 Thanks for the links, I’ve take a look a them, but I’m still not sure how to do what I want.
I would need something like this:
if(no network known available after 60 secondes){
WIFI.LISTEN();
}
But I don’t know I to do this and I don’t really understand how to run code when the Photon is offline.

I think that this function should be by default. Like the Chromecast: when there is no known network available, It enter in listening mode while checking if there is still no known network available.

@ScruffR for the button, I would need to use the pins of the Photon only.

Thanks a lot guys !

Nope, that would counteract intended default use of the device. You can take it away from one known network and it should still carry on doing its other duties (sensor reading, controlling, …) and when it aproaches another known network just join this and offload its collected data.

If you really can’t use the solder pads at the bottom you can find some threads mimicking some of the SETUP functions.
e.g. like this
Photon won’t enter listening mode while connecting to wifi (works using setup button but not using external button)

… snippet about 60sec listen follows …

1 Like

Wait you mean that when the core is trying to connect to a network (blinking green) it can still run the code ?

If you read up on SYSTEM_MODE() and SYSTEM_THREAD(ENABLED) in the docs, you’ll see :wink:

SYSTEM_THREAD(ENABLED)
SYSTEM_MODE(SEMI_AUTOMATIC)

uint32_t msWiFi;

void setup()
{
  Particle.connect();                 // since SEMI_AUTOMATIC it needs to be done in code
  waitFor(Particle.connected, 10000); // wait for the connection but 10sec at the most
}

void loop()
{
  if (WiFi.ready())                   // if the WiFi is connected and ready
    msWiFi = millis();                // rember when it last was seen ready

  if (millis() - msWiFi > 60000)      // check how long ago WiFi was seen reads (usually some nano sec)
  {                                   // but if it was more than 60sec
    WiFi.listen();                    // go into Listening Mode
    waitUntil(WiFi.ready);            // stall loop here till WiFi is readily connected
    Particle.connect();               // once it's ready start connecting to the cloud again
  }

  // do some stuff
}

Or you could use the SoftTimers instead of the "manual millis()" way :wink:

3 Likes

I don’t quite understand the code can you explain it a little bit… That would be very nice !

Thanks !

See inline comments above :wink: