ESP8266: Scanning WiFi Networks

The objective of this post is to explain how to scan the surrounding WiFi networks with the ESP8266.


The code

The code for this tutorial will be very simple and since we only want to scan and get some information about the surrounding WiFi networks, we will do all the coding on the setup function. So, we will leave the main loop empty.

First of all, we include the ESP8266WiFi library, which will make available the functionality needed for the ESP8266 to interact with the WiFi networks. We can remember from previous posts that this is also the library needed for us to connect to a WiFi network.

#include "ESP8266WiFi.h"

In the setup function, we start by  opening a serial connection, so we can send the data to the Arduino IDE serial console.

Next, to scan the networks, we call the scanNetworks method on the WiFi object of the library we included. This method will return the number of networks discovered [1].

Note that this method can be called with two additional arguments, one that indicates the scanning should be done asynchronously, and another to show networks with hidden SSID [1]. More details about these functionalities can be found here.

int numberOfNetworks = WiFi.scanNetworks();

Now, to get each network SSID, we just need to call the SSID method, which receives as argument the index of one of the previously discovered networks. In the example bellow, we are getting this parameter for the first network of the list. Naturally, you should check first if any network was discovered

Serial.println(WiFi.SSID(0));

We can also get the RSSI (Received Signal Strength Indicator) of each network by calling the RSSI method on the WiFi object, again passing as input the index of the network.

Serial.println(WiFi.RSSI(0));

The complete code is shown bellow, which includes the iteration over the discovered networks and printing of these indicators to the serial port.

#include "ESP8266WiFi.h"

void setup() {

  Serial.begin(115200);

  int numberOfNetworks = WiFi.scanNetworks();

  for(int i =0; i<numberOfNetworks; i++){

      Serial.print("Network name: ");
      Serial.println(WiFi.SSID(i));
      Serial.print("Signal strength: ");
      Serial.println(WiFi.RSSI(i));
      Serial.println("-----------------------");

  }

}

void loop() {}

To test the code just upload it to the ESP8266 and open the serial console. You should get something similar to figure 1.

esp8266-network-scan

Figure 1 – Output of the program.

Note that there are more functionalities associated with the scanning of surrounding WiFi networks, which can be seen here.


Related Posts

ESP8266: Connecting to a WiFi Network


References

[1]https://github.com/esp8266/Arduino/blob/4897e0006b5b0123a2fa31f67b14a3fff65ce561/doc/esp8266wifi/scan-class.md


Technical details

  • ESP8266 libraries: v2.3.0

5 thoughts on “ESP8266: Scanning WiFi Networks”

    1. Hi! Did you select the correct baud rate on the serial monitor? It should be the same baud rate you used on the Serial.Begin call.

      You also need to confirm that the serial port you have selected is the same of the ESP. To do so, you can check how many serial ports are available with the ESP connected, then connect it and check the new one that should appear. That should be the one of your device.

      To help debugging, you can also do a much simpler program that just prints stuff to the serial port, to confirm that that way you can receive the data.

      Best regards,
      Nuno Santos

    1. Hi! Did you select the correct baud rate on the serial monitor? It should be the same baud rate you used on the Serial.Begin call.
      You also need to confirm that the serial port you have selected is the same of the ESP. To do so, you can check how many serial ports are available with the ESP connected, then connect it and check the new one that should appear. That should be the one of your device.
      To help debugging, you can also do a much simpler program that just prints stuff to the serial port, to confirm that that way you can receive the data.
      Best regards,
      Nuno Santos

  1. Pingback: ESP32 Espruino: Scanning WiFi Networks | techtutorialsx

  2. Pingback: ESP32 Espruino: Scanning WiFi Networks | techtutorialsx

  3. Hi i am getting a problem with the WiFi.scanNetworks(). when i scan the networks i am getting random number of networks like 16 or 10 or so. While the actual networks are 27 or more. when i first scan i am getting randomly picked networks like 10 or 15. in the second scan other random picked networks are coming and the count of networks is also random like 10 or 20. So can you please help me with this problem.

    1. Hi!

      I never ran into that issue, so unfortunately I cannot be of much assistance.

      This is just a guess, but it may actually be related to the high number of networks present, maybe some problem with noise?

      I’m not sure if there is any limitation of the maximum number of networks the ESP can scan and show, my suggestion is that you ask around the GitHub page of the Arduino core. There someone may already faced the same issue and have some idea of what is happening.

      Let us know if you find a solution.

      Best regards,
      Nuno Santos

  4. Hi i am getting a problem with the WiFi.scanNetworks(). when i scan the networks i am getting random number of networks like 16 or 10 or so. While the actual networks are 27 or more. when i first scan i am getting randomly picked networks like 10 or 15. in the second scan other random picked networks are coming and the count of networks is also random like 10 or 20. So can you please help me with this problem.

    1. Hi!
      I never ran into that issue, so unfortunately I cannot be of much assistance.
      This is just a guess, but it may actually be related to the high number of networks present, maybe some problem with noise?
      I’m not sure if there is any limitation of the maximum number of networks the ESP can scan and show, my suggestion is that you ask around the GitHub page of the Arduino core. There someone may already faced the same issue and have some idea of what is happening.
      Let us know if you find a solution.
      Best regards,
      Nuno Santos

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading