Starting Spark Core without wifi [SOLVED]

Hi,
How do I set the Spark Core to start when it is not in wifi range (outside, for example)? I found this thread:

But the link provide does not work. If I boot the device while it is connected to wifi, the application keeps working even if I go outside the wifi range, but the application won’t start if I turn on the core when outside wifi range.

Thanks!

If you use SEMI_AUTOMATIC mode and start WiFi yourself using

WiFi.connect()

(docs)

Then your code will always run with or without a wifi connection.

2 Likes

Try this snippet:

SYSTEM_MODE(SEMI_AUTOMATIC);

uint8_t retry_count = 0;
unsigned long old_time = millis();

void setup(){
    WiFi.on();
}

void loop(){
     if(millis() - old_time >= 2000){        
        if(retry_count < 10){
            if(!WiFi.ready()){
                WiFi.connect();
                retry_count++;

            }
            else if (!Spark.connected()){
                Spark.connect();
                retry_count++;
            }
        }
        else{
            WiFi.off();
            retry_count = 0;
            WiFi.on();
        }
        old_time = millis();
    }

    // place the rest of your code below

}

1 Like

If I set the device to semi automatic mode by adding SYSTEM_MODE(SEMI_AUTOMATIC); , I am guessing that I won’t be able to flash the device, right? So how can I set the device to try and connect once after it is switched on and if it fails it proceeds in SEMI_AUTOMATIC mode?

Thanks.

@osprey, the code i wrote will automatically attempt to reconnect the moment wifi connection drops.

If you have a requirement that the device must first connect upon boot-up, you can add a Spark.connect() in setup().

I have to warn that your device will be stuck here until it finally connects so it is all or nothing.

Otherwise, the snippet i wrote above will work well. Give it a try and see how it behaves :wink:

1 Like

@kennethlimcp I tried that snippet and the device got stuck into an infinite loop in which it attempts to connect and then runs my code. I could not flash the device and ended up having to apply a factory reset. I am totally new to Spark/Particle (received it today) and do not know of another method to stop the code from running.

Edit:

I must have been doing something wrong. I just tried your code one more time and this time it worked fabulously. Sorry for the false alarm!

2 Likes

Have fun :smile:

This code is kinda stable but the WiFi.connect() blocks for 30+ seconds when there is no valid SSID in the vicinity. Needs some fixing but works great for me :smile: