Multiple WiFi?


#1

Hello, can be used in Cayenne, ESP8266 for multiple wireless networks without reprogramming the SSID and password? Such as at home or at work?


#2

That’s a good question… I’ll see if I can come up with a way to handle that. Should be possible.


#3

Very good idea :slight_smile:


#4

This is how I have my sketches setup so that I can use one config for multiple WiFi networks. Its probably not the best way to do it, but it works well for me. This should work for Cayenne by just changing WiFi.begin to Cayenne.begin and adding the token. :+1:

#include <ESP8266WiFi.h>

char* ssid[] = {"SSID_0", "SSID_1"};    //Array containing any wifi networks the device can use
char* pass[] = {"PASS_0", "PASS_1"};    //Array containing associated wifi passwords
int i = 0;
int w = 0;
int wifi_timeout = 10;    //Timeout for each connection attempt (seconds)

#define SSID_COUNT (sizeof(ssid)/sizeof(ssid[0]))  //Determines the number of SSIDs in ssid[] array

void setup() {
  Serial.begin(115200);
  //Attempt connection with the first SSID in the array (index 0)
  WiFi.begin(ssid[w], pass[w]);
  Serial.print(ssid[w]);
  
  while (WiFi.status() != WL_CONNECTED) {
    //If timeout is reached, increment w counter, try the next wifi network
    if (i == wifi_timeout){
      Serial.println("");
      w++;
      //Make sure we havent reached the end of the list of SSIDs
      if (w < SSID_COUNT){ 
        Serial.println("Switching to alternate SSID...");
        WiFi.begin(ssid[w], pass[w]);
        Serial.print(ssid[w]);
        delay(1000);
      } else {
        //If we have tried all the defined SSIDs, reboot and try again, or change to break; and continue on with program
        Serial.println("No WiFi connection available, rebooting...");
        ESP.restart();
      }
      i = 0;
    } else {
      Serial.print(".");
      delay(1000);
    }
    i++;
  }

  if (WiFi.status() == WL_CONNECTED){
    Serial.println("");
    Serial.print("Connected to: ");
    Serial.print(WiFi.SSID());
    Serial.print(", Signal: ");
    Serial.println(WiFi.RSSI());
  }
}