Connecting to wifi from code

I’m working on a Photon project where I allow a user to select a ssid, type in a password, then connect. The said selection and password entry are working. However the wifi connecting process is very unstable and sometimes crashes the device.

I’m calling the following functions in order:

WiFi.clearCredentials();
WiFi.disconnect();
WiFi.setCredentials(“ssid”,“password”);
WiFi.connect();

What is the best way to disconnect and then connect to a network given an ssid/password pair?

There are other discussions on this forum about the use of .setCredentials() causing issues when you don’t also specify the encryption protocol (i.e. WEP, WPA2, etc.) Not specifying the protocol will cause the Photon to take several seconds to figure out what the protocol is. Not sure about crashing though. Also, is the SSID hidden? If so, the protocol is mandatory as per the docs. I don’t have too much experience on this but wouldn’t it make sense to disconnect from the WiFi before calling clearCredentials()?

Is it possible to add some serial debugging input to your code to pinpoint which call actually crashes the system?

3 Likes

Also, clearing credentials will cause wear on the credentials storage if done often. Adding credentials allows you to store up to 5 different sets in a FIFO style storage. So adding a sixth pushes the oldest set out to make room.

1 Like