LoginSignup
0
1

More than 3 years have passed since last update.

Raspberry Piとhostapdでローカルサーバに接続する

Last updated at Posted at 2020-11-22

やりたいこと

Raspberry Pi内臓のWi-Fiをアクセスポイントとして使い、他のデバイス(スマホやPC)で接続することでRaspberry Piで構築したサーバに接続する。

使ったもの

  • Raspberry Pi3 Model B
    • 3B以上のモデルだとWi-Fiを内蔵しているのでこれだけでいい。

やり方

インストール(hostapd, dnsmasq, Node.js)

Node.jsが入ってるかを確認

$ node -v

入ってなかったら、入れる。
こちらの記事を参考にされると良いと思います。
ラズベリーパイ4にNode.jsをインストールするまで

$ sudo apt-get install hostapd dnsmasq

hostapdの設定

/etc/hostapd/hostapd.confを以下の内容で生成。
ssidとwpa_passphraseは自由に設定。

/etc/hostapd/hostapd.conf
interface=wlan0
driver=nl80211
ssid=test-hostapd
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=test1234
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

この設定を読み出すように、/etc/default/hostapdの以下を変更。

DAEMON_CONF="/etc/hostapd/hostapd.conf"

DHCPの設定

isc-dhcp-serverでも同じことができるようだが、うまくいかなかったのでdnsmasqを使用。
/etc/dnsmasq.confに以下を追記。

/etc/dnsmasq.conf
interface=wlan0
dhcp-range=192.168.2.2,192.168.2.50,255.255.255.0,24h

ネットワーク設定

/etc/dhcpcd.confに以下を追記。
IPアドレスを固定。

/etc/dhcpcd.conf
interface wlan0
    static ip_address=192.168.2.1/24
    nohook wpa_supplicant

起動

$ sudo systemctl restart dhcpcd
$ sudo systemctl start dnsmasq
$ sudo systemctl enable dnsmasq
$ sudo systemctl unmask hostapd
$ sudo systemctl enable hostapd
$ sudo systemctl start hostapd

再起動。

$ sudo reboot

簡単なWebサーバを構築

以下のソースでtest.jsを生成。

test.js
var http = require('http');
var server = http.createServer();

server.on('request', function(req, res) {
    res.writeHead(200, {'Content-Type' : 'text/plain'});
    res.write('hello world');
    res.end();
});

server.listen(3000, '0.0.0.0');

以下で実行。

$ node test.js

接続

PC等で自分でつけたssid(例:test-hostapd)のWi-Fiに接続。
http://192.168.2.1:3000 にアクセスし、hello Worldと出てくれば成功。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1