WiFi.setCredentials clarification

Looking for clarification on the correct use of WiFi.setCredentials.

A project of mine used an old Core that has worked for many months and a few days ago I added the following code to enable the Core to connect to WiFi at more than one location:

void setup() {
    
WiFi.setCredentials("SSIDA", "passwordA");
WiFi.setCredentials("SSIDB", "passwordB");
WiFi.setCredentials("SSIDC", "passwordC");

etc

I came home this evening to find my Core no longer connected to WiFi and flashing blue. After my attempts to revive it it started to “flash green without stopping” indicating that I needed to do a full firmware update. A little intimidated by all the steps involved in doing a full firmware update, I decided to replace my flaky Core with a Photon and reprogrammed it with my original code but without any of the WiFi.setCredentials. My suspicion is that the WiFi.setCredentials may have contributed to the Core freaking out so I am hesitant to add them back

Anyway - to my question at last. The WiFi.setCredentials documentation is sparse so I am not sure if I am using WiFi.setCredentials correctly. It will clearly run each time I reboot and it is not clear to me how the credentials get added and if adding them repeatedly is an issue. I assume the 5 credentials max is a FIFO stack or similar and that each time setup runs the three credentials will simply get added and three will be overwritten?

Bottom line is that I wish to add multiple WPA2 credentials to my Photon. Am I doing this correctly?

Thanks

i’m building a photon to also connect from several locations and i’m also facing the same situation.
i’ve added setCredentials in the same location with the same parameters.
the only difference is that i am declaring the Wifi Type and Encryption as well. but its still a no go.

the Photon just wouldn’t connect to the networks even though i am pretty sure they exist. (2x checked with a Wifi scanner)

i’m wondering if i did something wrong.

my code is similar

void setup() { // this is a blocking loop
    WiFi.off(); // turns off the particle connection on start to prevent being blocked at wake-up
    WiFi.setCredentials(ssid_1, pass_1, WPA2, WLAN_CIPHER_AES);    
    WiFi.setCredentials(ssid_2, pass_2, WPA2, WLAN_CIPHER_AES_TKIP);
    WiFi.setCredentials(ssid_3, pass_3, WPA2, WLAN_CIPHER_AES_TKIP);
}

any assistance and clarification would be great.

Setting credentials like this isn’t good for the lifetime of the device since it will erase flash memory on each boot in order to set the credentials.

The recommended approach is to use listening mode to configure the credentials via Serial, Mobile or a web browser.

For programmatic configuration, in 0.4.9, we will have a WiFi.getCredentails() API, so you can at least avoid updating credentials that already exist.

@mdma I am doing something similar where I want to setCredentials() on boot based on credentials stored on an SD card. I didn’t know it was bad for the lifetime of the Photon. Do you retain access to the credentials once they’re stored on the device? If so, what about something like this:

WiFi.hasCredentials(ssid_1, pass_1, WPA2, WLAN_CIPHER_AES);

That would return TRUE if credentials already exist that match the passed values. We could check for that and avoid setting new credentials if unnecessary.

If you don’t have access to them, I suppose we could do our own store & check in “EEPROM” but it would be cleaner to do the check at a lower level.

The last 5 set credentials are retained. The WiFI.getCredentials() API can retrieve the SSID and security details, but not the password.

So you might save the SSID and a checksum of the password/security type in EEPROM, and then compare that to the details stored on the SD card. Any differences mean the SD card was changed and the credentials need re-applying.

@mdma Thanks. I didn’t realize WiFi.getCredentials() worked without being connected. I think simply checking for SSID should be sufficient.

Out of curiosity, can you explain how to create a checksum of the password/security type?

EDIT: Actually I didn’t know about WiFI.getCredentials() at all. Is there documentation?

Hmm, I thought I told in the other thread


A similar suggestion to mine in the other thread :wink:


It’s a feature to come

2 Likes

hi MDMA

thanks for the advice. from a testing perspective, would this also mean that if i used WiFi.setCredentials() once, this information should be retained across reboots, and the Photon should automatically join the SSID as it works through the available SSIDs the photons sees?

and even if i comment the code across a re-flash, it should be retained?

thanks

That’s correct - credentials are retained across reboots.

You’re absolutely right. Sorry not to give you credit, but I didn’t think it was relevant. Surely you must know implicitly how helpful you are to everyone on this forum.

Of course the other thread was about an entirely different problem – a bug that I brought to Particle’s attention that will be fixed. Your solution didn’t solve that problem sufficiently for me, as I explained. A variation would solve the one we’re discussing here though, you’re right. Kudos.

Ah! I missed that. Thank you for pointing that out.

1 Like