WiFi with Arduino – Scan Networks


In order to use WiFi with Arduino Uno, or any other board, you may need to get a WiFi shield(unless you are using a board with built-in WiFi capabilities, like Arduino Uno WiFi). The WiFi shield, like any other shield, stacks up on your board, and provides access to the pins of Arduino on the shield itself.

You can read more about the WiFi shield here −

Assuming you have a WiFi shield with you, you will need the WiFi library to get started. You don’t need to download it separately; it will be built-in in your IDE. If you don’t get a compilation error at

#include <WiFi.h>

then your IDE contains the library.

In this article, we will walk through a built-in example of the WiFi library – Scan Networks. The code can be found here.

We begin with the inclusion of the SPI and WiFi libraries (SPI because the shield uses SPI for communication).

#include <SPI.h>
#include <WiFi.h>

Within the setup, we check for the presence of the shield, check its firmware version, and print its MAC address,

void setup() {
   //Initialize serial and wait for port to open:
   Serial.begin(9600);
   while (!Serial) {
      ; // wait for serial port to connect. Needed for native USB port only
   }

   // check for the presence of the shield:
   if (WiFi.status() == WL_NO_SHIELD) {
      Serial.println("WiFi shield not present");
      // don't continue:
      while (true);
   }

   String fv = WiFi.firmwareVersion();
   if (fv != "1.1.0") {
      Serial.println("Please upgrade the firmware");
   }

   // Print WiFi MAC address:
   printMacAddress();
}

The printMacAddress() function will be covered later.

Within the loop, we just list all networks every 10 seconds.

void loop() {
   // scan for existing networks:
   Serial.println("Scanning available networks...");
   listNetworks();
   delay(10000);
}

Now, the printMacAddress function simply copies the macAddress using the WiFi.macAddress() function into a 6-byte array, and then prints out the values byte by bye.

void printMacAddress() {
   // the MAC address of your Wifi shield
   byte mac[6];

   // print your MAC address:
   WiFi.macAddress(mac);
   Serial.print("MAC: ");
   Serial.print(mac[5], HEX);
   Serial.print(":");
   Serial.print(mac[4], HEX);
   Serial.print(":");
   Serial.print(mac[3], HEX);
   Serial.print(":");
   Serial.print(mac[2], HEX);
   Serial.print(":");
   Serial.print(mac[1], HEX);
   Serial.print(":");
   Serial.println(mac[0], HEX);
}

The listNetworks() function makes use of the WiFi.scanNetworks() function. If this function found no SSID, it returns -1, otherwise it returns the number of SSIDs it found. The values of the SSIDs, their signal strength indicator, RSSI and their encryption types can be obtained using WiFi.SSID(), WiFi.RSSI() and WiFi.encryptionType() respectively, for each of the networks.

void listNetworks() {
   // scan for nearby networks:
   Serial.println("** Scan Networks **");
   int numSsid = WiFi.scanNetworks();
   if (numSsid == -1) {
      Serial.println("Couldn't get a wifi connection");
      while (true);
   }

   // print the list of networks seen:
   Serial.print("number of available networks:");
   Serial.println(numSsid);

   // print the network number and name for each network found:
   for (int thisNet = 0; thisNet < numSsid; thisNet++) {
      Serial.print(thisNet);
      Serial.print(") ");
      Serial.print(WiFi.SSID(thisNet));
      Serial.print("\tSignal: ");
      Serial.print(WiFi.RSSI(thisNet));
      Serial.print(" dBm");
      Serial.print("\tEncryption: ");
      printEncryptionType(WiFi.encryptionType(thisNet));
   }
}

The printEncryptionType function is essentially a switch case, which prints the text of the encryption type, depending on the numerical value passed.

void printEncryptionType(int thisType) {
   // read the encryption type and print out the name:
   switch (thisType) {
      case ENC_TYPE_WEP:
         Serial.println("WEP");
         break;
      case ENC_TYPE_TKIP:
         Serial.println("WPA");
         break;
      case ENC_TYPE_CCMP:
         Serial.println("WPA2");
         break;
      case ENC_TYPE_NONE:
         Serial.println("None");
         break;
      case ENC_TYPE_AUTO:
         Serial.println("Auto");
         break;
   }
}

For more information on the WiFi library, see https://www.arduino.cc/en/Reference/WiFi

Updated on: 30-Jul-2021

538 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements