Photon wont connect back to wifi

my photon will be most of the times online but it may need to work offline (wifi modem off)
I am using:
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(MANUAL);

When the wifi is gone the green led blinks (thats ok),
After a while the led changes to blinking cyan and it wont connect to wifi nor respond, only a RESET will work and all my data stored in RAM is lost.

How can I make sure that it will connect back to wifi regardless of the amount of time it was without it?

(Maybe the following link helps but I did not understand how to fix it: [SOLVED] WiFi Reconnection Issue - Is there a solution?
)

thanks

@plusmartin, I have found that the @rickkas7 solution [SOLVED] TCPCLIENT intranet connection fails if no cloud connection works well.

He has supplied source code for the work around, so you should be able to copy it as it stands. Let us know what the issue is with your understanding of it.

When opting for SYSTEM_MODE(MANUAL) your code should take (manual) control of the connection.
Once your device loses connection (for prolonged time) your code should actively disconnect and then conciously try to reconnect.

how much data would that be?
As long your essential data does not exceed 3068 bytes you can store that in retained variables which will survive a reset.

I use SYSTEM_MODE(SEMI_AUTOMATIC); and SYSTEM_THREAD(ENABLED);

I assume that stored WiFi credentials are in range but if they are not then with system thread enabled and a recent GA device OS (post 0.8.0) the retry process will happen in the background. However, you need to be careful not to call app thread blocking functions when you are not WiFi connected and same with Particle functions if not cloud connected. This means testing for WiFi.ready() and Particle.connected() before calling such functions. If you want to turn off the WiFi module (to save power or for sleeping) then you will need to switch it back on and call WiFi.connect().

thanks @UMD, @ScruffR, @armor for your replies.


according to the link I need to add:
STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
but I am already using:
STARTUP(softap_set_application_page_handler(myPage, nullptr));
which I use to generate an access point if no wifi credentials are stored.
How do I proceed?

I only need to save one variable which is a counter of events, everything else can go. This variable gets updated every 20 sec aprox.
Normally when there is no wifi I am saving many other variables into an array:
#define ARRAYSIZE 1000
String backup[ARRAYSIZE];
in the hope that wifi comes back and I can read the array and publish the missing stuff. It works.

My only issue is that sometimes it wont reconnect and wont respond to any input, it is stuck forever, probably because of what @armor said, since my measurements do sometimes use delays for some seconds. I will add Wifi.ready() before asking if Particle.connected() and see if that helps.

The STARTUP macro takes a function pointer, so you can just put all of the things you want to run early in their own function:

void myStartup() {
    System.enableFeature(FEATURE_RETAINED_MEMORY);
    softap_set_application_page_handler(myPage, nullptr);
}

STARTUP(myStartup);

Or using a C++11 lambda:

STARTUP([]() {
    System.enableFeature(FEATURE_RETAINED_MEMORY);
    softap_set_application_page_handler(myPage, nullptr);
});
1 Like