Scanning Networks with MKR WiFi 1010

Learn how to setup your board to scan nearby Wi-Fi networks, and print them out in the Serial Monitor.

Introduction

One great feature with the MKR WiFi 1010 is the possibility to scan nearby Wi-Fi networks. This is done using the NINA-W102 radio module, and in this tutorial, we will go through some simple steps to get this working!

Goals

The goals of this project are:

  • Scan surrounding networks.
  • Print available networks in the Serial Monitor.
  • Print additional information about these networks.

Hardware & Software Needed

Scanning Networks

Scanning for Wi-Fi networks is a quite straightforward process. A device with a Wi-Fi module such as smart-phones, computers or development boards such as the MKR WiFi 1010, search their surroundings, and get a response from nearby networks.

The device looking for a network or the client, sends a probe request, while nearby networks send probe responses. These responses contains information such as name of the network, signal strength in dBm (decibel milli-watts) and encryption type.

Circuit

This tutorial requires no additional circuit.

There is no need for extra components in the circuit but the board.
There is no need for extra components in the circuit but the board.

Step by Step

1. First we need to make sure we have the dependencies installed. For this tutorial, we will need to install the WiFiNINA library. If we are using the offline editor, we can locate this library in Tools > Manage Libraries.... If we are using the Web Editor, it is already installed.

2. If we are using the offline editor, we need to make sure we have the drivers installed for the MKR WiFi 1010 board. In the editor, navigate to Tools > Board > Board Manager..., and install the Arduino SAMD boards (32-bits Arm® Cortex®-M0+) core.

3. Now let's take a look at some of the core functionalities of the sketch that we will use.

  • WiFi.macAddress()
    - obtains the boards MAC address.
  • WiFi.scanNetworks()
    - scans for nearby networks.
  • WiFi.encryptionType()
    - retrieves encryption type of found network.
  • WiFi.SSID()
    - retrieves SSID (network name) of found network.
  • WiFi.RSSI()
    - retrieves RSSI (signal strength) of found network (measured in dBm).

We can now upload the code that can be found in the snippet below to our MKR WiFi 1010 board.

1#include <SPI.h>
2#include <WiFiNINA.h>
3
4void setup() {
5 //Initialize serial and wait for port to open:
6 Serial.begin(9600);
7 while (!Serial) {
8 ; // wait for serial port to connect. Needed for native USB port only
9 }
10
11 // check for the WiFi module:
12 if (WiFi.status() == WL_NO_MODULE) {
13 Serial.println("Communication with WiFi module failed!");
14 // don't continue
15 while (true);
16 }
17
18 String fv = WiFi.firmwareVersion();
19 if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
20 Serial.println("Please upgrade the firmware");
21 }
22
23 // print your MAC address:
24 byte mac[6];
25 WiFi.macAddress(mac);
26 Serial.print("MAC: ");
27 printMacAddress(mac);
28}
29
30void loop() {
31 // scan for existing networks:
32 Serial.println("Scanning available networks...");
33 listNetworks();
34 delay(10000);
35}
36
37void listNetworks() {
38 // scan for nearby networks:
39 Serial.println("** Scan Networks **");
40 int numSsid = WiFi.scanNetworks();
41 if (numSsid == -1) {
42 Serial.println("Couldn't get a wifi connection");
43 while (true);
44 }
45
46 // print the list of networks seen:
47 Serial.print("number of available networks:");
48 Serial.println(numSsid);
49
50 // print the network number and name for each network found:
51 for (int thisNet = 0; thisNet < numSsid; thisNet++) {
52 Serial.print(thisNet);
53 Serial.print(") ");
54 Serial.print(WiFi.SSID(thisNet));
55 Serial.print("\tSignal: ");
56 Serial.print(WiFi.RSSI(thisNet));
57 Serial.print(" dBm");
58 Serial.print("\tEncryption: ");
59 printEncryptionType(WiFi.encryptionType(thisNet));
60 }
61}
62
63void printEncryptionType(int thisType) {
64 // read the encryption type and print out the title:
65 switch (thisType) {
66 case ENC_TYPE_WEP:
67 Serial.println("WEP");
68 break;
69 case ENC_TYPE_TKIP:
70 Serial.println("WPA");
71 break;
72 case ENC_TYPE_CCMP:
73 Serial.println("WPA2");
74 break;
75 case ENC_TYPE_NONE:
76 Serial.println("None");
77 break;
78 case ENC_TYPE_AUTO:
79 Serial.println("Auto");
80 break;
81 case ENC_TYPE_UNKNOWN:
82 default:
83 Serial.println("Unknown");
84 break;
85 }
86}
87
88void printMacAddress(byte mac[]) {
89 for (int i = 5; i >= 0; i--) {
90 if (mac[i] < 16) {
91 Serial.print("0");
92 }
93 Serial.print(mac[i], HEX);
94 if (i > 0) {
95 Serial.print(":");
96 }
97 }
98 Serial.println();
99}

Testing It Out

After we have uploaded the code to the board, we will need to open the Serial Monitor. When we open it, we will first see the MAC address of our board printed, followed by the text

"Scanning available networks"
. After 10 seconds of scanning, the available networks will be listed, along with information about the network.

List of surrounding networks.
List of surrounding networks.

If we look closer at #1 in the list, we can see that the signal is -38 dBm. When this test was done, the board was only a meter away from the router, so the signal strength is great.

But if we look at the other networks, we can see that the signal goes all the way down to -94 dBm, which is very weak. If we tried to connect a computer to this network, it would most likely fail.

Signal strength of routers.
Signal strength of routers.

Troubleshoot

If the code is not working, there are some common issues we can troubleshoot:

  • We have not installed the WiFiNINA library.
  • We have not installed the drivers for the MKR WiFi 1010.
  • We have not selected the right port.

Conclusion

In this tutorial we have created a simple Wi-Fi scanner, that has printed all available networks within range of our MKR WiFi 1010 board. This feature exists virtually in any device that can connect to the Internet, and can be quite a good tool to experiment with, for example how far our devices can be from a gateway (router) to work.

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.