Testing the limits of Photon's Wifi connection


#1

Hello everybody!!

I’m currently working with a product where the users will take their device inside and outside WiFi range. In addition, the battery life is very important to the product, so it is important to have the device not actively looking when there is no wifi.

The problem I have is that when walking outside with the device, it refuses to stop flashing green/connecting to wifi. It will flash orange once, and stop flashing green. Ideally, I want the device simply breath white after a certain time.

Here is a bit of test code that I have been using to test the wifi connectivity, and using delays instead of millis() with the full code.

void loop() {

WiFi.on();
WiFi.connect();
Particle.connect();
Particle.process();
delay(5000);
    if(WiFi.ready() == true) {
    Particle.publish("Event","Hey Guys! I'm connected to the cloud!");
    Particle.process();
    }
    
    else {
    WiFi.off();
}
delay(10000);
}

I fully expected the code to just be breathing white when I am nowhere near the wifi connection. Any ideas on what’s going wrong?


#2

You should use SYSTEM_THREAD(ENABLED) and probably also SYSTEM_MODE(MANUAL)

And you definetly don’t want to unconditionally call WiFi.on(), WiFi.connect() and Particle.connect() over and over in loop().

A more sophisticated approach would be to use WiFi.scan() and check whether a known SSID can be found and only connect when that’s the case.

BTW, your indentation scheme is less than ideal :wink:
The if() and else statements have the same scope/“hierarchy level” as your other instructions outside of the conditional blocks but all the instructions inside loop() have a lesser scope than loop() itself.


#3

Thanks for the feedback, I’ll give that a shot today :slight_smile:


#4

What is the problem with the photon trying to connect to WiFi when out of range? I presume you will want it to reconnect again when in range?

In such a case you need to use SYSTEM_THREAD(ENABLED); allow the system to keep checking in the background and then avoid any blocking calls from your loop();

If you search on the community forum you will find many threads that discuss and explain what is blocking and how to navigate that.