转载请注明出处:http://www.codelast.com/

For non-Chinese readers:
I'm a software engineer in Shanghai, China, and since I bought a Raspberry Pi, I spent some spare time on it and I'm glad to share something with Pi users all over the world, so this article has two languages: Chinese & English, and I try to translate the Chinese to English as accurately as possible, but, there might be some translation mistakes, so if you don't know what I mean in the article, just leave a comment, and I'll reply when I get an answer. Also welcome to contact me via Email.

Raspberry Pi是什么?
引用维基百科的一句话:

The Raspberry Pi is a credit card sized single-board computer developed in the UK by the Raspberry Pi Foundation with the intention of stimulating the teaching of basic computer science in schools.

简单地说,它就是一个基于ARM CPU的、信用卡那么大的迷你计算机。
In short, Pi is an ARM-based mini computer which has a credit card size.

在阅读本文之前,我建议你阅读另外一篇配置树莓派的文章,本文与它有一些联系。
Before reading this article, I recommend you to read another one on Pi configuration because this article has some connection with it.

为了能让树莓派摆脱每次使用都要插网线的麻烦,我最终还是决定为它配备一块USB无线网卡。在Google了一番之后,我发现很多树莓派用户都提到基于Ralink RT5370芯片的USB无线网卡,于是我购买了一个基于RT5370芯片的腾达(Tenda)W311MI迷你无线USB网卡(如下图所示),非常便宜的白菜价:只要¥30。
To get rid of the inconveniency of connecting a network cable to Raspberry Pi I decided to equip it with a wireless USB adapter. After Googling the Internet I found that many Pi users mention the wireless USB adapter based on Ralink RT5370 chipset, so I bought one too - the Tenda W311MI 150Mbps Wireless Mini USB Adapter(shown as below), which based on RT5370 and has a very low price: only $4.8 .

Tenda W311MI 150Mbps Wireless Mini USB Adapter

关于如何在使用Arch Linux ARM系统的树莓派上使用RT5370芯片的USB无线网卡,网上很难找到完整的教程(用Debian系统的倒是有),所以我走了不少弯路,搞定了之后决定要写出来。所以,再次提醒你,如果你用的是Arch Linux ARM系统,并且使用的是RT5370芯片的USB无线网卡,那么阅读本文可能对你有所帮助;如果不是,那么就不用看下去了。
It's hard to find a detailed tutorial on how to use a RT5370-chipset-based wireless USB adapter on Raspberry Pi which runs the Arch Linux ARM os(but there are some good tutorials on Debian), so I wasted a lot of time in the setup process and when I figured it out I decided to write them down. So be sure again that if you are running the Arch Linux ARM on Pi and using a wireless USB adapter which based on RT5370 chipset, then you'll read something useful in this article, if not, don't waste your time and just close this webpage.
文章来源:http://www.codelast.com/
【1】关于驱动 / About the driver

这个页面是Ralink RT5370芯片的官方驱动下载页面,不过,在整个配置过程中我们完全不用下载此驱动,因为Arch Linux ARM已经自带了,非常方便。
This page is the official driver download page, but we don't need it in the whole setup process because Arch Linux ARM has it installed already.

插上USB无线网卡后,用如下命令查看一下:
After plugging the wireless USB adapter, use following command to have a check:
[root@alarmpi ~]# lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp. LAN9500 Ethernet 10/100 Adapter
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. 
Bus 001 Device 004: ID 148f:5370 Ralink Technology, Corp. RT5370 Wireless Adapter
可见Ralink RT5370芯片已经被识别出来了,这是成功的第一步。
So the Ralink RT5370 chipset has been recognized and this is the first step to success.
文章来源:http://www.codelast.com/
然后我们还可以查看一下系统已经加载的模块:
Then we can check the modules which have been loaded by the OS:
[root@alarmpi ~]# lsmod
Module                  Size  Used by
rt2800usb              12977  0 
rt2800lib              47334  1 rt2800usb
rt2x00usb              10895  1 rt2800usb
rt2x00lib              40765  3 rt2x00usb,rt2800lib,rt2800usb
spidev                  5028  0 
mac80211              234117  3 rt2x00lib,rt2x00usb,rt2800lib
cfg80211              173637  2 mac80211,rt2x00lib
crc_ccitt               1433  1 rt2800lib
spi_bcm2708             4357  0 
i2c_bcm2708             3557  0 
ipv6                  289527  16
根据这篇文章所言,其中,rt2x00是Ralink芯片组的统一驱动。
According to this article, the rt2x00 is the uniform driver for Ralink chipsets.
文章来源:http://www.codelast.com/
【2】安装wireless_tools / Install wireless_tools
在配置过程中,我们要安装一个配置无线网络接口的工具——iwconfig。用如下命令安装:
First we need to install a wireless network interface configuration tool - iwconfig :
[root@alarmpi ~]# pacman -S wireless_tools
安装此工具后,你就可以用iwconfig命令看看你的无线网卡信息了:
Then you can use iwconfig to check the info of your wireless USB adapter:
[root@alarmpi ~]# iwconfig 
ifb0      no wireless extensions.
lo        no wireless extensions.
wlan0     IEEE 802.11bgn  ESSID:"XXX"  
Mode:Managed  Frequency:2.412 GHz  Access Point: XXXXXXXXXXX   
Bit Rate=54 Mb/s   Tx-Power=20 dBm   
Retry  long limit:7   RTS thr:off   Fragment thr:off
Encryption key:off
Power Management:on
Link Quality=70/70  Signal level=-39 dBm  
Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
Tx excessive retries:0  Invalid misc:18   Missed beacon:0
eth0      no wireless extensions.
ifb1      no wireless extensions.
“wlan0”就是你要关注的。
The "wlan0" is what you should focus on.
文章来源:http://www.codelast.com/
【3】配置无线连接的帐户 / Setup the WiFi account
我的无线网络是WPA加密的,要连接到这样的无线网络,需要使用wpa_supplicant工具。如果不出意外,你会发现Arch Linux ARM已经安装好了这个工具:
In order to connected to a WPA encrypted wireless network we need to use a tool like wpa_supplicant. No accident that you'll find it already been installed:
[root@alarmpi ~]# pacman -Q | grep wpa     
wpa_supplicant 1.0-2
所以我们直接用就可以了。如果你的系统没装,那么就用pacman装一下,也非常容易。
So we just use it. If you can't find the package, it's very easy to use pacman to install it.
 
