Configuring Wi-Fi

Compatible Wi-Fi Dongles

The only Pi to have integrated Wi-Fi is the Raspberry Pi 3 Model B. All other versions require a Wi-Fi USB dongle. I have successfully used the following two Wi-Fi dongles with the Raspberry Pi 2 Model B without the need to install extra drivers:

Ralink RT8188
Ralink RT8188
Edimax EW-7811Un
Edimax EW-7811Un

Connecting a USB WiFi adapter (if required)

You can check the adapter is correctly recognised by using the command lsusb to list all connected USB devices. Below you can see the response before and after connecting the Edimax WiFi adapter ro a Raspberry Pi 2 Model B. If using a Raspberry Pi 3 then its integrated Wi-Fi adapter is not shown by this command.

pi@raspberrypi:~ $ lsusb
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
pi@raspberrypi:~ $
pi@raspberrypi:~ $
pi@raspberrypi:~ $ lsusb
Bus 001 Device 005: ID 7392:7811 Edimax Technology Co., Ltd EW-7811Un 802.11n Wireless Adapter [Realtek RTL8188CUS]
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
pi@raspberrypi:~ $

Finding the name of the Wi-Fi adapter

In all likelihood your Wi-Fi adapter will be known as ‘wlan0’, if you plug a second Wi-Fi adapter in then that will be called ‘wlan1’, a third would be called ‘wlan2’, etc. However, you should check that the name assigned to your Wi-Fi adapter before continuing. Use the iwconfig command to view the Wi-Fi adapter settings:

iwconfig

In the example below you can see that the Wi-Fi adapter has been called ‘wlan0’:

pi@raspberrypi:~ $ iwconfig
wlan0     unassociated  Nickname:”
          Mode:Managed  Frequency=2.412 GHz  Access Point: Not-Associated
          Sensitivity:0/0
          Retry:off   RTS thr:off   Fragment thr:off
          Power Management:off
          Link Quality:0  Signal level:0  Noise level:0
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:0   Missed beacon:0

lo        no wireless extensions.

eth0      no wireless extensions.

pi@raspberrypi:~ $

Scan for networks

To scan for available networks use the iwlist command:

sudo iwlist wlan0 scan

You may see many more networks than are shown below. Here we see one network called ‘HomeWiFi’ with PSK encryption:

pi@raspberrypi:~ $ sudo iwlist wlan0 scan
          Cell 01 – Address: 02:1D:AA:81:5F:80
                    Channel:1
                    Frequency:2.412 GHz (Channel 1)
                    Quality=60/70 Signal level=-50 dBm
                    Encryption key:on
                    ESSID:”HomeWiFi”
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 9 Mb/s
                              18 Mb/s; 36 Mb/s; 54 Mb/s
                    Bit Rates:6 Mb/s; 12 Mb/s; 24 Mb/s; 48 Mb/s
                    Mode:Master
                    Extra:tsf=0000000000000000
                    Extra: Last beacon: 90ms ago
                    IE: Unknown: 0008486F6D6557694669
                    IE: Unknown: 010882848B961224486C
                    IE: Unknown: 030101
                    IE: Unknown: 32040C183060
                    IE: Unknown: 0706474220010D14
                    IE: Unknown: 33082001020304050607
                    IE: Unknown: 33082105060708090A0B
                    IE: Unknown: 050400010000
                    IE: Unknown: 2A0104
                    IE: Unknown: 2D1AEE1117FFFF000001000000000000000000000000000000000000
                    IE: Unknown: 3D1601050600000000000000000000000000000000000000
                    IE: Unknown: 4A0E14000A002C01C800140005001900
                    IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : CCMP
                        Pairwise Ciphers (1) : CCMP
                        Authentication Suites (1) : PSK
                    IE: Unknown: DD180050F2020101000003A4000027A4000042435E0062322F00
                    IE: Unknown: 0B0506003B127A
                    IE: Unknown: DD07000C4303000000
pi@raspberrypi:~ $

Connect to a Wi-Fi hotspot at start-up

In this example, the SSID of my WiFi network is ‘HomeWiFi’ and the password is ‘WiFiPassword’. To have the Raspberry Pi connect when the network is in range you need to add the following lines to the file /etc/wpa_supplicant/wpa_supplicant.conf

network={
ssid=”HomeWiFi”
psk=”WiFiPassword”
}

