Scanning for Wi-Fi Networks with ESP32

One of the most powerful features of the ESP32 is its ability to scan for and assess the quality of nearby Wi-Fi networks. This is crucial for a wide range of applications. For example, the ESP32 can use this information to automatically connect to the strongest available network, ensuring a reliable connection for your project. Or, by analyzing the signal strengths of different networks, the ESP32 can estimate its location within a building or area, a technique that’s commonly used in indoor positioning systems.

In this tutorial, we’ll show you how to program the ESP32 to list all Wi-Fi networks within its range, along with essential details about each network like its name (SSID), signal strength (RSSI), security type, and channel.

Step 1 – Set Up the Arduino IDE

We will be using the Arduino IDE to program the ESP32, so please ensure you have the ESP32 add-on installed before you proceed:

Step 2 – Connect the ESP32 to your Computer

Now connect your ESP32 board to your computer using a USB cable.

esp32 connected to computer using a usb cable

Step 3 – Selecting the Board and Port

Open the Arduino IDE and click on “Select other board and port…” on the top drop-down menu.

arduino ide 2 select board and port menu

A new window will pop up. Search for the specific type of ESP32 board you are using (in our case, it’s the DOIT ESP32 DEVKIT V1). If you’re not sure which board you have, choosing ESP32 Dev Module is often a safe bet.

Next, select the port that corresponds to your ESP32 board. It’s usually labeled something like “/dev/ttyUSB0” (on Linux or macOS) or “COM6” (on Windows).

arduino ide 2 select esp32 board and port

Step 4 – Upload the Code

You’re now ready to upload the code to the ESP32.

#include <WiFi.h>

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

  // Initialize WiFi and start scan
  WiFi.mode(WIFI_STA);  // Set the Wi-Fi mode to Station
  WiFi.disconnect();    // Disconnect from any previous connections
  delay(100);

  Serial.println("Scanning for Wi-Fi networks...");
  int n = WiFi.scanNetworks();  // Start network scan

  if (n == 0) {
    Serial.println("No networks found.");
  } else {
    Serial.print(n);
    Serial.println(" networks found:");
    for (int i = 0; i < n; ++i) {
      // Print SSID and signal strength for each network
      Serial.print(i + 1);
      Serial.print(": ");
      Serial.print(WiFi.SSID(i));
      Serial.print(" (");
      Serial.print(WiFi.RSSI(i));
      Serial.print(" dBm)");
      Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " [Open]" : " [Secured]");
      delay(10);  // Short delay for stable serial output
    }
  }
}

void loop() {
  // Nothing to do in loop
  delay(1000);
}

Step 5 – Check the Serial Monitor

After uploading the sketch, open the Serial Monitor and make sure the baud rate is set to 115200. Then press the EN (reset) button on your ESP32. You should now see a list of all the Wi-Fi networks that your ESP32 can detect, along with details like their signal strength and whether they are open or secured with a password.

esp32 list of all scanned networks
Shreepad Prabhu

Shreepad Prabhu

Shreepad is a passionate Electronics & Telecommunication Engineer with a deep love for embedded systems. He has over 15 years of experience, including his time as a Senior Embedded Engineer at Micromax contributing to solutions for Thermo Fisher Scientific, Tata Motors, Liebherr, and John Deere. Since co-founding Last Minute Engineers in 2018, he has written hundreds of articles and guides for Last Minute Engineers to help makers build with confidence. You can find him on LinkedIn