[SOLVED] setCredentials not saving WiFi credentials

Can anyone answer why WiFi.setCredentials() is not working for me (under firmware 0.4.9 and 0.5.0)?

I have researched the forums on this topic with no joy. It seems that this API is working for others.

The following code's purpose is to ensure that a default WiFi network "MySSID" is always loaded into the Photon. It only sets the network credentials if it is not loaded (so as to avoid flash wear).

SYSTEM_MODE(SEMI_AUTOMATIC);

void setup() {

    bool bHaveMyWiFi = false;
    char * szp = "????";


    delay(2000L);


    WiFiAccessPoint ap[5];

    int nFound = WiFi.getCredentials(ap, 5);

    for (int i = 0; i < nFound; i++) 
    {
        Serial.print("    SSID: ");
        Serial.print(ap[i].ssid);

        // Security 
        switch(ap[i].security)
        {
            case WLAN_SEC_UNSEC: 
                szp = "NONE";
                break;

            case WLAN_SEC_WEP:
                szp = "WEP ";          
                break;

            case WLAN_SEC_WPA:
                szp = "WPA ";
                break;

            case WLAN_SEC_WPA2:
                szp = "WPA2";
                break;
        }

        Serial.print(" Security: ");
        Serial.print(szp);

        // Cipher
        szp = "????";
        switch(ap[i].cipher)
        {
            case WLAN_CIPHER_AES: 
                szp = "AES";
                break;

            case WLAN_CIPHER_TKIP:
                szp = "TKIP";          
                break;

            case WLAN_CIPHER_AES_TKIP:
                szp = "TKIP";          
                break;
        }

        Serial.print(" Cipher: ");
        Serial.println(szp);

        // Check what WiFi networks are loaded
        if (strcmp("MySSID", ap[i].ssid) == 0)
            bHaveMyWiFi = true;

    }   // for ...

    if (!bHaveMyWiFi)
    {
        // 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(szp = "MySSID", "MyPASSWORD", WPA2, WLAN_CIPHER_AES);      
        Serial.print("Setting WiFi ");
        Serial.println(szp);
    }



}

void loop() {

}

Here is the console output after running the program for the first time (which is expected):

SSID: RZONE Security: WPA2 Cipher: AES
Setting WiFi MySSID

Here is the console output after every reset (not expected, it is the same as the above - there should be no "Setting WiFi MySSID" - this indicates that the credentials were not set):

SSID: RZONE Security: WPA2 Cipher: AES
Setting WiFi MySSID

I note that network "MySSID" is not in existence.

I get the feeling that there might be a "gotcha" here.

Thanks in advance - @umd

It seems to be missing in the docs, but you need to have WiFi.on() (which SEMI_AUTOMATIC hasn’t) in order to have WiFi.setCredentials() do its job.


Update:
I’ve just added a Note about this in the docs.

5 Likes

@ScruffR, advice received with thanks (as per usual).

Happy to confirm case closed - works as expected once I included WiFi.on() prior to WiFi.setCredentials().

This now allows us to set up WiFi connections for specific projects.

Cheers - Harry

1 Like