To edit the file I recommend the the text editor nano. [Ctrl-O to save, Ctrl-X to exit]

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

The file will look something like this:

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

Append the required lines like so, then save with ‘Ctrl-O’ and exit with ‘Ctrl-X’.

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
     ssid="HomeWiFi"
     psk="WiFiPassword"
}

The change is normally detected automatically within a few seconds, otherwise you can reboot the Raspberry Pi with:

sudo shutdown -r now

Connect to a private Wi-Fi, i.e. one that isn’t broadcasting a SSID

Simply add the line scan_ssid=1 inside the network={…} within the wpa_supplicant.conf like so:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
     ssid="HomeWiFi"
     psk="WiFiPassword"
     scan_ssid=1
}

Setup connections for multiple Wi-Fi networks

You can easily provide the Pi with multiple Wi-Fi access point details so that it will automaticially connect to which ever one happens to be in range. Simply edit the wpa_supplicant.conf config file as described above and add an additional network={}, like so:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
     ssid="HomeWiFi"
     psk="WiFiPassword"
}

network={
     ssid="Work-WiFi-SSID"
     psk="Work-WiFi-Password"
}

network={
     ssid="School-WiFi-SSID"
     psk="School-WiFi-Password"
}

Check the Wi-Fi connection, IP settings and Signal Strength

There are two useful commands which inform you of the status of the Wi-Fi connection, ifconfig and iwconfig.

ifconfig will tell you the adapters MAC address, IP settings and the amount of data sent and received. In this example you can see that the Pi has been allocated the address 192.168.1.11 on interface wlan0 (wireless lan 0):

ifconfig

pi@raspberrypi:~ $ ifconfig
eth0      Link encap:Ethernet  HWaddr b8:27:eb:26:f7:a4
          inet addr:192.168.1.10  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::ac3d:1fad:b08f:638/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:132 errors:0 dropped:0 overruns:0 frame:0
          TX packets:82 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:20457 (19.9 KiB)  TX bytes:13536 (13.2 KiB)

lo       Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

wlan0     Link encap:Ethernet  HWaddr 74:da:38:41:5a:1d
          inet addr:192.168.1.11  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::bc97:f8cd:1ef0:b73c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:139 errors:0 dropped:1 overruns:0 frame:0
          TX packets:39 errors:0 dropped:2 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:33900 (33.1 KiB)  TX bytes:6614 (6.4 KiB)

pi@raspberrypi:~ $

iwconfig will provide details about the Wi-Fi Hotspot that has been connected to, including its SSID, Frequency and MAC address, as well as information about the link quality and signal strength:

iwconfig

pi@raspberrypi:~ $ ifconfig
eth0      no wireless extensions.

wlan0     IEEE 802.11  ESSID:”HomeWiFi”
          Mode:Managed  Frequency:2.412 GHz  Access Point: 00:1D:AA:80:5F:80
          Bit Rate=65 Mb/s   Tx-Power=31 dBm
          Retry short limit:7   RTS thr:off   Fragment thr:off
          Power Management:on
          Link Quality=54/70  Signal level=-56 dBm
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:25  Invalid misc:0   Missed beacon:0

lo        no wireless extensions.

pi@raspberrypi:~ $

Renaming the Wi-Fi adapter and/or keeping the name the same

The names assigned to the Wi-Fi adapters – wlan0, wlan1, wlan2, etc is determined by the order in which the Wi-Fi adapters are detected. Therefore if you have more than one adapter, it is possible that upon reboot they could swap names. This could be an issue if there are serving different purposes with separate installation locations, antenna setups, etc.

To resolve this simply add a line to the file /etc/udev/rules.d/70-persistent-net.rules. If this file does not exist, then create it.

sudo nano /etc/udev/rules.d/70-persistent-net.rules

Add the following line where ATTR{address}=="" is set to the MAC address of the Wi-Fi adapter (use ifconfig to list the adapters), and NAME="" is set to the name you wish to assign. If you have more than one adapter that you wish to specify, then create one line per adapter and ensure the NAME is unique and the ATTR{address} is correct for each of the adapters.

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:13:ef:87:00:4c", ATTR{dev_id}=="0x0", ATTR{type}=="1", NAME="wlan0"

Leave a Reply

Your email address will not be published. Required fields are marked *