3 WLAN Accounts with set.credentials

I would like to be able to equip a preconfigured Photon with the access data of my friends WLAN.
For this I have created with set.credentials in total 3 accounts.
If I visit my friend, the photon flashes quickly green, but does not go online.
An output at the serial interfaces for control requires an online connection.
Do I have a thought-failure?

void setup() {
	Serial.begin(9600);
	
  // Connects to a network with a specified authentication procedure.
  // Options are WPA2, WPA, or WEP.
  WiFi.setCredentials("FRITZ!Box 7362 SL", "123", WPA2);  // Friend 1
  WiFi.setCredentials("Dagobert", "456", WPA2);                  // Friend 2
  WiFi.setCredentials("Ingo Heim", "789", WPA2);                 // Friend 3
    
   WiFi.connect(); 
   while(Serial.read() < 0 && millis() < 10000) Particle.process(); // this waits here till you connect via Serial and hit a key (or max 10sec)
  
  if (!waitFor(WiFi.ready, 10000))
    Serial.println("Not able to connect to WiFi for 10sec");
  else
  {
    Serial.print("Here we go SSID: ");
    Serial.println(WiFi.SSID());
  }
}

To store non-present network credentials you also needto provide the WLAN_CIPHER parameter (4-parameter overload of WiFi.setCredentials())

You also need to have the WiFi module on before you can store credentials.

And always storing the creds on each startup anew isn’t too good practice either.

And of course you need a non-AUTOMATIC system mode and/or SYSTEM_THREAD(ENABLED)

@Postler, you may want to use waitFor(WiFi.connected, 30000); after the WiFi.connect() to give the Photon up to 30secs to connect, which is a typical time to wait.

1 Like

a.) no success with
WiFi.connect();
waitFor(WiFi.connecting, 30000);

b.) With SYSTEM_THREAD(ENABLED) on top the temp-measurement begins and my Display works,
but the WIFI-Connection is not stable - further breathing fast green.

c.) IMO WiFi is always on as Photon standard.

d.) Cipher params are unknown - my friends using a Fritz!Box - untested at the moment.

e.) If I place WiFi.setCredentials outside setup() I got this error: ‘WiFi’ does not name a type

Any working example available?

My friend has problems with moisture in his apartment. I like to lend him my photon with a config gladly for a time, so that a time series of measurements can be recorded.

Have you considered using SoftAP to set WiFi creds on demand?

No, not in non-AUTOMATIC system modes and in AUTOMATIC your code won't run before the device has successfully connected to the cloud (unless you use threading).

When using WPA2 you can use AES+TKIP cipher

That is normal. These functions have to be called from within a function.

1 Like

Thx for clarification.
Have no experience with softAP: against a hard coded deposit of the access data speaks nothing: these lie to me.

this tested at the moment:

SYSTEM_THREAD(ENABLED)
void setup() {
	Serial.begin(9600);
  WiFi.setCredentials("FRITZ!Box 7362 SL", "123", WPA2, WLAN_CIPHER_AES_TKIP);  
  WiFi.setCredentials("Dagobert", "456", WPA2, WLAN_CIPHER_AES_TKIP);         
  WiFi.setCredentials("Ingo Heim", "789", WPA2, WLAN_CIPHER_AES_TKIP);              
    
   WiFi.connect(); 
   
   waitFor(WiFi.connecting, 30000);  
   
//   while(Serial.read() < 0 && millis() < 10000) Particle.process();
  
  if (!waitFor(WiFi.ready, 10000))
    Serial.println("Not able to connect to WiFi for 10sec");
  else
  {
    Serial.print("Here we go SSID: ");
    Serial.println(WiFi.SSID());
  }
}

Why not have him setup the device using the app, or setup.particle.io?

This is not very comfortable but corresponds to my backup strategy, Moors7.
If I leave the setting-up process to an unfettered third party, a lot can go wrong.
My friends would benefit from it when I prepare it with theres credentials, turn on and go.
In fact, I do not understand why it does not work with set.creds.
The installation process is certainly not left to the user, especially in the case of larger installations.
I will check softAP.

