E8-2ESP8266Beginner

ESP8266 Wi-Fi Network Scanner: List Nearby Networks by Signal Strength with a NodeMCU

Turn a NodeMCU into a Wi-Fi scanner. The board finds the networks around it, sorts them by signal strength and lists them in the Serial Monitor: a bar chart, percent, dBm, channel, network name and whether it is secured or open. The blue onboard LED blinks fast or slow to match the strongest network, and the scan repeats every 10 seconds. No extra parts and no Wi-Fi password needed. The ESP8266 only sees 2.4 GHz networks.

0.0(0)
ESP8266 Wi-Fi Network Scanner: List Nearby Networks by Signal Strength with a NodeMCU
The easiest way to use the Wi-Fi of an ESP8266 without joining any network: scan the surroundings and read the result. You learn to put the board into station mode, scan for networks, turn signal strength from dBm into a percentage, sort a list from largest to smallest, print aligned rows and a bar chart to the Serial Monitor, tell whether a network is secured, and tie the blink speed of an LED to the signal. It also helps you find the best spot for your router.

Video

Circuit Diagram

ESP8266 Wi-Fi Network Scanner: List Nearby Networks by Signal Strength with a NodeMCU - Circuit Diagram

Source Code

C++ (Arduino IDE)
1// ─────────────────────────────────────────
2//   BlueGrays · bluegrays.com
3//   ESP8266 Wi-Fi Network Scanner: List Nearby Networks by Signal Strength with a NodeMCU
4// ─────────────────────────────────────────
5// The NodeMCU scans the Wi-Fi networks around it, sorts them by signal
6// strength and lists them in the Serial Monitor (115200): bar chart,
7// percent, dBm, channel, name, secured or open. The onboard LED blinks
8// fast or slow to match the strongest network. The scan repeats every 10
9// seconds. No extra parts are needed. The ESP8266 only sees 2.4 GHz
10// networks.
11//
12// Pins:
13//   D4 (GPIO2) -> Onboard blue LED
14
15#include <ESP8266WiFi.h>
16
17// The onboard LED is inverted: writing LOW turns it on, HIGH turns it off.
18const uint8_t PIN_LED = LED_BUILTIN;         // GPIO2, labelled D4 on board
19const uint8_t LED_ON = LOW;
20const uint8_t LED_OFF = HIGH;
21
22// ============ SETTINGS ============
23const unsigned long SCAN_PERIOD_MS = 10000;  // Time between two scans (ms)
24const int MAX_NETWORKS = 50;                 // Most networks to list
25const int BAR_LENGTH = 10;                   // Length of the bar chart
26const unsigned long BLINK_FAST_MS = 100;     // LED interval at 100%
27const unsigned long BLINK_SLOW_MS = 1000;    // LED interval at 0%
28
29int order[MAX_NETWORKS];                     // Network numbers, sorted
30
31// Turns a dBm value into a signal percentage from 0 to 100: -100 dBm or less is 0, -50 dBm or more
32// is 100.
33int signalPercent(int rssi) {
34  return constrain(2 * (rssi + 100), 0, 100);
35}
36
37// Prints the percentage as a bar chart: hash signs for the filled part, dots for the rest.
38void printBar(int percent) {
39  int filled = percent * BAR_LENGTH / 100;
40  for (int i = 0; i < BAR_LENGTH; i++) {
41    Serial.print(i < filled ? '#' : '.');
42  }
43}
44
45// Arranges the network numbers by signal strength: the strongest network goes to the front of the
46// order array.
47void sortBySignal(int count) {
48  for (int i = 0; i < count; i++) {
49    order[i] = i;
50  }
51  for (int i = 1; i < count; i++) {
52    int current = order[i];
53    int j = i - 1;
54    while (j >= 0 && WiFi.RSSI(order[j]) < WiFi.RSSI(current)) {
55      order[j + 1] = order[j];               // Move the weaker one down
56      j--;
57    }
58    order[j + 1] = current;
59  }
60}
61
62// Prints one network on a single line: rank, bar, percent, dBm, channel, name and security.
63void printNetwork(int rank, int index) {
64  int rssi = WiFi.RSSI(index);
65  int channel = WiFi.channel(index);
66  bool isOpen = WiFi.encryptionType(index) == ENC_TYPE_NONE;
67  String name = WiFi.SSID(index);
68  if (name.length() == 0) {
69    name = "hidden network";      // A network hiding its name
70  }
71
72  Serial.printf("%2d  ", rank);
73  printBar(signalPercent(rssi));
74  Serial.printf("  %3d%%  %4d dBm  CH %2d  ", signalPercent(rssi), rssi, channel);
75  Serial.print(name);
76  Serial.print("  [");
77  Serial.print(isOpen ? "open" : "secured");
78  Serial.println("]");
79}
80
81// Waits until the next scan. Unless blinkMs is 0, the LED blinks at that interval.
82void waitForNextScan(unsigned long scanStart, unsigned long blinkMs) {
83  bool ledOn = false;
84  while (millis() - scanStart < SCAN_PERIOD_MS) {
85    unsigned long left = SCAN_PERIOD_MS - (millis() - scanStart);
86    if (blinkMs == 0) {
87      delay(left);                           // No network: LED stays off
88    } else {
89      delay(min(blinkMs, left));
90      ledOn = !ledOn;
91      digitalWrite(PIN_LED, ledOn ? LED_ON : LED_OFF);
92    }
93  }
94  digitalWrite(PIN_LED, LED_OFF);
95}
96
97void setup() {
98  Serial.begin(115200);
99  pinMode(PIN_LED, OUTPUT);
100  digitalWrite(PIN_LED, LED_OFF);            // Start with the LED off
101  WiFi.mode(WIFI_STA);                       // Station mode only
102  WiFi.disconnect();                         // Do not join any network
103  delay(100);
104  Serial.println();
105  Serial.println("ESP8266 Wi-Fi scanner is ready");
106}
107
108void loop() {
109  unsigned long scanStart = millis();
110  unsigned long blinkMs = 0;
111
112  Serial.println();
113  Serial.println("Scanning...");
114  digitalWrite(PIN_LED, LED_ON);             // LED is on during the scan
115  int count = WiFi.scanNetworks(false, true);     // true: hidden ones too
116  digitalWrite(PIN_LED, LED_OFF);
117
118  if (count <= 0) {
119    Serial.println("No networks found");
120  } else {
121    if (count > MAX_NETWORKS) {
122      count = MAX_NETWORKS;
123    }
124    sortBySignal(count);
125    Serial.print("Networks found: ");
126    Serial.println(count);
127    for (int i = 0; i < count; i++) {
128      printNetwork(i + 1, order[i]);
129    }
130
131    // The strongest network is at the top of the list. The stronger it is, the faster the LED
132    // blinks.
133    int best = signalPercent(WiFi.RSSI(order[0]));
134    blinkMs = BLINK_SLOW_MS - best * (BLINK_SLOW_MS - BLINK_FAST_MS) / 100;
135    Serial.print("LED interval (ms): ");
136    Serial.println(blinkMs);
137  }
138  WiFi.scanDelete();                         // Free the scan results
139
140  waitForNextScan(scanStart, blinkMs);
141}
142

