Choose a preset WIFI network to use with ESP32

Good morning.
I use several ESP32s and depending on where I work, I use a different network
I have a different network in each of the three locations.
At the end I would like the program to select which one is active.

I found the program to scan the network and I want to select the active one from one of the three

I put the program scanning WIFI network with my three different WIFI network

Many thanks in advance

#include "WiFi.h"

String get_encryption_type(wifi_auth_mode_t encryptionType) {
    switch (encryptionType) {
        case (WIFI_AUTH_OPEN):
            return "Open";
        case (WIFI_AUTH_WEP):
            return "WEP";
        case (WIFI_AUTH_WPA_PSK):
            return "WPA_PSK";
        case (WIFI_AUTH_WPA2_PSK):
            return "WPA2_PSK";
        case (WIFI_AUTH_WPA_WPA2_PSK):
            return "WPA_WPA2_PSK";
        case (WIFI_AUTH_WPA2_ENTERPRISE):
            return "WPA2_ENTERPRISE";
    }
}

// ---- begin of what I added

/*
char ssid[] = "Iphone de ben"; // your network SSID (name)
char pass[] = "XXXX1";  // your network password
*/

char ssid[] = "SFR-d078";       // your network SSID (name)
char pass[] = "DEDW3Z";  // your network 

//char ssid[] = "Bbox-E69F0626";       // your network SSID (name)
//char pass[] = "NKg5Ad";  // your network

// ---- end of what I added

void setup(){
    
   Serial.begin(115200);
    WiFi.mode(WIFI_STA);
}


void loop() {
    Serial.println("uPesy WiFi Scan Demo");
    Serial.println("[*] Scanning WiFi network");

        // WiFi.scanNetworks will return the number of networks found
        int n = WiFi.scanNetworks();
        Serial.println("[*] Scan done");
        if (n == 0) {
            Serial.println("[-] No WiFi networks found");
        } else {
            Serial.println((String)"[+] " + n + " WiFi networks found\n");
            for (int i = 0; i < n; ++i) {
                // Print SSID, RSSI and WiFi Encryption for each network found
                Serial.print(i + 1);
                Serial.print(": ");
                Serial.print(WiFi.SSID(i));
                Serial.print(" (");
                Serial.print(WiFi.RSSI(i));
                Serial.print(" dB) [");
                Serial.print(get_encryption_type(WiFi.encryptionType(i)));
                Serial.println("]");
                delay(10);
            }
        }
        Serial.println("");

        // Wait a bit before scanning again
        delay(5000);
}

I can recommend WiFiManager library for this, it`s also OTA capable.

Hii,
I looked at wifi manager examples, but I don't find a way to select one network among others
It seems to be complicated :confused:
I think there is a way to list all network and if one matching with one of mine then select it.
What is the the function to see a match with []ssid1 , []ssid2, []ssid3..?

See the example

Wifi / WifiMulti

it shows how to predefine several wifi SSIDs and passwords

if the ESP has to reconnect to wifi, it will try each pair one after another and when available the connection will be established.

1 Like

Thank you. It works perfectly :wink:

x

my pleasure.

btw:
you can mark ONE answer as solution.
you can say thank you to the helpers with the heart under their post...

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.