为防止误操作,先备份wpa_supplicant的配置文件:
Backup the configuration file of wpa_supplicant to avoid misoperation:
mv /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf.bak
文章来源:http://www.codelast.com/
然后用wpa_passphrase工具生成一个简单的配置文件:
Use wpa_passphrase tool to generate a very simple configuration file:
wpa_passphrase myWirelessSSID myPassword > /etc/wpa_supplicant/wpa_supplicant.conf
其中,“myWirelessSSID”是你的无线网络的SSID,“myPassword”是连接该无线网络的密码。wpa_passphrase工具会根据你提供的这两个信息生成一个非常简单的配置文件,此处我们将输出的信息写入了文件 /etc/wpa_supplicant/wpa_supplicant.conf 中。
The "myWirelessSSID" is the SSID of your wireless network, "myPassword" is the password used to connect the network. wpa_passphrase tool will generate a very simple configuration file according to the info you provided and here we write the output to file /etc/wpa_supplicant/wpa_supplicant.conf.
 
启动网卡:
Bring wlan0 up:
ifconfig wlan0 up
连接到网络:
Connect to the network:
wpa_supplicant -B -Dwext -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
文章来源:http://www.codelast.com/
但是,在没有获取IP前,我们还是无法通过无线网卡访问树莓派的,用下面的命令来获取一个IP地址:
But we can't access Pi until it get an available IP address, use following command to get one:
[root@alarmpi ~]# dhcpcd wlan0
dhcpcd[286]: version 5.6.2 starting
dhcpcd[286]: wlan0: sending IPv6 Router Solicitation
dhcpcd[286]: wlan0: broadcasting for a lease
dhcpcd[286]: wlan0: offered 192.168.X.X from 192.168.X.X
dhcpcd[286]: wlan0: acknowledged 192.168.X.X from 192.168.X.X
dhcpcd[286]: wlan0: checking for 192.168.X.X
dhcpcd[286]: wlan0: sending IPv6 Router Solicitation
dhcpcd[286]: wlan0: leased 192.168.16.10 for 86400 seconds
dhcpcd[286]: forked to background, child pid 327
现在树莓派应该可以连接无线网络了。
Now Pi should be able to connect the wireless network.
 
【4】开机自动连接无线网络 / Connect on system startup
为了能让树莓派开机自动连接无线网络,将如下两句shell命令添加到开机执行的列表中:
To let Pi connects the wireless network automatically, add the following 2 shell commands to OS startup execution list:
wpa_supplicant -B -Dwext -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
dhcpcd wlan0
至于怎么添加,你可以参考这篇文章的第【2】条。
You can refer to this article, item 【2】 on how to execute shell command on startup.
文章来源:http://www.codelast.com/
如果上面的方法不能让无线网卡工作,可以试试从第2步开始用下面的方法(以下命令均需以root用户执行):
* 先安装一个让你可以在shell命令行界面显示类似于图形界面对话框的程序:

pacman -S dialog

* 使用 wifi-menu 程序,来“图形化”(利用上面安装的dialog程序)地连接上无线网络:

wifi-menu -o

