WiFi.setCredential sometimes fail to set credential

photon
Tags: #<Tag:0x00007fe222576c08>

#1

Hello

I am using “WiFi.setCredentials” to set credentials in P1. By same wifi credentials most of time it set credential but sometimes it’s not.

I am passing wifi credentials using our custom mobile app which send command to P1 via bluetooth so credentials given from mobile app to P1 is correct always. If any case sensitive/wrong psw issue then app give indication to user so credentials is always correct.

Also one question, What is return value of “WiFi.setCredentials”?

Thanks.


#2

Sorry but your post is a bit confusing. Are you referring to P1 or Photon? Then you mention bluetooth, neither P1 nor Photon supports bluetooth!

I suggest looking at the reference documents - there is no return value for WiFi.setCredentials() instead you would check after using WiFi.setCredentials() if it has succeeded with WiFi.hasCredentials() which returns true or false. If you are adding many credentials then you would need to use a different approach.


#3

Hi @armor

I am using P1.

Bluetooth is connected to P1 via serial.

I am passing one credential only and also clearing credentials before set.

Yes. I checked in doc but when set new credential succesfully then it is giving “1” as return value otherwise “0”.

//my code part//
/////////////////////////////////////////////////

  if(WiFi.hasCredentials())
  {
    WiFi.disconnect();
    if(WiFi.ready())
    {
      delay(2000);
      WiFi.disconnect();
    }

    if(WiFi.clearCredentials() == 0)
        Serial.println("failed clear credentials... ");
    else
        Serial.println("Previous wifi credentials CLEAR... ");
  }
  else
   Serial.println("NO previous wifi credentials avaialable... ");
   
  if(WiFi.setCredentials((const char*)gxWiFiInfo.uWifiSSID, (const char*)gxWiFiInfo.uWifiKey) == 1)
  {
    WiFi.listen(false);
    WiFi.connect();
     Serial.println("succeed to set credentials... ");

    if(WiFi.hasCredentials())
      Serial.println("New wifi credentials present - if... ");
    else
      Serial.println("New wifi credentials NOT set - if... ");
  }
  else
  {
     Serial.println("failed to set credentials... ");

    if(WiFi.hasCredentials())
      Serial.println("New wifi credentials present - else... ");
    else
     Serial.println("New wifi credentials NOT set - else... ");
  }
//////////////////////////////////////////////////////////////////////////////////////////////

#4

I would rather use waitFor(); after trying to connect. You also might try switching off and on the wifi module and a short delay (50) afterwards when clearing credentials. Also, using SYSTEM_THREAD(ENABLED);

I assume you are using the latest device OS.

Lastly, on the return value from setCredentials method - I thought that this was defined as type void but I can’t check the source. I assume it is just returning a non null value!


#5

I tried using waitFor(); but before that if fails to set credentials.

Yes. i do.

Yes latest 1.4.0.

I also assume it should not return any value because nothing written about that on particle documents but as i observe by doing many times wifi credentials set, when it fails to set credentials (at that time also it is not able to connect with wifi) at that time it return “0” otherwise it always return “1” when it succesfully set credentials(at that time also it succesfully connect with wifi).


#6

Nope, it’s returning a bool
See here


#7

Hi @ScruffR

Any permanent solution for wifi credentials update using code?

Here, with some times it is working with first try, sometimes after 2-3 times and for other persons it is not working after many retry.

Even on forum and not other place also not any example or more help available for wifi credentials set using code.

Please help on this if possible because facing this issue in many devices with different router/location.

Thanks.


#8

Hello @rickkas7,

I am using

WiFi.setCredentials(ssid, password);

So Does this work always for router security - “WPA-PSK/WPA2-PSK” and Auth - “Automatic”?


#9

The two parameter version of WiFi.setCredentials() has to have the network available and visible to actually connect and store the creds.
If you can’t be sure of that you’d need to use the four parameter overload with explicitly stating the encryption scheme and cipher.
WEP and open networks are supported too, but for WEP the password has to be provided in a praticular way (I can’t quite cite off the top of my head :blush:)

Additionally captive portals are not supported at all.


#10

Hello,

I noticed one more thing.

If P1 is already connected with one wifi and then if require to connect with other wifi then i am first doing “WiFi.Clearcredentials()” also i checked if it has credentials available or not after clearing it.

And then if i set wifi credential with wrong password then it also set credentials and it still connected to first wifi so it looks like system got connected to second wifi but it is not.

If i power of and again then at that time it clear first wifi credentials and now second wifi credentials are wrong so it will not connect with wifi.

So in short after clear credentials it still present. once P1 reboot then after it cleared.
That makes confuse to user.

Is there any way to clear credentials instantly without P1 reset?

Thanks.


#11

WiFi.clearCredentials() does only remove the credentials from the inventory of known networks but does not remove the currently active creds from the active storage of the module.
If you want to make the module discard that info from its “short term memory” too you’d want to call WiFi.disconnect() before clearing the creds.

Having said this, my advice would be to reconsider the actual need for WiFi.clearCredentials() only for the sake of adding a new one.
Also delay() doesn’t really help in respect of giving the new creds time to propagate through the system. A better approach would be (as mentioned by others) waitFor() or even a tight loop like this

  for(uint32_t ms = millis(); millis - ms < delayTime; Particle.process());

This will throw all controler time into the tasks related to the radio module.


#12

Ok. got it.

Ok i will try this and let you know result.

Thanks


#13

I tried this and this is not good idea at all. It works perfectly if credentials are correct. If password is wrong then it always stuck in wifi off state and even my code is also not running. It stuck somewhere.

If i reboot P1 then led status is “White - wifi off” so then i have to configure wifi using Particle app.

So turning off/on wifi during configuration is not good idea.


#14

Hello,

Wifi configuration working good with below settings.
image

but not working with below settings.
image

Any idea?

Thanks.


#15

You’d use SYSTEM_THREAD(ENABLE) and SYSTEM_MODE(MANUAL) to prevent that.


#16

With regards to the quote - it wasn’t an option - you need to setup under SYSTEM_THREAD(ENABLED) and either SYSTEM_MODE(MANUAL) or SYSTEM_MODE(SEMI_AUTOMATIC) as ScruffR has suggested. Also, the turning off of the WiFi module was to forget the current details as ScruffR has also mentioned. It is important to understand how the wifi module works in these different modes and what actions are blocking and non-blocking. Unfortunately, this has changed over time with different Device OS and is not accurately reflected in the documents. There isn’t a ‘model’ code example which handles all possible conditions.


#17

I am using SYSTEM_THREAD(ENABLED) in current code.

so is it also require SYSTEM_MODE(MANUAL)?


#18

SYSTEM_MODE(MANUAL) is not a hard requirement, but prevents the system from just “blindly” trying to connect. It allows your code to first check prerequisites and then consciously trigger a connection attempt.

It also prevents the device from falling into the Listening Mode “trap” if there aren’t any WiFi creds available in the inventory.


#19

That really depends upon what scenarios you are trying to cover. I use SEMI_AUTOMATIC but also maintain certain flags - are there credentials, are credentials bad, is network out of range. these can only be tested for at certain times and with certain modes. The advantage of SEMI_AUTOMATIC over MANUAL is that I have the control to connect and make these tests whilst doing so and also can let the system reconnect if it loses connection and then finds a connection again.


#20

Ok.

I think it is better to not use SYSTEM_MODE because it is production firmware.

I just tried with one of my development P1 and now it is breathing green from almost half hour so somehow not able to connect with cloud even if i change wifi or restart router. :roll_eyes:

Then i have to update firmware using STLink and now it’s connected, blinking cyan.:sweat:

Sorry it’s breathing cyan.