[PHOTON] WiFi.setCredentials() not saving in specific cases

I am running this program on a Photon with firmware v0.6.0, and two of the wifi credentials fail to save. It seems like entering in an SSID, password, and specifying WEP works just fine. However, negating the third option or replacing it with WPA2 causes it to fail. If you only specify an SSID it works fine, and if you specify a cipher for WPA2 it seems to also work. This problem also occurs with system mode AUTOMATIC. I tried to use as much demo code from the reference guide as possible.

My demo code as follows:

void setup() 
{
    delay(2000);
    WiFi.on();
    WiFi.clearCredentials();
    WiFi.setCredentials("My_Router_Is_Big"); //stored
    WiFi.setCredentials("My_Router1", "mypasswordishuge"); //failed
    WiFi.setCredentials("My_Router2", "wepistheworst", WEP); //stored
    WiFi.setCredentials("My_Router3", "wepistheworst", WPA2); //failed
    WiFi.setCredentials("SSID", "PASSWORD", WPA2, WLAN_CIPHER_AES); //stored
    Serial.begin(9600);
    WiFiAccessPoint ap[5];
    int found = WiFi.getCredentials(ap, 5);
    Serial.printlnf("Stored Credentials: %d", found);
    for (int i = 0; i < found; i++) 
    {
        Serial.print("ssid: ");
        Serial.println(ap[i].ssid);
        // security is one of WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA, WLAN_SEC_WPA2
        Serial.print("security: ");
        Serial.println(ap[i].security);
        // cipher is one of WLAN_CIPHER_AES, WLAN_CIPHER_TKIP
        Serial.print("cipher: ");
        Serial.println(ap[i].cipher);
    }
}

void loop() 
{
} 

Here is the output of the code above:

ssid: SSID
security: 3
cipher: 1
ssid: My_Router2
security: 1
cipher: 0
ssid: My_Router_Is_Big
security: 0
cipher: 0

Thanks for the help!

Are all these networks actually available and visible at the time of execusion?
Without the WLAN_CIPHER your device needs to see the network.

Thanks for the reply.
I have tested it with two networks side by side. One was a portable hotspot from my Samsung S7, and the other was an open network with no password. Do the networks need to be present when programming their credentials into the photon?
The photon will be deployed remotely so it was my intention to be able to pre-program my clients WiFi credentials into the device before it is shipped.
Have a great day!

As I said, if you know and provide the WLAN_CIPHER your network doesn’t need to be available and otherwise it does.

1 Like

Forgive me for asking, have you seen the code example?
The wifi credentials in that example are only sometimes saving into the devices memory; they are all taken directly from the WiFi.setCredentials() reference page. The networks on site are available and broadcast their SSID, there is nothing unusual or atypical about my clients wifi setup.

Here is a snippet from the reference guide at https://docs.particle.io/reference/firmware/photon/#setcredentials

“When the Photon used with hidden or offline networks, the security cipher is also required.”

Here is also the supporting code from the WiFi.setCredentials() refrence guide:

// Connects to an unsecured network.
WiFi.setCredentials(SSID);
WiFi.setCredentials("My_Router_Is_Big");

// Connects to a network secured with WPA2 credentials.
WiFi.setCredentials(SSID, PASSWORD);
WiFi.setCredentials("My_Router", "mypasswordishuge");

// Connects to a network with a specified authentication procedure.
// Options are WPA2, WPA, or WEP.
WiFi.setCredentials(SSID, PASSWORD, AUTH);
WiFi.setCredentials("My_Router", "wepistheworst", WEP);When the Photon used with hidden or offline networks, the security cipher is also required.

// 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));

I do not understand why I would have to have the network “available” while hard coding it into the device’s wifi. According to the reference guide what you are suggesting is usually only applicable to hidden networks. I would also like to point out in the code example that three of the networks did save, even though again, all of the network examples in my code do not actually exist as real or available networks. With or without the network present, while the device flashes the wifi credentials, it will always fail to save in the cases outlined in my first post. Its regardless of the SSID of the network, or if it is currently available for an incoming connection.
In case I was not clear in my first post, the output of the code example is a direct print off of the successfully saved WiFi networks stored in the photons list of last five connections.
If you think there is something I am missing, I would really appreciate if you spell it out for me, as I do not understand your previous responses.

