官术网_书友最值得收藏!

Adding Wi-Fi dongle over USB OTG

An easy way to communicate with your Pi and get your little computer connected to the Internet is connecting it to a USB Wi-Fi adapter. Once you have your Raspberry Pi Zero connected to Wi-Fi, we can get it to interact with pretty much anything that lives on your home network.

Getting ready

For this recipe, you will need your Raspberry Pi Zero, a USB OTG adapter, and a USB Wi-Fi device. Most of this equipment comes with any starter kit and is inexpensive to buy inpidually-the OTG adapters can be found for less than $5 and Wi-Fi dongles are $10-$15. It is still amazing that a lot of these accessories cost at least as much as the computer they are needed for!

A small USB OTG dongle and Wi-Fi adapter make for a pretty small setup:

You do need to add power, but even with that, you maintain a very small footprint for what will be an Internet-enabled computer.

The embedded Linux site ( elinux.org ) has a comprehensive listing of tested and compatible Wi-Fi cards. If you don't use one from the list, behavior may be unpredictable or the device may not work at all. The list also identifies which ones can be used without a powered USB hub. Another important note about that list is whether the driver for the card is preinstalled or not-if you pick one that is preinstalled, then your Raspberry Pi should recognize it immediately. If not, you'll need to get the driver files onto your Pi first.

Of course, if you have one laying around (as many hardware geeks do), try that first: many devices will work, or can be configured to, without too much trouble.

Finally, you'll want to know your Wi-Fi network's SSID and password so you can authenticate your Zero.

How to do it...

