Setting a default Wi-fi credentials before setup()

Hi, I’m developing a firmware for argon where the device should connect to an Wi-fi AP which is not necessarily connected to internet (for security reasons, I think).

It seems that the Argon device first connects to an internet, so I made it to connect to an Wi-fi AP (say A) which is connected to internet, then disconnect, and connect to the target Wi-fi AP (say B).

I found it extremely cumbersome when the Argon device, when booted up, connects to B and tries to connect to the cloud (in this case, I had to set Wi-fi credentials as ‘A’ using Particle App).

So, here’s my question:

  • Is there any methods to skip cloud connection and to start with ‘setup()’?
  • Or, can I set a ‘default’ Wi-fi credential to be used at the first stage?

I tried as shown below, but failed to go into ‘setup()’ (the device is just blinking blue).

// in \.particle\toolchains\deviceOS\3.2.0-rc.1\system\src\system_task.cpp

void Network_Setup(bool threaded)
  network_setup(0, 0, 0);

  network_clear_credentials(0, 0, NULL, NULL);
  NetworkCredentials init_credential;
  memset(&init_credential, 0, sizeof(init_credential));
  init_credential.size = sizeof(init_credential);
  init_credential.ssid = "TARGET_AP";
  init_credential.ssid_len = strlen(init_credential.ssid);
  init_credential.password = "PW_OF_TARGET_AP";
  init_credential.password_len = strlen(init_credential.password); = WLAN_SEC_WPA2;
  init_credential.cipher = WLAN_CIPHER_AES;
  network_set_credentials(0, 0, &init_credential, NULL);
  // don't automatically connect when threaded since we want the thread to start asap
  if ((!threaded && system_mode() == AUTOMATIC) || system_mode()==SAFE_MODE)
    network_connect(0, 0, 0, 0);

Yes, SYSTEM_MODE(SEMI_AUTOMATIC) will not connect automatically. Only after you called for Particle.connect() the device will attempt to connect to the cloud (or WiFi.connect() if you only want WiFi).

You cannot explicitly state which set of credentials should be used first but you can clear and (re)set the credentials stored on the device and I’d not use undocumented functions for that but rather the official WiFi.setCredentials() and WiFi.clearCredentials().

However, when the Argon tries to connect to a WiFi network it first scans which networks are available, checks against its stored set of networks and tries to connect to them the network with the strongest RSSI first.

This would indicate there are no valid WiFi credentials stored on the device.

SYSTEM_MODE(SEMI_AUTOMATIC) was just the function I needed!
Thank you.

1 Like