Keep permanent Wifi credentials

We have a device where we would like to always have a default wifi network configured. On the P1 we can do this:

  // Check that default WiFi is configured
  WiFiAccessPoint ap[5];
  int found = WiFi.getCredentials(ap, 5);
  bool defaultWifiOK=false;
  for (int i = 0; i < found; i++) {
      if (strcmp(ap[i].ssid,prefix)==0) defaultWifiOK=true;
  if (!defaultWifiOK) {
    WiFi.setCredentials(prefix, prefix, WPA2, WLAN_CIPHER_AES_TKIP);

On the P2, i can not get this to work.
How many networks can we store in the P2? In the P1 i believe it was 5.
How can i make sure that the default wifi is always configured, and still have room for at least 1 other network?

The P2 and Photon 2 can store 10 credentials, the same as the Argon.

That code should work on the P2 assuming your default network password is the same as the SSID and is in the variable prefix.

You should use SYSTEM_THREAD(ENABLED) and also use SYSTEM_MODE(SEMI_AUTOMATIC). After you have updated the credentials using the code above, then do Particle.connect(). If you allow a connection to start before you've added the credentials the device will go into listening mode (blinking dark blue) instead.