I wonder if the spaces in the SSID names cause problems. I would put underscores instead of spaces. Also, you are looking for the wrong status in the waitFor(). What you have will almost immediately pass the waitFor since it is “connecting”. You want to wait for an actual connection with WiFi.connected:

if (!WiFi.connected, 30000) {
  Serial.println("Failed to connected to WiFi");
  // other actions
}
SYSTEM_THREAD(ENABLED)
void setup() {
	Serial.begin(9600);
  WiFi.setCredentials("Dagobert", "345", WPA2, WLAN_CIPHER_AES_TKIP);          
  WiFi.setCredentials("Ingo Heim", "678", WPA2, WLAN_CIPHER_AES_TKIP);              

   WiFi.connect(); 
   
   if (!waitFor(WiFi.connecting, 30000)) {
     Serial.println("Failed to connected to WiFi");
   } else
   {
     Serial.println("Here we are connected");
   }
  
  if (!waitFor(WiFi.ready, 10000)) {
    Serial.println("Not able to connect to WiFi for 10sec");
  } else
  {
    Serial.print("Here we go SSID: ");
    Serial.println(WiFi.SSID());
  }

a.) I delete the first credential setting with "SSID with blank".
b.) I changed the WiFi.connection

photon blinks fast green. same as before.

I´m wondering that the time function update the clock during blinking green.
I thought an cloud-connect is needed therefore.

@Postler, I referred to Particle.connected() which is show as WiFi.connected()! My bad.

I would use WiFi.ready() only with waitFor() since when true, the WiFi is connected AND the Photon has an IP. Set the waifFor() time for 30 seconds and that should do it.

Setting the credentials each time is not necessary since they are stored until cleared.

That will not "clear" the credentials. Instead clear all the credentials by holding the SETUP button down for more than 10 seconds. Then run your code with the setCredentials() only if there are no credentials set:

if (!WiFi.hasCredentials()) {
  WiFi.setCredentials("Dagobert", "345", WPA2, WLAN_CIPHER_AES_TKIP);          
  WiFi.setCredentials("Ingo Heim", "678", WPA2, WLAN_CIPHER_AES_TKIP);              
}

Again, I need to point out that having spaces in the SSID may be a problem.

The timers are actually based on the interrupt driven millis() timer so ANY connectivity is not required. Try:

SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);  // Needed otherwise your code is working against the system firmware!

void setup() {
   Serial.begin(9600);

   if (!WiFi.hasCredentials()) {
    WiFi.setCredentials("Dagobert", "345", WPA2, WLAN_CIPHER_AES_TKIP);          
    WiFi.setCredentials("Ingo Heim", "678", WPA2, WLAN_CIPHER_AES_TKIP);              
   }     

   WiFi.connect(); 
   
   if (!waitFor(WiFi.ready, 30000)) {
     Serial.println("Failed to connected to WiFi and get IP");
     while(1) Particle.process;   // freeze here but allow system firmware to run for OTA
   } else
   {
    Serial.print("Here we go SSID: ");
    Serial.println(WiFi.SSID());
  }
}
1 Like

My P is now in status “Cloud not connected” after i flashed with this creds: first my own “Ingo_Heim” and second line with “Dagobert” (my friend creds).

I then put it into the safe mode (with setup + released reset) and via smartphone app “Particle” I was able to manually enter my own credits again. After new own access I flashed again without sketch changes and I landed again in “Cloud not Connected” mode.
It must be because of me that it does not work.

@Postler, if you are looking for internet AND cloud connectivity then I misunderstood. The code only attempts a WiFi connection. If you want both WiFi and cloud connection then you need some minor changes:

SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);  // Needed otherwise your code is working against the system firmware!

void setup() {
   Serial.begin(9600);

   if (!WiFi.hasCredentials()) {
    WiFi.setCredentials("Dagobert", "345", WPA2, WLAN_CIPHER_AES_TKIP);          
    WiFi.setCredentials("Ingo Heim", "678", WPA2, WLAN_CIPHER_AES_TKIP);              
   }     

  Particle.connect();   // This will connect to WiFi then to the Particle Cloud
   
   if (!waitFor(Particle.connected, 30000)) {
     Serial.println("Failed to connected to WiFi and Cloud");
     while(1) Particle.process;   // freeze here but allow system firmware to run for OTA
   } else
   {
    Serial.print("Here we go SSID: ");
    Serial.println(WiFi.SSID());
  }
}

