树莓派最终作为独立的多功能USB设备时,可能会面对相对复杂的应用环境。为了脱离HDMI转接线,路由器,电脑等环境的限制,故添加了无线AP模式以应对特殊使用场景。 在按键开启无线AP模式以后,树莓派可作为具有路由功能的三层设备,只需手机等终端连接热点即可完成树莓派的完全控制,也可结合BadUSB功能实现无线USB功能
实现AP模式需要用到一个开源项目https://github.com/oblique/create_ap
,详细信息可在github上查看
git clone https://github.com/oblique/create_ap
cd create_ap
sudo make install
apt-get install util-linux procps hostapd iproute2 iw haveged dnsmasq
root@raspberrypi:~/init_os# create_ap -n wlan0 My_AP 12345678
WARN: brmfmac driver doesn't work properly with virtual interfaces and
it can cause kernel panic. For this reason we disallow virtual
interfaces for your adapter.
For more info: https://github.com/oblique/create_ap/issues/203
ERROR: Your adapter can not be a station (i.e. be connected) and an AP at the same time
root@raspberrypi:~/init_os#
这是由于树莓派无线适配器当前处于无线网卡模式,而其不支持同时做无线终端和AP
root@raspberrypi:~/init_os# ps aux|grep "iwlan0"
root 356 0.0 0.9 10944 4016 ? Ss 02:27 0:00 wpa_supplicant -B -c/etc/wpa_supplicant/wpa_supplicant.conf -iwlan0 -Dnl80211,wext
root 1147 0.0 0.4 7304 1792 pts/0 S+ 03:54 0:00 grep iwlan0
root@raspberrypi:~/init_os#
需kill
掉该进程后再开启AP模式,以下是该功能的实现函数。需要注意的是,在主程序调用脚本时os.popen().read()
会阻塞进程,故此处做后台运行处理,防止树莓派在开启AP模式后假死(卡在等待os.popen().read()
返回状态)
function enable_ap()
{
AP_name="SecretAP"
AP_pwd="12345678"
pid1=`ps aux|grep "create_ap -n wlan0"|grep -v grep`
pid2=`ps aux|grep "iwlan0"|grep -v grep`
if [ ! -z "$pid1" -a -z "$pid2" ];then
green_message "It is AP mode now,skip..."
return 0
fi
kill "$(ps aux|grep "iwlan0"|head -1|awk '{print $2}')" > /dev/null 2>&1
DATE=`date +"%Y%m%d%H%M%S"`
print_run "nohup create_ap -n wlan0 $AP_name $AP_pwd > /tmp/create_ap_log_${DATE}.log 2>&1 &"
}
RaspAP 是一个可以将树莓派轻松部署成无线 AP(Access Point)的软件方案,具有一套响应式的 WebUI 来控制 WiFi,用起来和家用路由器一样方便。RaspAP 可以运行在 Raspbian 上,只需要先给树莓派安装好 Raspbian 系统,再通过快速安装脚本就可以轻松完成 RaspAP 的安装和配置。
该方案易于安装,集成了webUI,但是不利于多功能USB的模块化管理。
选择 “4 AP mode”:
运行结果:
选择 “7. Re-GetIP” :
手机连接“SecretAP”后即可通过ssh客户端APP连接控制树莓派:
create_ap(https://github.com/oblique/create_ap) RaspAP:轻松实现树莓派无线AP(https://shumeipai.nxez.com/2019/09/30/raspap-webgui-installation-guide.html)