2015年4月5日 星期日

Raspberry Pi 的連接點設定


新機體打算用 Raspberry Pi 2 作為主板,考慮到現今 iOS 及 Android 大行其道,機體需要支援這些流動裝置。但要開發 iOS 及 Android 控制程式的話十分費時,而且使用時需要安裝應用程式,若系統加入新功能後又需要升級,不太方便。所以,今次嘗試把控制程式安裝在 Raspberry Pi 內,透過 WiFi 連接點(Access Point)把 Pi 及手機連線,同時解決了平台、安裝及更新問題。

不過問題來了。之前購買的 D-Link DWA-131 並不支援連接點模式,需要購買新的 WiFi USB。在淘寶找到了一些聲稱支援 Raspberry Pi 及連接點模式的 WiFi USB,價錢連運費約 HK$100。朋友替我在深水埗找到了一顆 TP-Link TL-WN725N,價錢只是 HK$85,二話不說便買了回來。參考了朋友的網頁進行設定。

把 TL-WN725N 插進 Raspberry Pi 2,同時也接上 LAN 線、鍵盤、滑鼠。啟動 Raspberry Pi 後以 Pi 登入並輸入 ifconfig 會發現找不到 TL-WN725N。解決方法是:
pi@raspberrypi ~ $ sudo wget https://github.com/lwfinger/rtl8188eu/raw/c83976d1dfb4793893158461430261562b3a5bf0/rtl8188eufw.bin -O /lib/firmware/rtlwifi/rtl8188eufw.bin

重啟後再輸入 ifconfig 便能看到 TL-WN725N
pi@raspberrypi ~ $ ifconfig
eth0      Link encap:Ethernet  HWaddr 88:22:ee:cc:dd:ff  
          inet addr:192.168.1.10  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:159 errors:0 dropped:0 overruns:0 frame:0
          TX packets:81 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:13895 (13.5 KiB)  TX bytes:12282 (11.9 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          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:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

wlan0     Link encap:Ethernet  HWaddr 33:bb:22:11:99:00  
          UP BROADCAST MULTICAST  MTU:1500  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:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

之後確保系統在最新狀態:
pi@raspberrypi ~ $ sudo apt-get update
pi@raspberrypi ~ $ sudo apt-get upgrade

接著是安裝 hostapd:
pi@raspberrypi ~ $ sudo apt-get install hostapd isc-dhcp-server

設定 DHCP 服務器,紅色是改動了的地方:
pi@raspberrypi ~ $ sudo nano /etc/dhcp/dhcpd.conf
    ::    ::    ::    ::    ::
# option definitions common to all supported networks...
#option domain-name "example.org";
#option domain-name-servers ns1.example.org, ns2.example.org;
    ::    ::    ::    ::    ::
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;
    ::    ::    ::    ::    ::
subnet 192.168.2.0 netmask 255.255.255.0  {
        range 192.168.2.200 192.168.2.250;
        option broadcast-address 192.168.2.255;
        option routers 192.168.2.1;
        default-lease-time 600;
        max-lease-time 7200;
        option domain-name "rpi2-ap";
        option domain-name-servers 8.8.8.8, 8.8.4.4;
}

修改 wlan0 設定,紅色是改動了的地方:
pi@raspberrypi ~ $ sudo nano /etc/network/interfaces
auto lo

iface lo inet loopback
iface eth0 inet dhcp

##----------------------------------------
##  Original wlan0 settings
##----------------------------------------
#allow-hotplug wlan0
#iface wlan0 inet manual
#wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
#iface default inet dhcp

allow-hotplug wlan0
iface wlan0 inet static
  address 192.168.2.1
  netmask 255.255.255.0

更換 hostapd:
pi@raspberrypi ~ $ wget http://www.daveconroy.com/wp3/wp-content/uploads/2013/07/hostapd.zip
pi@raspberrypi ~ $ unzip hostapd.zip
pi@raspberrypi ~ $ sudo mv /usr/sbin/hostapd /usr/sbin/hostapd.bak
pi@raspberrypi ~ $ sudo mv hostapd /usr/sbin/hostapd
pi@raspberrypi ~ $ sudo chmod 755 /usr/sbin/hostapd
pi@raspberrypi ~ $ sudo update-rc.d hostapd enable

設定 Raspberry Pi 2 的 IP 地址:
pi@raspberrypi ~ $ sudo ifconfig wlan0 192.168.2.1 
pi@raspberrypi /etc/network $ ifconfig
eth0      Link encap:Ethernet  HWaddr 88:22:ee:cc:dd:ff
          inet addr:192.168.1.120  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8958 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3610 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:8855945 (8.4 MiB)  TX bytes:418480 (408.6 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          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:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

wlan0     Link encap:Ethernet  HWaddr 33:bb:22:11:99:00  
          inet addr:192.168.2.1  Bcast:192.168.2.255  Mask:255.255.255.0
          UP BROADCAST MULTICAST  MTU:1500  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:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

設定 hostapd.conf:
pi@raspberrypi ~ $ sudo nano /etc/hostapd/hostapd.conf
interface=wlan0
driver=rtl871xdrv
ssid=RPi_AP
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=password   
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

修改 hostapd.conf:
pi@raspberrypi ~ $ sudo nano /etc/default/hostapd
# Defaults for hostapd initscript
#
# See /usr/share/doc/hostapd/README.Debian for information about alternative
# methods of managing hostapd.
#
# Uncomment and set DAEMON_CONF to the absolute path of a hostapd configuration
# file and hostapd will be started during system boot. An example configuration
# file can be found at /usr/share/doc/hostapd/examples/hostapd.conf.gz
#
#DAEMON_CONF=""
DAEMON_CONF="/etc/hostapd/hostapd.conf"

# Additional daemon options to be appended to hostapd command:-
#       -d   show more debug messages (-dd for even more)
#       -K   include key data in debug messages
#       -t   include timestamps in some debug messages
#
# Note that -B (daemon mode) and -P (pidfile) options are automatically
# configured by the init.d script and must not be added to DAEMON_OPTS.
#
#DAEMON_OPTS=""

最後就是安裝 Nginx。成功後利用 iPad 接上 Raspberry Pi 2,開啟瀏覽器,輸入地址 http://192.168.2.1/,看到「Welcome Nginx!」便代表成功!

沒有留言: