I am trying to make photon work with multiple networks, each for a different purpose, for my project. Say I could use WiFi A as my main network and use WiFi B as a backup network or use this network to restart the power supply of WiFi A, essentially restarting WiFi A, whenever WiFi A loses internet connection or WiFi A is down for some reason. As a backup network, WiFi B is always powered, meaning photon is able to detect both networks.
I imagine the pseudo-code can be something like:
if (WiFi A loses connection){
WiFi.disconnect();
WiFi.connect(WiFiB, passwordB);
//then with a peripheral connected to WiFi B, restart the power for WiFi A
WiFi.disconnect();
WiFi.connect(WiFiA, passwordA);
}
However, WiFi.connect() is not made to be used that way. Instead, we have WiFi.setCredentials(SSID, PASSWORD); and a separate WiFi.connect(). And, also, WiFi.connect(), in the presence of multiple available networks chooses the connection with the strongest RSSI, and not necessarily as preferred by the user. (Multiple WVLANs - #17 by peekay123)
One approach I've seen here in Switching between Wifi devices [Solved] makes use of clearing and setting the credentials of the WiFi Networks. In the same thread, however, ...
So, yeah, that's where I am now. And what I know so far.
And so it boils down to this question:
What is the best way to switch connections between multiple known networks via the code? (I see that this is possible with listening mode in tandem with the mobile app, but as it is for the purpose of a backup network, I believe it is best accomplished as an automatic feature - written in the firmware and no user interaction required)
This is a good question, and I think there is another reason for wanting some more control here. As far as I can tell the Photon insists on connecting to the last configured WiFi point if it can see it. On the unit on my bench it is connected to a network at -80dB thats 20m away with several brick walls inbetween, however it also configured to talk to an AP 2m away with a signal around -40dB, but has chosen not to, this is silly. At -80dB Photons do not work well and are pretty much incapable of carry out OTA updates, and ignoring that I can tell you that attempting to use that -80dB AP on devices with better WiFi reception is not exactly a pleasant experience, both the signal power and the interference form 5 other networks in a shared office space next door conspire against you.
I do recall a discussion about such feature and I thought there already was an open issue about that, but as it turns out there isn’t (or I just couldn’t find it).
Could you open an issue to propose that feature, please?
I’ve already made an issue for the feature. I hope I did it right.
So, I guess, I’ll have to work with the clear and set credentials for now.
If you have any better ideas, I’ll be glad to hear them
One question though. Is it possible to make a network out of a photon in listening mode - i mean using the softAP? What I mean is, in cases where WiFi A is down, I will connect my photon to another photon which is in listening mode then, maybe the two photon can communicate so that the second photon can make a restart on WiFi A. (Sorry if I’m using the ideas/terms wrongly, I barely have a knowledge regarding this softAP thing)
Not really since the SoftAP doesn't really implement a lot of the functionality needed to run a full network, but having one Photon enter SoftAP mode when it loses its "home network" and have another one "subscribing" to that SoftAP SSID should be possible. The trouble with this tho' is that the communication between the two would be rather cumbersome, since - AFAIK - these two can only communicate via HTTP.
@engr.pron and @ScruffR: I was going through this thread enthusiastically because engr.pron described in his opening question exactly the scenario that I would like to address: i.e. to implement a sort of "backup strategy" where two independent WiFi networks are available (with one maybe as a preferred one or two equivalent ones), either by providing the Photon/Argon with credentials during the setup, or through the firmware code by using WiFi.setCredentials(credentials); and then have the Photon/Argon switch automatically to the other WiFi when one is not available (this should happen automatically anyway) or when the internet/cloud cannot be reached despite being connected to the WiFi (basically angr.pron has outliend the flow perfectly in his pseudo-code). I had already started to experiment with some "draft" code which basically checked whether the cloud is connected (e.g. periodically calling Particle.connected() and when this returns a false , say for WIFI1, to do a WiFi.disconnect(); followed by WiFi.clearCredentials(); , WiFiCredentials credentials("WIFI2", password); and again WiFi.setCredentials(credentials); and WiFi.connect(); . While is technically feasible, it seems not advisable, after I read the caveat brought forward by @ScruffR (due to the wearing out of the memory used for the credentials. The thread is now two years old and @engr.pron mentioned he made a feature request. Are you, @engr.pron or @ScruffR, aware whether such a feature has been implemented in the meantime? If not, is the "memory" wear really an issue? In other words, does anyone know a ballpark figure how many cycles of WiFi.clearCredentials(); / WiFiCredentials credentials("WIFI2", password); and WiFi.setCredentials(credentials);` can be safely executed by a Photon or Argon before they actually become unusable/unreliable?
Flash wear is a real thing, but if you don't expect 10k~100k of rewrites over the lifetime of the device you shouldn't need to worry too much - but always good to be aware of things to make a educated decission.