Photon: WiFi.clearCredentials crashes - sometimes

My photon code is setup using

SYSTEM_MODE(MANUAL)
SYSTEM_THREAD(ENABLED)

We have communication between a host processor and the Photon over i2c.
One of the functions is to set/reset the Wifi Credentials.

The code to do this is called from the loop() routine.

WiFi.on();
delay(100);
WiFi.disconnect();

Log.info("Clearing Credentials ");
WiFi.clearCredentials();
int retry = 5;

do
{
   Log.info("Setting new Credentials ");
   WiFi.setCredentials(req[0].c_str(), req[1].c_str());
            
   Log.info("Set Credentials done");

   if (!WiFi.hasCredentials())
   {
       Log.info("Failed to set credentials: %d", retry);
       retry--;
       if (retry == 0)
       {
          return (REG_STATUS_FAILED);
       }
       else
       {
           WiFi.off();
           delay(500);
           WiFi.on();
           delay(100);
       }                           
     }
     else
     {
         retry = 0;
     }
                            
 } while(retry);
          

// now do connection
    WiFi.on();
    if (WiFi.hasCredentials())
    {
        Log.info("StartWiFiConnection");
        WiFiAccessPoint ap[5];
        int found = WiFi.getCredentials(ap, 5);
        for(int i=0; i<found; i++)
        {
             Log.info("ssid: %s", ap[i].ssid);
        }
        Log.info("requesting connect skip listen");
        WiFi.connect(WIFI_CONNECT_SKIP_LISTEN);
    }
    else
    {   
        Log.info("No credentials");    
        WiFi.off();
    }


The loop for setCredentials is because nearly every time the first call to setCredentials fails to save (hasCredentials() returns false). Second attempt succeeds.

Most of the time this works fine.
But sometimes (often enough to be an issue) it never returns from clearCredentials(). The LED is solid blue, and however long I wait the code never returns from clearCredentials().

Any ideas?
How to fix?

@ajmilford
Welcome to the Particle Community.

Perhaps you could explain exactly what you are setting out to achieve with your application and which Device OS you are using. Also, posting all your code using Share Revision feature in the web IDE?

I am not clear why you are using SYSTEM_MODE(MANUAL) rather than SYSTEM_MODE(SEMI-AUTOMATIC);

Also, after you call WiFi.clearCredentials(); it would be worthwhile giving the wifi module a small amount of time delay(50); to perform the clear and then checking WiFi.hasCredentials() again before proceeding.

Where you set credentials it is not clear how you have defined req[0] and req[1]. The function takes const char * so I presume you are defining as strings and then converting to c-string using .c_str() method? Have you tried just using a constant string like “your_SSID” and “your_password”?

The two parameter WiFi.setCredentials() overload requires the repsective network to be present and visible and you should check the return value (boolean) to see whether the call worked at all.

Power-cycling the WiFi module a few milliseconds after setting credentials may also help getting the settings to stick first time.

1 Like

What I am trying to do is to set the wifi credentials for a manual connection to a wifi access point. Our application does not use the Particle cloud. We merely use it as a means to connect our appliance to the wifi network (and the module has custom firmware which does some communication both to the appliance and to a server and to a mobile app in a tablet on the same wifi network). The system has to be able to set the wifi credentials to match the wifi access point available. These are set by the user on the UI of our appliance and sent over i2c to the Photon firmware. All this is not an issue - it works.

A delay after clearCredentials would be irrelevant as the problem I have is that sometimes it never ever ever returns from the call to clearCredentials! So it would not get to the delay.

The credentials are fed into req[0] and req[1] via my i2c code - again irrelevant to the question at hand - when clearCredentials fails to return req[0[ and req[1] never get a chance to be used!

I am not really concerned with having to call setCredentials more than once - the loop I have implement sorts this issue - my issue here is that sometimes clearCredentials() fails to return and the Photon is totally locked up. The only way to recover is to power cycle which is not acceptable in a consumer appliance.