FAQ

Why can't the ESP8266 see 5 GHz networks?

The radio of the ESP8266 works on the 2.4 GHz band only; it has no hardware for 5 GHz and no code can switch it on. If your router sends both bands under one name, the list shows only the 2.4 GHz one. A network that is 5 GHz only never appears. The common ESP32 models are the same. For later projects that connect the board to a network, the 2.4 GHz band of the router has to be on.

What is dBm, and which value counts as good?

dBm is the unit of signal strength, and for Wi-Fi it is always a negative number; the closer to zero, the stronger the signal. -50 dBm and above is excellent, -50 to -70 is good, -70 to -80 is weak but usable, below -80 is trouble. The code turns the value into a percentage: -100 dBm counts as 0 percent, -50 dBm as 100 percent. The value moves by a few dBm from scan to scan, which is normal.

Do I need a Wi-Fi password to scan?

No. Routers announce their name, channel and type of encryption openly; the Wi-Fi list on your phone shows the same information. The board asks who is on the air and listens to the answers; it joins no network and tries no passwords. You do not have to put a network name or password into the code. Joining a network is a separate job and needs the password of that network.

What is a hidden network, and why does it have no name in the list?

Some routers are set up not to announce their name. Such a network still shows up in a scan because its signal is in the air; only the name field comes back empty. When the code sees an empty name it prints hidden network instead. Signal strength, channel and security are listed like for any other network. If you do not want hidden networks in the list, change the second value in WiFi.scanNetworks to false.