Raspberry Pi 3 Cookbook for Python Programmers
上QQ阅读APP看书,第一时间看更新

How to do it...

This task has two stages: first, we identify and install firmware for the Wi-Fi adapter, and then we need to configure it for the wireless network.

We will try to identify the chipset of your Wi-Fi adapter (the part that handles the connection); this may not match the actual manufacturer of the device.

An approximate list of supported firmware can be found with this command:

sudo apt-cache search wireless firmware  

This will produce results similar to the following output (disregarding any results without firmware in the package title):

atmel-firmware - Firmware for Atmel at76c50x wireless networking chips.
firmware-atheros - Binary firmware for Atheros wireless cards
firmware-brcm80211 - Binary firmware for Broadcom 802.11 wireless cards
firmware-ipw2x00 - Binary firmware for Intel Pro Wireless 2100, 2200 and 2915
firmware-iwlwifi - Binary firmware for Intel PRO/Wireless 3945 and 802.11n cards
firmware-libertas - Binary firmware for Marvell Libertas 8xxx wireless cards
firmware-ralink - Binary firmware for Ralink wireless cards
firmware-realtek - Binary firmware for Realtek wired and wireless network adapters
libertas-firmware - Firmware for Marvell's libertas wireless chip series (dummy package)
zd1211-firmware - Firmware images for the zd1211rw wireless driver  

To find out the chipset of your wireless adapter, plug the Wi-Fi-adapter into Raspberry Pi, and from the terminal, run the following command:

dmesg | grep 'Product:|Manufacturer:'
This command stitches together two commands into one. First, dmesg displays the message buffer of the kernel (this is an internal record of system events that have occurred since power on, such as detected USB devices). You can try the command on its own to observe the complete output.

The | (pipe) sends the output to the grep command; grep 'Product:|Manufacturer' checks it and only returns lines that contain Product or Manufacturer (so we should get a summary of any items that are listed as Product and Manufacturer). If you don't find anything or want to see all your USB devices, try the grep 'usb' command instead.

This should return something similar to the following output—in this case, I've got a ZyXEL device, which has a ZyDAS chipset (a quick Google search reveals that zd1211-firmware is for ZyDAS devices):

[    1.893367] usb usb1: Product: DWC OTG Controller
[    1.900217] usb usb1: Manufacturer: Linux 3.6.11+ dwc_otg_hcd
[    3.348259] usb 1-1.2: Product: ZyXEL G-202
[    3.355062] usb 1-1.2: Manufacturer: ZyDAS  

Once you have identified your device and the correct firmware, you can install it as you would any other package available through apt-get (where zd1211-firmware can be replaced with your required firmware). This is shown in the following command:

sudo apt-get install zd1211-firmware  

Remove and reinsert the USB Wi-Fi dongle to allow it to be detected and the drivers loaded. We can now test whether the new adapter is correctly installed with ifconfig. The output is shown as follows:

wlan0     IEEE 802.11bg  ESSID:off/any
          Mode:Managed  Access Point: Not-Associated   Tx-Power=20 dBm
          Retry  long limit:7   RTS thr:off   Fragment thr:off
          Power Management:off  

The command will show the network adapters present on the system. For Wi-Fi, this is usually wlan0 or wlan1 and so on if you have installed more than one. If not, double-check the selected firmware and perhaps try an alternative or check on the site for troubleshooting tips.

Once we have the firmware installed for the Wi-Fi adapter, we will need to configure it for the network we wish to connect to. We can use the GUI as shown in the previous recipe, or we can manually configure it through the Terminal, as shown in the following steps:

  1. We will need to add the wireless adapter to the list of network interfaces, which is set in /etc/network/interfaces, as follows:
sudo nano -c /etc/network/interfaces   

Using the previous wlan# value in place of wlan0 if required, add the following command:

allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf  

When the changes have been made, save and exit by pressing Ctrl + X, Y, and Enter.

  1. We will now store the Wi-Fi network settings of our network in the wpa_supplicant.conf file (don't worry if your network doesn't use the wpa encryption; it is just the default name for the file):
sudo nano -c /etc/wpa_supplicant/wpa_supplicant.conf  

It should include the following:

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

The network settings can be written within this file as follows (that is, if the SSID is set as theSSID):

    • If no encryption is used, use this code:
network={ 
  ssid="theSSID" 
  key_mgmt=NONE 
} 
    • With the WEP encryption (that is, if the WEP key is set as theWEPkey), use the following code:
network={ 
  ssid="theSSID" 
  key_mgmt=NONE 
  wep_key0="theWEPkey" 
} 
    • For the WPA or WPA2 encryption (that is, if the WPA key is set as theWPAkey), use the following code:
network={ 
  ssid="theSSID" 
  key_mgmt=WPA-PSK 
  psk="theWPAkey"     
} 
  1. You can enable the adapter with the following command (again, replace wlan0 if required):
sudo ifup wlan0
  

Use the following command to list the wireless network connections:

iwconfig
  

You should see your wireless network connected with your SSID listed, as follows:

wlan0     IEEE 802.11bg  ESSID:"theSSID"
          Mode:Managed  Frequency:2.442 GHz  Access Point: 
00:24:BB:FF:FF:FF
Bit Rate=48 Mb/s Tx-Power=20 dBm Retry long limit:7 RTS thr:off Fragment thr:off Power Management:off Link Quality=32/100 Signal level=32/100 Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0 Tx excessive retries:0 Invalid misc:15 Missed beacon:0

If not, adjust your settings and use sudo ifdown wlan0 to switch off the network interface, and then sudo ifup wlan0 to switch it back on. This will confirm that you have successfully connected to your Wi-Fi network.

  1. Finally, we will need to check whether we have access to the internet. Here, we have assumed that the network is automatically configured with DHCP and no proxy server is used. If not, refer to the Connecting to the internet through a proxy server recipe.

Unplug the wired network cable, if still connected, and see whether you can ping the Raspberry Pi website, as follows:

sudo ping www.raspberrypi.org  
If you want to quickly know the IP address currently in use by Raspberry Pi, you can use hostname -I, or to find out which adapter is connected to which IP address, use ifconfig.