然后就会进入交互式界面,首先会扫描无线网络,如果扫描到了,会在一个类图形界面里显示出来所有无线网络的SSID,用键盘选择后进入下一步,输入无线网络的密码,以及设置一个配置文件的名字(可用默认名字,生成的配置文件在 /etc/netctl/ 目录下),然后就应该OK了,经过大概一两分钟的等待(界面完全没有任何文字或图形显示),无线网络就连上了,用 ifconfig 看看,是不是无线网卡已经分配到IP地址了?
但是这样的方法,并不能让树莓派开机的时候自动连接WiFi,可以在开机自动执行的脚本中添加下面这句话,让树莓派开机自动连接WiFi:

netctl start xxx

其中,xxx 是你前面用 wifi-menu 程序生成的配置文件名,注意,这里是不需要写全路径的,netctl程序会自动地去 /etc/netctl/ 目录下找名为 xxx 的配置文件并用它里面写的配置内容来连接对应的网络。
当然,上面的开机启动方式可能有点不规范,可以用 systemd 的方式来设置开机启动:

netctl enable xxx

这里 xxx 也是你的WiFi配置文件名。这将创建并启用一个随计算机启动而自动运行的 systemd 服务(参考这个链接)。
文章来源:http://www.codelast.com/
最后展示一下USB无线网卡在树莓派上的工作图(可见有线网卡工作状态指示灯没亮):
At last show you the wireless USB adapter working on Pi(it's obvious that the LNK status LED is off):

Tenda W311MI 150Mbps Wireless Mini USB Adapter on Raspberry Pi

至此,本文的主要目的已经达到。如果你还想对树莓派了解更多,请看这些文章
So the main purpose of this article has been reached. If you want to know more about Pi, please read these articles.
[原创]在树莓派上使用腾达(Tenda)W311MI迷你无线USB网卡(RT5370芯片)/Using Tenda W311MI mini wireless USB adapter(based on RT5370 chipset) on Raspberry Pi

14 thoughts on “[原创]在树莓派上使用腾达(Tenda)W311MI迷你无线USB网卡(RT5370芯片)/Using Tenda W311MI mini wireless USB adapter(based on RT5370 chipset) on Raspberry Pi

  • 2016 年 01 月 23 日 at 22:45
    Permalink

    HELP!
    似乎我也遇到这个问题
    [root@alarmpi wpa_supplicant]# wpa_supplicant -B -Dwext -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
    Successfully initialized wpa_supplicant
    ioctl[SIOCSIWAP]: Operation not permitted
    ioctl[SIOCSIWENCODEEXT]: Invalid argument
    ioctl[SIOCSIWENCODEEXT]: Invalid argument
    多谢了 >_<!

    Reply
    • 2016 年 01 月 24 日 at 13:34
      Permalink

      用文中所说的 wifi-menu 程序来图形化地配置试一试,如果不行,我想不出其他办法了

      Reply
        • 2016 年 01 月 24 日 at 15:23
          Permalink

          太感激你了,我在网上找了好多wiki和帖子都没成功,用你说的这个netctl auto终于行了,应该是/etc/netctl/中那个wlan*的文件没做对,自动生成就成功了,谢谢了!

          Reply
  • 2014 年 01 月 27 日 at 22:14
    Permalink

    呃,根据阁下的教程弄了半天,可还是没有弄出来,55555555.在执行 wpa_supplicant -B -Dwext -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf 是提示(两行):ioctl[SIOCSIWENCODEEXT]: Invalid argument
    ioctl[SIOCSIWENCODEEXT]: Invalid argument
    请问阁下这是怎么回事?另外,我的pi上有个wifi config(openssl项目的),不知道配置好后,这个软件是否能用?

    Reply
    • 2014 年 01 月 28 日 at 23:00
      Permalink

      这个...真心不知道是怎么回事,你只能去Google了。我也不知道wifi config能不能用,因为我没用过。
      我觉得玩树莓派遇到的问题多多,就是折腾来折腾去,我前面有篇关于树莓派SD卡调整分区大小的文章,有个台湾网友看了之后说照做无效,但是在我这里就是有用的,所以我只能说,某些奇葩的问题只能靠自己去尝试解决啊,很无奈

      Reply
  • 2013 年 05 月 09 日 at 05:11
    Permalink

    你好,如果是开放wifi 没有密码 改怎么配置呢?

    Reply
    • 2013 年 05 月 09 日 at 08:01
      Permalink

      这个解决了,另外WPA/WPA2 Enterprise (需要输入用户名和密码)有办法连接吗

      Reply
  • 2013 年 01 月 20 日 at 12:38
    Permalink

    请问支持W311M这款无线网卡吗?我手上刚好有一块,不知道支持不支持~

    Reply
  • 2013 年 01 月 10 日 at 22:02
    Permalink

    问下 不能在图形界面下配置吗?

    Reply
    • 2013 年 01 月 11 日 at 10:30
      Permalink

      应该没有图形配置界面。玩Raspberry Pi的就不要指望着图形界面了,毕竟是Linux,命令行才是王道

      Reply

发表评论

电子邮件地址不会被公开。 必填项已用*标注