Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Raspberry Pi cookbook for Python programmers

You're reading from   Raspberry Pi cookbook for Python programmers The Raspberry Pi Cookbook has over 50 tailor-made recipes for programmers to get the most out of Raspberry Pi using Python to unleash its huge potential.

Arrow left icon
Product type Paperback
Published in Apr 2014
Publisher
ISBN-13 9781849696623
Length 402 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (18) Chapters Close

Raspberry Pi Cookbook for Python Programmers
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Getting Started with a Raspberry Pi Computer FREE CHAPTER 2. Starting with Python Strings, Files, and Menus 3. Using Python for Automation and Productivity 4. Creating Games and Graphics 5. Creating 3D Graphics 6. Using Python to Drive Hardware 7. Sense and Display Real-world Data 8. Creating Projects with the Raspberry Pi Camera Module 9. Building Robots 10. Interfacing with Technology Hardware and Software List Index

Networking and connecting your Raspberry Pi to the Internet via a USB Wi-Fi dongle


Many home networks provide a wireless network over Wi-Fi. By adding a USB Wi-Fi dongle to the Raspberry Pi's USB port, it can connect to and use the Wi-Fi network.

Getting ready

You shall need to obtain a suitable USB Wi-Fi dongle; and in some cases, you may require a powered USB hub (this will depend on the hardware version of the Raspberry Pi you have and the quality of your power supply). General suitability of USB Wi-Fi dongles will vary depending on the chipset that is used inside and the level of Linux support available. You may find that some USB Wi-Fi dongles will work without installing additional drivers (in which case you can jump to configuring it for the wireless network)

A list of supported Wi-Fi adapters is available at http://elinux.org/RPi_VerifiedPeripherals#USB_WiFi_Adapters.

You will need to ensure that your Wi-Fi adapter is also compatible with your intended network; for example, supporting the same types of signals 802.11bgn and the encryptions WEP, WPA, and WPA2 (although most networks are backward compatible).

You will also need the following details of your network:

  • Service set identifier (SSID): This is the name of your Wi-Fi network and should be visible if you use the following command:

    sudo iwlist scan | grep SSID
    
  • Encryption type and key: This value will be None, WEP, WPA, or WPA2, and the key will be the code you normally enter when you connect your phone or laptop to the wireless network (sometimes, it is printed on the router).

You will require a working Internet connection (that is, wired Ethernet) in order to download the required drivers. Otherwise, you may be able to locate the required firmware files (they will be the .deb files), and copy them to the Raspberry Pi (that is, via a USB flash drive; the drive should be automatically mounted if you are running in the desktop mode). Copy the file to a suitable location and install it with the following command:

sudo apt-get install firmware_file.deb

How to do it…

This task has two stages; first, we would identify and install firmware for the Wi-Fi adapter, and then we would 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 the following 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:'

Note

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:\|Manuf' checks it and only returns lines that contain Product or Manuf (so we should get a summary of any items, which are listed as Product and Manufacturer). If you don't find anything or want to see all your USB devices, try grep 'usb' 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 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 for any other package available through apt-get (where zd1211-firmware can be replaced with your required firmware as) 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 if 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 as wlan0, or wlan1, or 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. Perform 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, add the following command:

    auto wlan0
    iface wlan0 inet dhcp
    wpa-conf /etc/wpa.conf
    

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

  2. We will now store the Wi-Fi network settings of our network in the wpa.conf file (don't worry if your network doesn't use the wpa encryption; it is just the default name for the file) as follows:

    sudo nano -c /etc/wpa.conf
    

    The following information (that is, if the SSID is set as theSSID):

    • If no encryption is used, use the following 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"
      }
    • Or 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"
      }
  3. 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.

  4. 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 if we can ping the Raspberry Pi website as follows:

    ping www.raspberrypi.org
    

Tip

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

There's more…

The Model A version of the Raspberry Pi does not have a built-in network port; so in order to get a network connection, a USB network adapter will have to be added (either a Wi-Fi dongle, as explained in the preceding section, or a LAN-to-USB adapter, as described in the following section.

Using USB wired network adapters

Just like the USB Wi-Fi, the adapter support will depend on the chipset used and the drivers available. Unless the device comes with the Linux drivers, you may have to search the Internet to obtain the suitable Debian Linux drivers.

If you find a suitable .deb file, you can install it with the following command:

sudo apt-get install firmware_file.deb

Ensure that you also check using ifconfig, as some devices will be supported automatically and will appear as eth1 (or eth0 on Model A), and be ready to use immediately.

You have been reading a chapter from
Raspberry Pi cookbook for Python programmers
Published in: Apr 2014
Publisher:
ISBN-13: 9781849696623
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime