Explanation on how to use setCredentials()

I’m trying to add WiFi credentials directly through code. However looking at the documentation I am unsure of how to actually use setCredentials(). I know this is a very elementary question and I feel dumb for not figuring it out myself. I feel like WiFi.setCredentials() is an internal function however when I try to use it it is acting like I have to write the function. Any help is appreciated.

p.s I really am liking the Photon. lol :smile:

The docs do provide samples too

e.g.

WiFi.setCredentials("thisIsYourSSID", "ThisIsYourPASSWORD");

(you’d only need to do this once, since the info will be stored in flash)

Only if you want to add credentials of a not present network, it gets a bit more hairy :wink:

Or am I not getting what your actual problem is :confused:

1 Like

Wow…yea i was overthinking the entire problem…You again were right. Like always :wink:

So after I flash the code with that in it I technically remove it from the code because those credentials are now stored?

3 Likes

Yup, that’s how you’d do that.
If this set of credentials is the only one to be stored, you could check for WiFi.hasCredentials() and only do the setting thing if that returns false (this’d only work in non-AUTOMATIC modes or in STARTUP() before the forced Listening Mode gets triggered)

Or you set an EEPROM flag to indicate your credentials had been set already.

ScruffR: You said it gets a bit more hairy if network not present. How?!

Let me describe what I want to do.

I run Automatic. When I bought the Photon, I set it up to work on my router, SSID = AAA.

It works great with AAA. I bought a range extended hardware to get wifi elsewhere on the property; it’s SSID is BBB.

I want to write some code which will execute one time only and set credentials for BBB. I will do this from a place where both AAA and BBB exist.

So far, I think no problem.

I want to travel to a place away from my house where there is a router CCC and neither AAA nor BBB are present.

This is what your post applied to, I think. “… a bit more hairy.” Any broad brush ideas on how it’s hairy? Or how to make it work?

There’s mention of an EEPROM flag trick. Is that documented or explained anywhere?

Thanks,
–jim

It’s not really that complicated. You just need to know a bit more about network.

The docs do provide this sample

// for hidden and offline networks on the Photon, the security cipher is also needed
// Cipher options are WLAN_CIPHER_AES, WLAN_CIPHER_TKIP and WLAN_CIPHER_AES_TKIP
WiFi.setCredentials("SSID", "PASSWORD", WPA2, WLAN_CIPHER_AES));

While the required WLAN cipher can be assessed by “listening” to a present network, this is not possible for a non-present, so you have to provide that info beforehand.

1 Like