When WIFI is lost, anyways to keep the device running instead of blinking green?

Hi,

Is it possible to have the device keep running instead of blinking green while WIFI lost? I ran into this scenario today, my wifi was lost to the device. The device keep blinking green but I can’t do anything else with the device. Anyways to have the device enter no wifi mode after a period of time trying to reconnect with out success? Then try to reconnect after a period of time. Thank you for the help.

Try searing for “no wifi” on these forums :wink:

1 Like

You could acheive this by using SYSTEM_MODE(SEMI_AUTOMATIC) or SYSTEM_MODE(MANUAL).

You would need to manually connect and disconnect from wifi in your code.

https://docs.particle.io/reference/firmware/photon/#wifi

Or system threading…

1 Like

I been looking thru the document and forum, I am thinking of using SEMI_AUTOMATIC. But I am confuse on how to release the blocking form the user code once the internet is lost, it just keep blinking green.

Seem like back in '14, Functioning with wifi and internet drops, there were similar question as mine.

Does anyone have some sample code to keep the user code running during a lost of network and reconnect when the network is available?

I just made this example that works perfectly on my Photon:

#include "Particle.h"

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

#define disconnectDelay 15000 // After fifteen seconds of no WiFi, disconnect
#define checkInterval 60000 // How often to check for WiFi once disconnected

unsigned int lastAttempt, lastDisconnect;
bool connected = true;

void checkConnection()
{
        if (Particle.connected() == false && millis() - lastDisconnect >= disconnectDelay)
        {
                lastDisconnect = millis();
                WiFi.off();
                connected = false;
        }

        if (millis() - lastAttempt >= checkInterval)
        {
                WiFi.on();
                Particle.connect();
                lastAttempt = millis();
        }

        if (millis() - lastAttempt >= disconnectDelay)
        {
                connected = Particle.connected();
        }
}

void setup() // Put setup code here to run once
{
        pinMode(D7, OUTPUT);

        Particle.connect();

// Your setup code

}

void loop() // Put code here to loop forever
{
        checkConnection();

        if (connected == false) // This code will run while the Photon is disconnected from Cloud and WiFi
        {
                digitalWrite(D7, HIGH);
                delay(250);
                digitalWrite(D7, LOW);
                delay(250);
        }
        else // This code will run only while you are connected
        {
                digitalWrite(D7, HIGH);
        }

// Your loop code

}
3 Likes

@nrobinson2000, first of all, Thank You for the sample code. I am not sure if it make any difference if I am using the p1 module vs photon. When I ran the sample code, the device does not reconnect to the wifi once its available after lost.

I was able to get the device reconnect to the available wifi if I add WiFi.on() as below:

 if (millis() - lastAttempt >= checkInterval)
        {
                WiFi.on();
                Particle.connect();
                lastAttempt = millis();
}

From the document, Particle.connect() should turn on the WiFI module. Am I wrong?

I noticed if I pressed the reset button while the device is not connected back to WiFi. The device will be continuously blinking green again and enter a blocking mode until WiFi is connected. Any one know why this is happening? Since I am using SYSTEM_MODE(SEMI_AUTOMATIC); I was expecting this should not happen.

I doubt that. Where is that written?

@ScruffR, Ok, it did not said exactly turn on but it said automatically activate the Wi-Fi module.

Particle connect reference

Particle.connect() connects the device to the Cloud. This will automatically activate the Wi-Fi module and attempt to connect to a Wi-Fi network if the device is not already connected to a network.

If Particle.connect() does not turn on the Wi-Fi module, how did @nrobinson2000 sample code worked since the code contain WiFi.off();?

Does the Reset Button enter blocking mode regardless of the SYSTEM_MODE?

I have tested with SEMI_AUTOMATIC and MANUAL on SYSTEM_MODE with no WiFi, both give me the same result of blinking green light with blocking. Are there ways around this?

Using system thread will make it so wifi connecting is not blocking.

https://docs.particle.io/reference/firmware/photon/#system-thread

The code I posted does not block when connecting to wifi. D7 still blinks until you are connected to the cloud and the 15 second callback is reached.

I know there is probably a better way of doing this, so my code is not really perfect.

I stand corrected :flushed:

Particle.connect() does in deed power up the WiFi module - the docs are correct.

I think I solved the blinking green light blocking problem that I am having after pressing the reset button.

By putting the Particle.connect() at end of the setup() as the document example below solved the problem. I also put the SYSTEM_THREAD(ENABLED); first, not sure it make a difference but it can’t hurt. As it stated in the example, if I put the Particle.connect() before the Particle.variable, Particle.subscribe, Particle.publish, it will tied up thread.

From the reference document:

SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);

void setup()
{
    // the system thread isn't busy so these synchronous functions execute quickly
    Particle.subscribe("event", handler);
    Particle.publish("myvar", myvar);
    Particle.connect();    // <-- now connect to the cloud, which ties up the system thread
}
1 Like

Particle.subscribe(), Particle.function() and Particle.variable() can be executed with or without cloud connection, but Particle.publish() needs a valid connection.

Also if myvar is anything but a string, it’s also wrong.

1 Like

@ScruffR, the example code is from the reference document not my code.

System Functions

I was experiencing a blocking when I put the Particle.connect() right at the beginning of Setup (). After reading the document on System Functions, I put the Particle.connect() at the end of Setup() and it did the job. Before, the device keep blinking green when I pressed the reset button and after a long while it timeout and operate as normal. So somehow by pressing the reset button it cause the device to enter blocking mode. I do have Particle.subscribe(), Particle.function() and Particle.variable() in the setup(). Hope I am explaining this right. I just want to include this just in case anyone run into this scenario. System Functions have more information. Thanks all for the help on this. :smile:

Actually, if you don't need to do anything, like taking power-saving measures, you need do nothing but enable the system thread. Everything is taken care for you, just be sure to call Particle.connect() in setup. Instead of using flags, use Particle.connected() and possibly the waitUntil() and waitFor() functions.

Not sure why you posted this three years after the last post but one note to that:
When your checkInterval is chosen too short, you may still cause troubles when retriggering a connection attempt while a previous attempt is still ongoing.

Also there is little use in unconditionally calling that if Particle.connected() already is true.

1 Like

Thanks man….