Make Particle Photon go into listening mode when no wifi is found

Also, I use comments to help me try an work out code with logical blocks when I want to code something that I don’t exactly know what the code should look like. For example,

//Check if we are connected to WiFi
  //if not connected, wait for 2 minutes
    //after wait period, go into listening mode

From there, I start to research how to do each one of those tasks. You could add some if statements with psuedo code to get closer to the end goal like this:

//Check if we are connected to WiFi
if (!WiFi.ready) {  //We are NOT connected to WiFi
  //if not connected, wait for 2 minutes
  if (millis() - whendidwestartlistening > 120000) {
    //after wait period, go into listening mode
    WiFi.listen();
  }
} else { //We are connected to WiFi

}

@ScruffR gave you the check for WiFi connectivity with the WiFi.ready(). @Viscacha gave you the suggestion for a millis() based timer which you can search this forum for examples on how to accomplish. @ScruffR also gave you the piece for how to go into listening mode with WiFi.listen(). All you have to do at that point is create a few variables to track status between loops, figure out when to reset those variables and when to increment those variables. For example, when I was writing the code in the previous post, I got to a point where I said “I only want to call WiFi.listen() once. Now how do I track that I already called it?” So I had to go back and add the boolean to the header and figure out when to set/reset that boolean.

2 Likes