天天看點

廢舊闆子再利用:搭建無線調試環境!

前言

大家好,我是雜燴君。

本篇博文我們來分享嵌入式Linux裝置開啟無線AP/無線接入點(Wireless Access Point)的方法。

什麼情況下會用到無線AP?

我最近的工作中有如下兩種情況需要用到:

(1)AP配網。裝置熱點配網,智能硬體處于AP模式,手機作為STA連接配接到處于AP模式的智能硬體後組成區域網路。此時,手機就可以通過區域網路把裝置即将連接配接的路由的ssid和pwd資訊至智能硬體,智能硬體接收後,連接配接路由器,完成配網。

(2)把廢舊不用的闆子作為開啟無線AP組建各裝置的區域網路通信。對于移動機器人的開發來說,裝置實際工作過程中,無線調試無疑是最友善的。

因為裝置一直處于運動狀态,如果接着有線,電腦需要跟着裝置跑,很不友善。因為我們調試時,對路由器的需求比較大,而路由器比較有限,是以我把廢舊不用的闆子配成了無線AP模式。

經過實測,相同距離,舊闆子區域網路通信速度略低于我們路由器,但不影響我們作為調試時使用。

嵌入式Linux裝置,要開啟無線接入點需要準備如下四個檔案:

  • hostapd:一個使用者态用于AP和認證伺服器的守護程序。
  • hostapd.conf:hostapd配置檔案,包含無線AP的名稱、密碼等資訊。
  • udhcpd:dhcp撥号的伺服器端。
  • udhcpd.conf:udhcpd配置檔案,配置網關位址及IP位址的範圍。

其中,hostapd、udhcpd工具busybox中包含有。當然,也可以自己下載下傳源碼進行編譯,方法可參照我們往期的博文:

RTL8723驅動移植+wpa_supplicant移植+SSH移植,編譯方法都是大同小異的。

注意區分:udhcpc、udhcpd工具。

  • udhcpc是dhcp撥号的用戶端。裝置作為STA時,用于自動擷取IP。
  • udhcpd是dhcp撥号的伺服器端。裝置作為AP時,用于自動配置設定IP。

其中,我們的往期博文如何實作程式開機自啟動?中有用到udhcpc,本博文中我們用的是udhcpd。

下面我們來看hostapd及udhcpd的配置檔案如何配置:

hostapd配置檔案

hostapd的配置檔案可參考hostapd源碼下的hostapd.conf:

廢舊闆子再利用:搭建無線調試環境!

裡面的内容很多,實際中我們可能用不到那麼多配置,我們可以删減、修改,隻保留我們所需的配置。

我們删減修改之後得到:

左右滑動檢視全部代碼>>>

# AP netdevice name
interface=wlan0

# SSID to be used in IEEE 802.11 management frames
ssid=LinuxZn_AP

# Driver interface type (hostap/wired/none/nl80211/bsd);
# default: hostap). nl80211 is used with all Linux mac80211 drivers.
# Use driver=none if building hostapd as a standalone RADIUS server that does
# not control any wireless/wired driver.
driver=nl80211

# Interface for separate control program.
# /var/run/hostapd is the recommended directory for sockets and by default,
# hostapd_cli will use it when trying to connect with hostapd.
ctrl_interface=/var/run/hostapd

# Channel number (IEEE 802.11)
channel=5

# ieee80211n: Whether IEEE 802.11n (HT) is enabled
# 0 = disabled (default)
# 1 = enabled
# Note: You will also need to enable WMM for full HT functionality.
# Note: hw_mode=g (2.4 GHz) and hw_mode=a (5 GHz) is used to specify the band.
ieee80211n=1
hw_mode=g

# Send empty SSID in beacons and ignore probe request frames that do not
# specify full SSID, i.e., require stations to know SSID.
# default: disabled (0)
# 1 = send empty (length=0) SSID in beacon and ignore probe request for
#     broadcast SSID
# 2 = clear SSID (ASCII 0), but keep the original length (this may be required
#     with some clients that do not support empty SSID) and ignore probe
#     requests for broadcast SSID
ignore_broadcast_ssid=0

# WPA/IEEE 802.11i configuration
wpa=2
wpa_passphrase=12345678
wpa_key_mgmt=WPA-PSK
rsn_pairwise=CCMP      

該檔案主要配置了:

  • 所用網卡:wlan0
  • AP名稱:LinuxZn_AP
  • AP密碼:12345678
  • 加密:WPA2
  • 頻段:2.4GHz

我們把hostapd.conf配置檔案我們放到闆子上的/etc目錄下備用:

廢舊闆子再利用:搭建無線調試環境!

udhcpd配置檔案

udhcpd的配置檔案可參考udhcpd源碼下的udhcpd.conf:

廢舊闆子再利用:搭建無線調試環境!

同樣的,我們隻保留如下内容:

左右滑動檢視全部代碼>>>

# The start and end of the IP lease block
start           192.168.3.2
end             192.168.3.254

# The interface that udhcpd will use
interface       wlan0

opt     dns     114.114.114.114
option  subnet  255.255.255.0
opt     router  192.168.3.1
option  domain  local
option  lease   864000     # 10 days of seconds      

該檔案主要配置了:

  • 所能配置設定的IP位址的範圍為:192.168.3.2~192.168.3.254
  • 網卡接口:wlan0
  • 網關位址:192.168.3.1

我們把udhcpd.conf配置檔案放到闆子上的/etc目錄下備用:

廢舊闆子再利用:搭建無線調試環境!

開啟熱點

有了以上工具及相關配置檔案之後,還需要進行一些操作,才可以開啟我們的熱點,我們把這些操作寫成腳本:

start_ap.sh:

左右滑動檢視全部代碼>>>

#!/bin/bash

# 殺掉網卡操作相關的程序
killall wpa_supplicant udhcpc dhcpcd dnsmasq udhcpd hostapd > /dev/null 2>&1

# 禁用網卡
ifconfig wlan0 down

# 啟用網卡
ifconfig wlan0 up

# 給無線網卡設定IP位址(網關位址)
ifconfig wlan0 192.168.3.1

# 啟動DHCP
udhcpd /etc/udhcpd.conf 

# 啟動熱點
hostapd /etc/hostapd.conf -B      

開啟熱點:

廢舊闆子再利用:搭建無線調試環境!

連接配接測試: