Raspberry PiでIoTなシステム開発:ワイヤレスアクセスポイントとして動作させるまで
先日書いたこちらの記事が以下の理由により使えないことが判明しました。その他にも色々と問題がありましたので、改訂版マニュアルを以下したためます。
Raspberry PiでIoTなIT:WordPressはダブルクォーテーションを置き換える
イメージの取得
これを使います。ローカルでunzipする。
df -h
これでSDが刺さっているドライブのデバイス名をメモ。
sudo dd bs=1m if=~/Downloads/2014-09-09-wheezy-raspbian/2014-09-09-wheezy-raspbian.img of=/dev/rdisk3
eLinuxは1Mになっているけれどもエラーになる。1mで実行。ifはunzipしたイメージを指定。of以下のrdiskxのxはデバイス名の数字disk3s1の3の方に置き換える。
接続
Raspberry PiでIoTなシステム開発:OSXのインターネット共有でIPを割り当ててログイン
Macのインターネット共有を利用してRaspberry PiにIPを振る。ただしアクセスは次項の通りホスト名で。
ログイン
Raspberry PiでIoTなシステム開発:Raspberry Piにホスト名でアクセスする
今のwheezyはavahiがデフォルトでインストールされているようで、いきなりbonjourでホスト名の確認ができます。OSXのターミナルからホスト名でログイン。
kawauchiyasuo-no-MacBook-Pro:~ yasuo$ ssh pi@raspberrypi.local Warning: the RSA host key for 'raspberrypi.local' differs from the key for the IP address '192.168.2.10' Offending key for IP in /Users/yasuo/.ssh/known_hosts:9 Matching host key in /Users/yasuo/.ssh/known_hosts:11 Are you sure you want to continue connecting (yes/no)? yes pi@raspberrypi.local's password: Linux raspberrypi 4.1.13-v7+ #826 SMP PREEMPT Fri Nov 13 20:19:03 GMT 2015 armv7l The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Mon Feb 8 07:16:17 2016 from kawauchiyasuo-no-macbook-pro.local pi@raspberrypi ~ $
最初の作業
Raspbianを入れたら最初はアップデート、タイムゾーン設定、電源設定変更。
sudo apt-get update sudo apt-get upgrade
しばらく時間がかかります。
sudo raspi-config
1 Expand Filesystemと4 Internationalization Optionを実行。4ではタイムゾーンをTokyoに設定。
起動安定化のため /boot/config.txtを編集。
pi@raspberrypi ~ $ sudo nano /boot/config.txt
末尾に追加。
safe_mode_gpio=4 max_usb_current=1
USBドライバ設定の追加
以前は以下の設定を追加していましたが、現在のwheezyでは設定をせずともWN-G150UMを認識しました。設定を間違えると逆にwlan0が消えるので注意。何も触らない方が吉でしょう。
pi@navio-rpi ~ $ sudo vi /etc/udev/rules.d/network_drivers.rules ACTION=="add",SUBSYSTEM=="usb", ATTR{idVendor}=="04bb", ATTR{idProduct}=="094c", RUN+="/sbin/modprobe -qba 8192cu" pi@navio-rpi ~ $ sudo vi /etc/modprobe.d/network_drivers.conf install 8192cu /sbin/modprobe –ignore-install 8192cu $CMDLINE_OPTS; /bin/echo "04bb 094c" > /sys/bus/usb/drivers/rtl8192cu/new_id
クライアントとして接続テスト
アクセスポイント化する前にクライアントとしてつながるかをテスト。
pi@navio-rpi ~ $ sudo nano /etc/network/interfaces
auto lo iface lo inet loopback iface eth0 inet dhcp auto wlan0 iface wlan0 inet dhcp wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
pi@navio-rpi ~ $ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 # Default network ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 network={ ssid="apname" psk="hoge" }
インターフェースの再起動
pi@raspberrypi ~ $ sudo ifdown wlan0 ifdown: interface wlan0 not configured pi@raspberrypi ~ $ sudo ifup wlan0
ifconfigでwlan0にIPアドレスが振られていれば成功。
wlan0 Link encap:Ethernet HWaddr 34:76:c5:5d:34:91 inet addr:192.168.0.108 Bcast:192.168.0.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:140 errors:0 dropped:345 overruns:0 frame:0 TX packets:27 errors:0 dropped:1 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:186351 (181.9 KiB) TX bytes:4399 (4.2 KiB)
pingは-Iオプションで使用するインターフェースを指定できる。ルーターや外部と通信できることを確認。
ping -I wlan0 192.168.0.1 ping -I wlan0 yahoo.com
同じルーターの配下にある別のマシンからルーティングできていることを確認。
kawauchiyasuo-no-MacBook-Pro:~ yasuo$ ping 192.168.0.108 PING 192.168.0.108 (192.168.0.108): 56 data bytes Request timeout for icmp_seq 0 Request timeout for icmp_seq 1 64 bytes from 192.168.0.108: icmp_seq=2 ttl=64 time=20.403 ms 64 bytes from 192.168.0.108: icmp_seq=3 ttl=64 time=4.651 ms
hostapdのインストール
ドライバ設定を追加しなくてもWN-G150UMを使えたのだからhostapdもそのままいけるのではと試して見たのですが、やはりRealtekの対応版に差し替えないとダメでした。まずは本家をインストールして、Realtek版にバイナリを差し替えます。
pi@raspberrypi ~ $ sudo apt-get install hostapd
realtekサイトはダウン比率が高い様ですね。なんとかgetしてください。私が使用したのは「0001-RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911.zip」です。とりあえずhostapdはGPLのようなので、その部分だけアップしておきます。
wpa_supplicant_hostapd-0.8_rtw_r7475.20130812.tar.gz
piからはアドレスが指定しにくいので、母艦のMacでダウンロードしてからSFTPで/home/piにアップロード。FileZillaを使っています。Mac用のFTPクライアントとしては一番使いやすいと思います。なのに何故かマイナー。
pi@raspberrypi ~ $ ls 0001-RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911.zip pi@raspberrypi ~ $ cd RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/ pi@raspberrypi ~/RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911 $ cd wpa_supplicant_hostapd/ pi@raspberrypi ~/RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/wpa_supplicant_hostapd $ tar zxvf wpa_supplicant_hostapd-0.8_rtw_r7475.20130812.tar.gz pi@raspberrypi ~/RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/wpa_supplicant_hostapd $ cd wpa_supplicant_hostapd-0.8_rtw_r7475.20130812/ pi@raspberrypi ~/RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/wpa_supplicant_hostapd/wpa_supplicant_hostapd-0.8_rtw_r7475.20130812 $ ls COPYING README hostapd src wpa_supplicant pi@raspberrypi ~/RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/wpa_supplicant_hostapd/wpa_supplicant_hostapd-0.8_rtw_r7475.20130812 $ cd hostapd/ pi@raspberrypi ~/RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/wpa_supplicant_hostapd/wpa_supplicant_hostapd-0.8_rtw_r7475.20130812/hostapd $ make
出来上がったバイナリを/usr/sbinにコピー。/usr/local/sbinではない。localに入れてしまうと置き換えられずに、コマンドラインから起動したら/usr/local/sbinの方が、起動スクリプトからだと/usr/localから起動してしまう。
pi@raspberrypi ~/RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911/wpa_supplicant_hostapd/wpa_supplicant_hostapd-0.8_rtw_r7475.20130812/hostapd $ sudo mv -f hostapd hostapd_cli /usr/sbin/
hostapdの設定
設定ファイルを編集。
pi@raspberrypi ~ $ nano /etc/hostapd/hostapd.conf
もちろんssidとwpa_passphraseは適宜変更で。構文チェックがかなり厳しくちょっとでも間違うとサービスが起動しないので注意。
interface=wlan0 driver=rtl871xdrv ssid=rpi2 hw_mode=g channel=6 macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 wpa_passphrase=raspberrypi wpa_key_mgmt=WPA-PSK wpa_pairwise=TKIP rsn_pairwise=CCMP
IPアドレスを固定化
アクセスポイントにする以上IPが変わると困るので固定化。
pi@raspberrypi ~ $ sudo nano /etc/network/interfaces
auto lo iface lo inet loopback iface eth0 inet dhcp auto wlan0 iface wlan0 inet static address 192.168.10.1 netmask 255.255.255.0
WPASupplicantを停止させる。
sudo mv /usr/share/dbus-1/system-services/fi.epitest.hostap.WPASupplicant.service ~/
ifplugdが作用するとwlan0のIPアドレスが飛んでしまうので、対象をeth0に限定する。
pi@raspberrypi ~ $ sudo nano /etc/default/ifplugd
変更前
INTERFACES="auto" HOTPLUG_INTERFACES="all" ARGS="-q -f -u0 -d10 -w -I" SUSPEND_ACTION="stop"
変更後
INTERFACES="eth0" HOTPLUG_INTERFACES="all" ARGS="-q -f -u0 -d10 -w -I" SUSPEND_ACTION="stop"
hostapdの起動
まずはサービスとしてでは無くコマンドラインから起動できるかの確認。
pi@raspberrypi ~ $ sudo hostapd -dd /etc/hostapd/hostapd.conf random: Trying to read entropy from /dev/random Configuration file: /etc/hostapd/hostapd.conf drv->ifindex=3 l2_sock_recv==l2_sock_xmit=0x0x1c1638 BSS count 1, BSSID mask 00:00:00:00:00:00 (0 bits) Allowed channel: mode=1 chan=1 freq=2412 MHz max_tx_power=0 dBm Allowed channel: mode=1 chan=2 freq=2417 MHz max_tx_power=0 dBm Allowed channel: mode=1 chan=3 freq=2422 MHz max_tx_power=0 dBm Allowed channel: mode=1 chan=4 freq=2427 MHz max_tx_power=0 dBm Allowed channel: mode=1 chan=5 freq=2432 MHz max_tx_power=0 dBm Allowed channel: mode=1 chan=6 freq=2437 MHz max_tx_power=0 dBm Allowed channel: mode=1 chan=7 freq=2442 MHz max_tx_power=0 dBm Allowed channel: mode=1 chan=8 freq=2447 MHz max_tx_power=0 dBm Allowed channel: mode=1 chan=9 freq=2452 MHz max_tx_power=0 dBm Allowed channel: mode=1 chan=10 freq=2457 MHz max_tx_power=0 dBm Allowed channel: mode=1 chan=11 freq=2462 MHz max_tx_power=0 dBm Allowed channel: mode=1 chan=12 freq=2467 MHz max_tx_power=0 dBm Allowed channel: mode=1 chan=13 freq=2472 MHz max_tx_power=0 dBm Allowed channel: mode=1 chan=14 freq=2484 MHz max_tx_power=0 dBm Allowed channel: mode=0 chan=1 freq=2412 MHz max_tx_power=0 dBm Allowed channel: mode=0 chan=2 freq=2417 MHz max_tx_power=0 dBm Allowed channel: mode=0 chan=3 freq=2422 MHz max_tx_power=0 dBm Allowed channel: mode=0 chan=4 freq=2427 MHz max_tx_power=0 dBm Allowed channel: mode=0 chan=5 freq=2432 MHz max_tx_power=0 dBm Allowed channel: mode=0 chan=6 freq=2437 MHz max_tx_power=0 dBm Allowed channel: mode=0 chan=7 freq=2442 MHz max_tx_power=0 dBm Allowed channel: mode=0 chan=8 freq=2447 MHz max_tx_power=0 dBm Allowed channel: mode=0 chan=9 freq=2452 MHz max_tx_power=0 dBm Allowed channel: mode=0 chan=10 freq=2457 MHz max_tx_power=0 dBm Allowed channel: mode=0 chan=11 freq=2462 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=36 freq=5180 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=40 freq=5200 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=44 freq=5220 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=48 freq=5240 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=52 freq=5260 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=56 freq=5280 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=60 freq=5300 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=64 freq=5320 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=100 freq=5500 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=104 freq=5520 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=108 freq=5540 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=112 freq=5560 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=116 freq=5580 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=120 freq=5600 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=124 freq=5620 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=128 freq=5640 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=132 freq=5660 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=136 freq=5680 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=140 freq=5700 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=149 freq=5745 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=153 freq=5765 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=157 freq=5785 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=161 freq=5805 MHz max_tx_power=0 dBm Allowed channel: mode=2 chan=165 freq=5825 MHz max_tx_power=0 dBm Completing interface initialization Mode: IEEE 802.11g Channel: 6 Frequency: 2437 MHz RATE[0] rate=10 flags=0x1 RATE[1] rate=20 flags=0x1 RATE[2] rate=55 flags=0x1 RATE[3] rate=110 flags=0x1 RATE[4] rate=60 flags=0x0 RATE[5] rate=90 flags=0x0 RATE[6] rate=120 flags=0x0 RATE[7] rate=180 flags=0x0 RATE[8] rate=240 flags=0x0 RATE[9] rate=360 flags=0x0 RATE[10] rate=480 flags=0x0 RATE[11] rate=540 flags=0x0 Flushing old station entries Deauthenticate all stations +rtl871x_sta_deauth_ops, ff:ff:ff:ff:ff:ff is deauth, reason=2 rtl871x_set_key_ops rtl871x_set_key_ops rtl871x_set_key_ops rtl871x_set_key_ops Using interface wlan0 with hwaddr 34:76:c5:5d:34:91 and ssid 'rpi2' Deriving WPA PSK based on passphrase SSID - hexdump_ascii(len=4): 72 70 69 32 rpi2 PSK (ASCII passphrase) - hexdump_ascii(len=11): [REMOVED] PSK (from passphrase) - hexdump(len=32): [REMOVED] rtl871x_set_wps_assoc_resp_ie rtl871x_set_wps_beacon_ie rtl871x_set_wps_probe_resp_ie urandom: Got 20/20 bytes from /dev/urandom Get randomness: len=32 entropy=0 GMK - hexdump(len=32): [REMOVED] Get randomness: len=32 entropy=0 Key Counter - hexdump(len=32): [REMOVED] WPA: group state machine entering state GTK_INIT (VLAN-ID 0) Get randomness: len=16 entropy=0 GTK - hexdump(len=16): [REMOVED] WPA: group state machine entering state SETKEYSDONE (VLAN-ID 0) rtl871x_set_key_ops rtl871x_set_beacon_ops rtl871x_set_hidden_ssid ignore_broadcast_ssid:0, rpi2,4 rtl871x_set_acl wlan0: Setup of interface done. Wireless event: cmd=0x8b15 len=20
wlan0: Setup of interface done.と表示されればたぶん成功している。この時点でWifiアクセスポイントとして動作しているはずなので、MacとかiPhoneのアクセスポイント一覧に現れる。
プロセスとしても動作しているはず。
pi@raspberrypi ~ $ ps ax | grep hostapd 3658 pts/1 S+ 0:00 sudo hostapd /etc/hostapd/hostapd.conf -dd 3659 pts/1 S+ 0:00 hostapd /etc/hostapd/hostapd.conf -dd 3671 pts/0 S+ 0:00 grep --color=auto hostapd
ここでkillするかctrl+cして次はサービスとして起動させるための設定。initスクリプト(/etc/init.d/hostapd)から参照されている設定ファイルを変更。
pi@raspberrypi ~ $ sudo nano /etc/default/hostapd
こんな感じ。
DAEMON_CONF="/etc/hostapd/hostapd.conf"
DHCPサーバーのインストール
インストールする、必要はなかった。既にインストールされていて、最初のコマンドでアップデートされています。
pi@raspberrypi ~ $ sudo apt-get install isc-dhcp-server Reading package lists... Done Building dependency tree Reading state information... Done isc-dhcp-server is already the newest version. 0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
DHCPサーバーの設定
動作対象のインターフェースの指定とIPの割り当て設定は別々のファイルで行います。
こちらは対象インターフェースの指定。
pi@raspberrypi ~ $ sudo nano /etc/default/isc-dhcp-server
ファイル末尾の設定を変更。
INTERFACES="wlan0"
こちらは割り当て設定。
pi@raspberrypi ~ $ sudo nano /etc/dhcp/dhcpd.conf
この部分をコメントアウト。
#option domain-name "example.org"; #option domain-name-servers ns1.example.org, ns2.example.org;
ping-check true; subnet 192.168.10.0 netmask 255.255.255.0 { option routers 192.168.10.1; option broadcast-address 192.168.10.255; option subnet-mask 255.255.255.0; option domain-name "local"; option domain-name-servers 8.8.8.8,8.8.4.4; default-lease-time 600; max-lease-time 7200; range 192.168.10.101 192.168.10.199; }
ここでWifi端末からアクセスして、設定されたIPやDNSアドレスが割り振られていれば成功
おつかれさまでした
長い。これだけの手順を間違わずに再現するのは大変。設定方法が時々変わるので更に大変。そりゃシステムイメージでセットアップしたくなりますよね。
pi@raspberrypi:~ $ sudo service hostapd start
pi@raspberrypi:~ $ sudo service hostapd stop





納得したらすぐにシェア!