Connecting Photon to Local (no internet) Wifi running the Code I've uploaded via Cloud?

So, my Photon is currently connected to the cloud, but I want to take the code it is currently running, and have it automatically and continually try to connect to a specific WPA2 wifi network that is NOT connected to the internet.

There just aren’t a lot of good full code examples of this out there and the wiki is a little vague on what exactly needs to be called (wifi.on? WiFi.setCredentials();?) , and where exactly to put it in the code (before setup, in setup, in loop?)

Ideally, I’d like to be able to connect my photon to (cloud-connected) wifi network A, put my code on, and then connect it to my internal, internet-less wifi network B and repeat the process as I need to update my code.

Any words of wisdom?

1 Like

Will both networks be available at the same time/same location?

AFAIK currently there is no way to tell the device which network to connect to if there are multiple networks stored and available simultaniously.

To set your device up for cloud-less mode after a successful OTA update, I'd go for SYSTEM_MODE(SEMI_AUTOMATIC), connect to WiFi, check with WiFi.SSID() if connected to the internet capable WiFi in setup, and only if so use WiFi.clearCredentials() & WiFi.setCredentials() for isolated network once.

To go back online with the cloud, you'd either have to manually do this via Listening Mode or you provide a way to trigger a similar behaviour as above but for the connected network.

But coming up with the code for this should be possible by use of the docs.

There are infinitely many possible things you and others might want to see full code examples for each and any idea someone might have, but there is no way to provide this.

By reading the docs carefully, learning what the RGB codes tell you about the status of the device and some common sense it's not too complicated to get the idea.

2 Likes

True on the infinite examples thought :smile:

And my two networks are in two totally different locations. I just want to store one network…

@ScruffR So I’m going to break this down…So, let’s assume my cloud connected wifi SSID is wifiA, password is qwerty, and my non-cloud wifi SSID is wifiB, password is also qwerty, and let’s just take a simple blink sketch, and I’m going to (try) to add in what you suggested:

SYSTEM_MODE(SEMI_AUTOMATIC)

int led1 = D0;

int led2 = D7;

void setup() {
  WiFi.on()
  WiFi.connect();
WiFi.setCredentials("wifiB", "qwerty");

if (WiFi.ready()) {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
}
}

void loop() {
  // To blink the LED, first we'll turn it on...
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);

  // We'll leave it on for 1 second...
  delay(1000);

  // Then we'll turn it off...
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);

  // Wait 1 second...
  delay(1000);

  // And repeat!
}

Am I close?

When you say your two networks are locally separated you don’t need to bother with setCredentials() at all.
Just add both sets once via CLI or serial (or a simple one-time sketch) and you are ready to rock.

You’d only check which SSID you’re connected to either do Particle.connect() or not.

SYSTEM_MODE(SEMI_AUTOMATIC)
const int led1 = D0;
const int led2 = D7;

void setup() 
{
  WiFi.on()
  WiFi.connect();
  waitUntil(WiFi.ready); // or use if (waitFor(WiFi.connected, TIMEOUT)) { ... } 

  if (WiFi.SSID() == "wifiA")
  { 
    Particle.connect();              // prepare for OTA
    while(true) Particle.process();  // and wait forever for it
  }

  // so we are on wifiB then
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
}

void loop() { ... }
2 Likes