If you have a Wi-Fi dongle that is listed as preinstalled on Raspbian, this should be a very easy recipe. Using the USB-serial connection you established in the previous recipe, we can get Wi-Fi configured:

  1. Start with the lsusb command to see whether it has been recognized by your Raspberry Pi Zero:
    pi@rpz14101:/usr/share/ppp$ lsusb
    Bus 001 Device 012: ID 0bda:8176 Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN Adapter
    Bus 001 Device 002: ID 05e3:0608 Genesys Logic, Inc. USB-2.0 4-Port HUB
    Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
    
  2. Here at the top of the list is our Wi-Fi device. If your device showed up, it should be pretty easy to get yourself connected to your wireless network. If not, check dmesg to see whether something went wrong. This is the command:
    dmesg | grep -I usb | tail -20
    

    This command instructs the Pi to search the logs for anything containing usb or USB, and return the most recent 20 lines. If the following test run, first a USB dongle with power issues was attached, followed by a working dongle. You can see the difference between a USB device that attached correctly versus one that experienced an error:

    pi@rpz14101:/usr/share/ppp$ dmesg | grep -i usb | tail -20
    [ 4222.016769] usb 1-1.3: device not accepting address 7, error -32
    [ 4222.017187] usb 1-1-port3: unable to enumerate USB device
    [ 8456.015076] usb 1-1: reset high-speed USB device number 2 using dwc_otg
    [ 8456.515070] usb 1-1.3: new full-speed USB device number 8 using dwc_otg
    [ 8456.595063] usb 1-1.3: device descriptor read/64, error -32
    [ 8456.785087] usb 1-1.3: device descriptor read/64, error -32
    [ 8456.975074] usb 1-1.3: new full-speed USB device number 9 using dwc_otg
    [ 8457.055082] usb 1-1.3: device descriptor read/64, error -32
    [ 8457.245079] usb 1-1.3: device descriptor read/64, error -32
    [ 8457.435082] usb 1-1.3: new full-speed USB device number 10 using dwc_otg
    [ 8457.855082] usb 1-1.3: device not accepting address 10, error -32
    [ 8457.935163] usb 1-1.3: new full-speed USB device number 11 using dwc_otg
    [ 8458.355084] usb 1-1.3: device not accepting address 11, error -32
    [ 8458.355431] usb 1-1-port3: unable to enumerate USB device
    [29178.281722] usb 1-1.3: new high-speed USB device number 12 using dwc_otg
    [29178.383627] usb 1-1.3: New USB device found, idVendor=0bda, idProduct=8176
    [29178.383667] usb 1-1.3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
    [29178.383690] usb 1-1.3: Product: 802.11n WLAN Adapter
    [29178.383710] usb 1-1.3: Manufacturer: Realtek
    [29178.383732] usb 1-1.3: SerialNumber: 000000000000
    
  3. If your device won't get picked up by your Raspberry Pi Zero, you can search for your error message on the Internet to see whether someone has experienced the same thing, or, ideally, use a spare USB Wi-Fi dongle or one you can borrow (it's probably not something wrong with your Pi). Most compatible USB dongles will attach without incident and are ready to get connected to your Wi-Fi network. Running ifconfig should show the interface, but without any connection information:
    pi@rpz14101:/usr/share/ppp$ ifconfig
    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:1992 errors:0 dropped:0 overruns:0 frame:0
     TX packets:1992 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1
     RX bytes:161808 (158.0 KiB) TX bytes:161808 (158.0 KiB)
    wlan0 Link encap:Ethernet HWaddr 00:13:ef:80:0b:41
     inet addr:192.168.2.119 Bcast:192.168.2.255 Mask:255.255.255.0
     inet6 addr: fe80::a577:b1b7:a7a7:8a60/64 Scope:Link
     UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
     RX packets:20 errors:0 dropped:10000 overruns:0 frame:0
     TX packets:55 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000
    
  4. Now we just need to find and connect to our network. This is very easy to do from the command line. To search for available networks, we use the iwlist command:
    sudo iwlist wlan0 scan | grep SSID | sort | uniq
    

    This command asks your Wi-Fi adapter, wlan0, to scan for available networks. This outputs a lot of information, so we've piped it to the grep command to filter out the data we don't need in order to connect. The sort and uniq commands sort the SSIDs by name and remove any duplicates:

    pi@rpz14101:~ $ sudo iwlist wlan0 scan | grep SSID | sort | uniq
     ESSID:""
     ESSID:"141FAST"
     ESSID:"FBI Surveillance Van 3"
     ESSID:"bullies"
     ESSID:"HOME-7068-2.4"
     ESSID:"L78MH"
     ESSID:"NETGEAR45"
    

    Hopefully, you'll see the network you want to connect to. If not, you may have to look into your signal from your router. Timing can be a factor too, as SSIDs are broadcast on an interval. Give the command a few tries if nothing comes up the first time.

  5. Once you've found it, you just need to add it to the list of available SSIDs for your Raspberry Pi Zero to use. For this, we'll edit the wpa_supplicant.conf file, located in /etc/wpa_supplicant/wpa_supplicant.conf. Initially, it should be mostly empty, like this:
    pi@rpz14101:~ $ sudo cat /etc/wpa_supplicant/wpa_supplicant.conf
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1
    country=US
    
  6. We will add some lines to this file for the SSID to be used. The format is as follows:
            network = {
     ssid="MYSSID"
     psk="My SSID Password"
            }
    

    Add this to the file, save it, and then run this:

            sudo ifdown wlan0; sudo ifup wlan0
    
  7. Finally, check ifconfig again. Your wlan0 interface should now show an IP address and additional information it needs to work on the Wi-Fi network:
            pi@rpz14101:/usr/share/ppp$ ifconfig
            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:1992 errors:0 dropped:0 overruns:0 frame:0
     TX packets:1992 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1
     RX bytes:161808 (158.0 KiB) TX bytes:161808 (158.0 KiB)
            wlan0 Link encap:Ethernet HWaddr 00:13:ef:80:0b:41
     inet addr:192.168.2.119 Bcast:192.168.2.255 Mask:255.255.255.0
     inet6 addr: fe80::a577:b1b7:a7a7:8a60/64 Scope:Link
     UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
     RX packets:20 errors:0 dropped:10000 overruns:0 frame:0
     TX packets:55 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000
     RX bytes:3466461 (3.3 MiB) TX bytes:115712 (113.0 KiB)
    

That's it! Your Raspberry Pi Zero is now on your Wi-Fi network, much like any other computer, tablet, cell phone, or any other wireless, internet-enabled device you have in your home.

主站蜘蛛池模板: 永新县| 肃宁县| 沁源县| 广元市| 岳普湖县| 山阴县| 山丹县| 扎囊县| 抚松县| 阿克苏市| 福州市| 聂荣县| 孝感市| 宿州市| 汾西县| 德格县| 天台县| 酉阳| 浠水县| 西和县| 湟源县| 凤城市| 抚州市| 永新县| 赤壁市| 调兵山市| 福清市| 忻州市| 巴林左旗| 临海市| 祁门县| 扬中市| 淮南市| 界首市| 嘉定区| 临安市| 富裕县| 华安县| 临泉县| 博野县| 泌阳县|