Photon - how to provide new WiFi credentials


#1

I have several systems with Photons that will be moved from location to location and I prefer not to require a laptop. I had mistakenly assumed that the Particle app could be used to provide new credentials once the Photon was in setup mode.

I’ve read through several discussions about the topic, but would like to know the current thinking of best practice to address this situation. In other words, how do I provide a Photon with new WiFi credentials without having to change the application or drag along a laptop? If there are some code examples, that would be great! Thanks in advance for newbie assistance!


#2

I’ve kind of discovered the following. If I hold the SETUP button until the light blinks blue (Listening mode), I’m then able to tap the “+” key on the Particle app and setup the WiFi credentials. I chose to use the same name as before and all seems good.

So…is this an acceptable way to provide new credentials or am I setting myself up for pain? One of my devices is in a very small enclosure that does not facilitate getting to the SETUP button. However, I can redesign around that issue if the above solution is appropriate.


#3

@ctmorrison Yes, that method is going to be the best to accomplish changing WiFi connections with out needing a laptop with you. Same goes for the one device in the small enclosure.


#4

That should indeed work. That’s kind of how it’s intended to work as well. Entering listening mode allows you to (Re)configure your credentials.
If necessary you could code the device to automatically enter listening mode after a certain time without a connection. That way, you wouldn’t have to access the setup button. Make sure you add a timeout to that, so unintentional network disruptions don’t cause your device to ‘hang’ in listening mode indefinitely.


#5

If you go to the wifi section of the reference documentation (https://docs.particle.io/reference/firmware/photon/) you will see that there several functions that can manipulate the stored access point login details.

From there I have developed this algorithm (below) that loads five SSID/password pairs from a string array (apGroup) that stores the required APs in preference order. This routine reverses the loading order to ensure the required priority is used.

The Photon moves automatically between these AP’s seemingly only when the signal becomes insufficient to maintain the current connection.

Rgds, Neil.
(LEDBlue is defined as D7.)

void loadAPCredentials()
    {
    Serial.print("Loading ...");
    flashLED(LEDBlue, 500);
    WiFi.disconnect();
    WiFi.clearCredentials();
    for(int loopCount = 4; loopCount >= 0; loopCount--)
        {
        reqdAP = loopCount * 2;
        WiFi.setCredentials(apGroup[reqdAP], apGroup[reqdAP + 1]);
        }
    WiFi.connect();
    waitUntil(WiFi.ready);
    delay(1000);
    Serial.print("\rConnected to ");
    Serial.println(WiFi.SSID());
    }

#6

@Neil_Mudford, can you post the entire working code example for this? It looks like a great solution and I’d like to investigate it further :slight_smile:


#7

Hi everyone !

Is someone had figured out how this code works ? I’m trying to understand and he should be really great to have a code like that to add new credentials to our beloved boards.

For now, I retro-engineering this part of the code :

char* SSID = "your SSID";
char* PASSWORD = "your PASSWORD";

#define led1 D7

void setup() {

Serial.begin(9600);
Serial.print("Loading ...");
pinMode(led1, OUTPUT);
flashLED();
WiFi.disconnect();

WiFi.on();

waitFor(Serial.isConnected, 10000);

WiFi.setCredentials(SSID, PASSWORD);
}

void loop() {

while (!WiFi.ready()){
    Serial.println("Attempting to connect to WiFi...");
    delay(1000);
    Serial.println("-----------------------------");
    Serial.println("Try again in 5 seconds...");
    delay(5000);
}

Serial.println("Photon is up in the sky !");

}

int flashLED() {

for (int i = 1:5) {
    digitalWrite(led1, HIGH);
    digitalWrite(led1, LOW);
    delay(250);
}

} 

What about the waitFor part ?? what is this function ?

And the more important, I would like to know how to load a code in my photon without using the WiFi.

Cheers


#8

Have a look at the docs :wink:

The CLI along with DFU util should be able to help you there. Again, have a look at the docs for further instructions :slight_smile:


#9

Yeah tks Moors7 but I dont have all the knowledge to understand all the informations in the docs sometimes. And I’m a bit in a rush for a school project… :wink: I’ll give another look so.


#10

https://docs.particle.io/reference/firmware/photon/#waiting-for-the-system

I couldn’t have explained it any simpler though…

As for flashing locally, again, the docs sums it up pretty nicely…
https://docs.particle.io/reference/cli/#compiling-remotely-and-flashing-locally


#11

I’m actually a bit stumped by the use of waitFor() in conjunction with Serial.isConnected but just reading it literally, this would wait for up to 10 sec (=10000 milliseconds) for a serial connection via USB before proceeding to catch all the output.
Not too difficult to grasp IMHO


#12

grasp IMHO ?


#13

grasp = get hold & understand
IMHO = common acronym In My Humble Opinion


#14

tks guys for your quick responses !


#15

If it’s not too late to answer, here is the code which I use to flash new WiFi credentials.

  1. If you are connected to WiFi already and want to switch to a different WiFI, compile and flash this code with your WiFi credentials

#define WIFINAME “myWIFI”
#define PASSWORD “myPASSWORD”

SYSTEM_MODE(MANUAL)

void setup() {
WiFi.on();
if(WiFi.hasCredentials())
{
WiFi.clearCredentials();
}

WiFi.setCredentials(WIFINAME, PASSWORD);
Particle.connect();

}

void loop() {
if(Particle.connected() == false){
WiFi.off();
delay(500);
WiFi.on();
delay(500);
Particle.connect();
}
Particle.process();
}

  1. If you switched to a zone where you don’t have access to WiFi, put the particle in DFU-util mode (flashing yellow) and then

compile using: particle compile photon file.ino

flash using: particle flash --usb firmware.bin

where file.ino is the example script saved as file.ino and firmware.bin is the file downloaded from particle cloud after compiling via particle cli.


#16

This will erase/write the credentials with every run of setup, which might wear out the respective memory. You might want to avoid running it like that without further checks.


#17

I run this just once to update my WiFi credentials and flash my program.


#18

I recently wrote a blog post that demonstrates how to set wifi credentials via code on your Particle device. Check it out to get some clarification on how best to set wifi credentials from within your Photon code.


#19

Sorry if this has been asked before - and it must have - just cant find the concise answer…
I need to ‘OWN’ all photons that get shipped - to enable OTA updates etc.
The customer must be able to set their own wifi credentials - obviously, but not take ownership.

I assume using the particle ‘apps’ it requires a particle account, and to be logged in, and to have ownership of the photon - or am I wrong?

At worst case - we have access to the Uart so could poke the data in that way using the hosting products display, buts it’s cumbersome as there is no qwerty keyboard available.

Did I miss the best practise in this instance because once the photon leave our premises we are relying on the end user to be able to get it onto their own wifi network.


#20

Hi @rowifi -

This is something that I encounter often as I seldom am “on site” when products are delivered. More so, I do not want to open products on site.

Quick question;

Do you have the WiFi Credentials of your client prior to shipping? I use one of two methods at this stage, I can offer either if I know whether you have this information.