Scanning of WiFi on ESP32 controller

Wi-Fi is a wireless radio local area network technology based on IEEE 802.11 standards and it is used for connecting to the network. The WiFi works at 2.4GHz or 5GHz frequencies. Basically, Wi-Fi is a radio wave that broadcast from a Wi-Fi router. The devices such as desktop and laptop, video game consoles, smartphones, tablets, and smart TVs are using WiFi technology. The WiFi works longer than Bluetooth or infrared and it is low power technology. The coverage of one or more interconnected access points(hotspots) can extend from an area as small as a few rooms to as large as many square kilometers. Wi-Fi is a trademark of Wi-Fi Alliance, an association of manufacturers and regulators defining standards and certifying products as Wi-Fi compatible. In this tutorial we demonstrate how to works ” scanning of Wi-Fi on ESP32 controller”.

Scanning of WiFi on ESP32 controller

Different modes of WiFi network

The wifi libraries provide support for configuring and monitoring the ESP32 wifi networking functionality. This include configuration for:

  • Station mode (aka STA mode or WiFi client mode). ESP32 connects to an access point.
  • AP mode (aka Soft-AP mode or Access Point mode). Stations connect to the ESP32.
  • Combined AP-STA mode (ESP32 is concurrently an access point and a station connected to another access point).
  • Various security modes for the above (WPA, WPA2, WEP, etc.)
  • Scanning for access points (active & passive scanning).
  • Promiscuous mode monitoring of IEEE 802.11 WiFi packets.

Scanning of WiFi on ESP32 controller1

This example shows how to use a scan of ESP32.
With the ESP32, the library to use will be:

#include "WiFi.h"

Setting the wifi to station mode and disconnect from an AP(Access Point mode) if it was previously connected.

 WiFi.mode(WIFI_STA);
 WiFi.disconnect();

After setup, Scanning of wifi starts. An SSID (service set identifier) is the primary name associated with an 802.11 wireless local area network (WLAN) including home networks and public hotspots. RSSI is the relative received signal strength in a wireless environment.

void loop()
{
    Serial.println("scan start");

    // WiFi.scanNetworks will return the number of networks found
    int n = WiFi.scanNetworks();
    Serial.println("scan done");
    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 RSSI for each network found
            Serial.print(i + 1);
            Serial.print(": ");
            Serial.print(WiFi.SSID(i));
            Serial.print(" (");
            Serial.print(WiFi.RSSI(i));
            Serial.print(")");
            Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
            delay(10);
        }
    }

WiFi.encryptionType(i) – The encryption type of the networks discovered during the scanNetworks.The parameter i specify from which network item want to get the information. Then return the encryption type of the specified item on the network scanned list.
WiFi.SSID(i)- The SSID discovered during the network scan. The parameter i specifies from which network item want to get the information. Then SSID string of the specified item on the networks scanned list.
WiFi.RSSI(i)-The RSSI discovered during the network scan. Then returning the signed value of RSSI of the specified item on the network scanned list.