Yep, i like both: WiFi and cloud connectivity for my own SSID “Ingo Heim” and in case of comfort for my friend “Dagobert”.

SYSTEM_THREAD(ENABLED)
SYSTEM_MODE(SEMI_AUTOMATIC);  // Needed otherwise your code is working against the system firmware!

void setup() {
	Serial.begin(9600);
 
  if (!WiFi.hasCredentials()) {
  WiFi.setCredentials("Ingo_Heim", "xy", WPA2, WLAN_CIPHER_AES_TKIP);               
  WiFi.setCredentials("Dagobert", "abc", WPA2, WLAN_CIPHER_AES_TKIP);            
  } 
  WiFi.connect(); // This will connect to WiFi then to the Particle Cloud
   
  if (!waitFor(Particle.connected, 30000)) {
     Serial.println("Failed to connected to WiFi and Cloud");
     while(1) Particle.process;   // freeze here but allow system firmware to run for OTA
   } else
   {
    Serial.print("Here we go SSID: ");
    Serial.println(WiFi.SSID());
  }
   
/*  
   if (!waitFor(WiFi.ready, 30000)) {
     Serial.println("Failed to connected to WiFi for 30sec and get IP");
     while(1) Particle.process;   // freeze here but allow system firmware to run for OTA
   } else
   {
    Serial.print("Here we go SSID: ");
    Serial.println(WiFi.SSID());
  }
*/

After startup for more then 10 sec i got the blue one and transmitted via the Smartphone app my WiFi data.
After I entered the WLAN key, I am manually online and ready to flash.
Flashed “Ingo_Heim” with underscore and P is now breathing slow green (imo cloud not connected mode).
Also get “Failed to connected to WiFi and Cloud” by serial mon.

Update: I saw a mistake from me: Particle.connect(); - change it immediately and P is connected. Puh.
Next day I´m going to my friend - standby :wink: Thank you for your support up to here.

1 Like

Today I was on a cup of coffee with my friend for the test of the prepared WLAN access: I started the footpath with a breathing normal Photon - my connection became weaker - and the P waved into the green flashing.
Property then in the corridor of the friend “setup” longer than 10 sec pressed to blue flickers.
Power off and on and had hoped that then the credentials from the sketch would be used.
Have the message “Failed to connected to WiFi and Cloud” received. Well, it was worth it.

Solved my needs with softAP. No more action necessary. Thx all!

2 Likes

What’s wrong with this code? I’m running it on a P1 but it doesn’t ever seem to run the setup function – I put a !Serial.available loop before the WiFi code but it doesn’t seem to ever get there. I’ve tried using system thread as well. I’m on 0.6.2:

#include "application.h"

String SSID = "ssid";
String PASSWORD = "pw";
uint SECURITY = 3;
uint CIPHER = 1;

SYSTEM_MODE(SEMI_AUTOMATIC)

void setup(void) {
    Serial.begin(115200);
    Serial1.begin(19200);

    WiFiAccessPoint ap[5];
    int found = WiFi.getCredentials(ap, 5);
    int hasCredentials = 0;
    for (int i = 0; i < found; i++) {
        if (ap[i].ssid == SSID) {
          hasCredentials++;
        }
    }

    if (hasCredentials == 0) {
      WiFi.setCredentials(SSID, PASSWORD, SECURITY, CIPHER);
    }

    WiFi.connect();
    if (!waitFor(WiFi.ready, 30000)) {
      RGB.color(255, 0, 255);
      Serial.println("Failed to connected to WiFi and get IP");
      while(1) {
        Particle.process();
      }
    }

    RGB.control(true);
    RGB.color(255, 255, 255);

    Serial.println("Starting");
}

void loop(void) {
}

Try adding WiFi.on() before attempting WiFi.getCredentials() - the module needs to be powered up to respond to commands.

2 Likes

I swore I tried that and it didn’t work but you saved the day @ScruffR

2 Likes

This is the same issue we have with API 0.6.4. It will not accept spaces on SSID