Hello,
I am using particle P1. in code what i am doing receiving ssid and password of wifi on serial and setting it using WiFi.setcredentials(). first time it works well. but when i use it second time it get stuck at WiFi.setcredentials() function and not setting credentials also. after that it need reset only. i tried with adding WLAN_CIPHER_AES in argument as i found that i will store first credentials. but after adding this what i get led just blinking green. not connecting to network, i am not able to keep P1 in listen as well as safe mode. nothing was working even after reset also it blinks green and in last i have programmed bootloader. my problem is why it get stuck on that function? is there any solution for that? is there any solution that i should not have to upload bootloader?i have added SYSTEM_THREAD(ENABLED);
I use P1’s a lot and I also had some issues using setCredentials (though I presume it’s the same with P0’s). One thing you can try is to do a System.reset() after setting the credentials, and see if that works. It would require some tweaking of that code of course, so that it does not clear the newly set credentials over again, but it’s worth a try.
Instead of doing this via code, I now just flash it as part of the setup procedure for all my customers. I’m using an extended version of this script https://github.com/nrobinson2000/ParticleDetect where I’ve added our custom cloud registration at the end there. Could that be helpful? Flashing credentials via serial works really well.
Which Device OS are you using? I am using Photons, but assuming you have the right circuit around the P1 it should behave the same.
I have 2 main product code bases; one uses softAP for WiFi setup however around this there is a user initiated process, the other product uses a selection of SSID from a scan and then input of the password, the security/cipher having been picked up from the scan.
A couple of things I have learned; only 0.6.3 and 0.8.0-RC.11 work properly.
I use SYSTEM_MODE(SEMI_AUTOMATIC) and SYSTEM_THREAD(ENABLED).
From setup() - I switch on WiFi first (not WiFi.connect()) and then check WiFi.hasCredentials(). I keep a note of the result. I then call WiFi.connect() if there are credentials stored waitFor(WiFi.ready, 5000) then check if Wifi.ready() and then call Particle.connect() followed by a waitFor(Particle.connected, 5000). This could all be simplified to just Particle.connect(); it depends upon whether that level of control is required.
I have found that it is necessary to disconnect the WiFi before WiFi.clearCredentials(); (as you are doing) but with a check that !WiFi.ready(); - it has disconnected and to do a WiFi module power bounce after WiFi.off(); delay(20); WiFi.on(); delay(20); otherwise I find that it may not clear the credentials every time.