Again, I really appreciate your help! I have been banging me head against this for a few days now and the boss is breathing down my neck :smile:

I have seen the code and hence I commented on the visibility of the networks bacause you only have WLAN_CIPHER present in one line.

You may have to ask Cypress/Broadcom why their module needs that, but it does need all info required for that network in order to save the data.
If a network is present and visible, missing info can be acquired automatically once you can authenticate yourself (via SSID/PWD).
And the reason why some stored and others not although none of them being present (which was not indicated initially and presented otherwise in post #3) is that “all info required for that network” depends on the encryption scheme selected (open and WEP are more easily satisfied than WPA/WPA2 - since there is no WLAN_CIPHER_TKIP or _AES for open or WEP it can’t be required by definition).

BTW:

That’s only part of the story.
Since these are potentially sensitive informations they are not stored in the devices normal memory but in a specially protected area of the Broadcom/Cypress module to prevent readback of the password and hence that behaviour is hardwired into the chip.

1 Like

This is critical information that needs to be added to the documentation
@BDub

Wasted a couple of hours here today.

It is here:

https://docs.particle.io/reference/firmware/photon/#setcredentials-

When the Photon is used with hidden or offline networks, the security cipher is also required.

1 Like

How about:

Caution: When using setCredentials() for a network that is not available and online (or is hidden), the credentials will only be stored if the security cipher (ie WLAN_CIPHER_AES or WLAN_CIPHER_TKIP) is given.
Using the shortened form setCredentials(SSID, PW) causes the WiFi hardware to look for the network to determine its settings before storing them.

1 Like

By the by, my problem wasn’t even this specifically-- it was that the WiFi was turned off when I tried to store the credentials. This doesn’t seem to work at all but I’m out of time to fool with it.

The thing that I seem to keep running into is that the documentation is written as though Firmware API were simple when in fact the API is hiding a lot of complex behavior, and often this is not completely abstracted away-- so some knowledge of this is required to use it effectively.

The above is an example of an extreme gloss-over of the fact that setCredentials(SSID, PW) is an interactive process that will fail if certain conditions are not met. – it will not fail and defer until later – it will just fail.

Except for extremely simplified situations, you must know this to use it effectively. I keep tripping over this “next step” beyond the trivial case and between the issues with the tools and the issues with the API, it’s starting to get pretty frustrating.

In terms of learning curve with Particle, there are two places to live: Arduino tinkerer and GitHub trawler. There doesn’t seem to be an in-between.

I like this suggestion!

I find that it clears up the misconception I had when reading the documentation previously.
From https://docs.particle.io/reference/firmware/photon/#setcredentials

Allows the application to set credentials for the Wi-Fi network from within the code.

Makes sense to me

These credentials will be added to the device’s memory, and the device will automatically attempt to connect to this network in the future.

This leads me to believe that if I run this function, the credentials will be added to the device’s memory, then it will attempt a connection.
The use of “and” implies to me that the wifi credentials are first added to the devices memory, then the device will attempt the communication after. This is where I got hung up; many an hour had been lost on that day.:crazy_face:

When the Photon is used with hidden or offline networks, the security cipher is also required.

I find this is not clear at all, it could be interpreted either:

  • The device would save the connection settings and just fail to connect
  • the function will fail to save the network altogether. It would also be nice to know in the reference that this is a hardware limitation in the photon. :slight_smile:

My use case was to save the wifi networking information pre-device shipment so the product would be a drop in replacement for the technician installing it. Many of our clients did not know their cipher, so I wanted to omit it as usually I thought it is acquired on connection handshake.
I found it very interesting how many of our clients had no idea what a security cipher was, not to mention which they were using in their own wifi! :thinking:

1 Like