WiFi with Arduino – Connect to a Network

ArduinoHardwareSoftware & Coding

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 – Connect with WPA.You can find the example here.

Most networks you come across will have WPA2 encryption. If you create a hotspot field using your mobile phone, you will generally opt for WPA2 encryption and set a password.

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>

Next, we define some global variables, including SSID, password of the network you intend to connect your Arduino to and a status int.

char ssid[] = "yourNetwork";     // your network SSID (name)
char pass[] = "secretPassword";  // your network password
int status = WL_IDLE_STATUS;    // the Wifi radio's status

Within the setup, we do the following −

  • Initialize Serial

  • Check for the presence of the WiFi shield

  • Check if the fw_version of the shield is correct or needs to be upgraded

  • Attempt to connect to the network using WiFi.begin()

  • Once connected, print details of the network (SSID, BSSID (MAC address of router),Signal strength or RSSI, and encryption type), using printCurrentNet()

  • Also, print the WiFi details (local IP and MAC address) using printWifiData()

Within the loop, we just keep network details every 10 seconds using printCurrentNet()

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");
   }

   // attempt to connect to Wifi network:
   while (status != WL_CONNECTED) {
      Serial.print("Attempting to connect to WPA SSID: ");
      Serial.println(ssid);
      // Connect to WPA/WPA2 network:
      status = WiFi.begin(ssid, pass);
      // wait 10 seconds for connection:
      delay(10000);
   }

   // you're connected now, so print out the data:
   Serial.print("You're connected to the network");
   printCurrentNet();
   printWifiData();
}

void loop() {
   // check the network connection once every 10 seconds:
   delay(10000);
   printCurrentNet();
}

void printWifiData() {
   // print your WiFi shield's IP address:
   IPAddress ip = WiFi.localIP();
   Serial.print("IP Address: ");
   Serial.println(ip);
   Serial.println(ip);
   // print your MAC address:
   byte mac[6];
   WiFi.macAddress(mac);
   Serial.print("MAC address: ");
   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);
}

void printCurrentNet() {
   // print the SSID of the network you're attached to:
   Serial.print("SSID: ");
   Serial.println(WiFi.SSID());
   // print the MAC address of the router you're attached to:
   byte bssid[6];
   WiFi.BSSID(bssid);
   Serial.print("BSSID: ");
   Serial.print(bssid[5], HEX);
   Serial.print(":");
   Serial.print(bssid[4], HEX);
   Serial.print(":");
   Serial.print(bssid[3], HEX);
   Serial.print(":");
   Serial.print(bssid[2], HEX);
   Serial.print(":");
   Serial.print(bssid[1], HEX);
   Serial.print(":");
   Serial.println(bssid[0], HEX);
   // print the received signal strength:
   long rssi = WiFi.RSSI();
   Serial.print("signal strength (RSSI):");
   Serial.println(rssi);

   // print the encryption type:
   byte encryption = WiFi.encryptionType();
   Serial.print("Encryption Type:");
   Serial.println(encryption, HEX);
   Serial.println();
}

Note that the MAC address of the WiFi and the router (WiFi.macAddress() and WiFi.BSSID()) is stored in 6-byte array, and each byte is printed in the hex format one by one. The difference between the two is that WiFi.macAddress() gives you the MAC address of your device (Arduino with WiFi shield), whereas WiFi.BSSID() gives you the MAC address of the router or the access point to which your device is connected.

raja
Updated on 30-Jul-2021 15:00:23

Advertisements