One great feature with the Arduino UNO WiFi Rev2 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!
The goals of this project are:
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 Arduino UNO WiFi Rev2, 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.
This tutorial requires no additional circuit.
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.
If we are using the offline editor, we need to make sure we have the drivers installed for the Arduino UNO WiFi Rev2 board. In the editor, navigate to Tools > Board > Board Manager..., and install the Arduino avrMEGA Boards core.
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). 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 only9 }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 continue15 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}
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.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.
If the code is not working, there are some common issues we can troubleshoot:
In this tutorial we have created a simple Wi-Fi scanner, that has printed all available networks within range of our Arduino UNO WiFi Rev2. 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.