WIFI & Bluetooth
Luckfox Lyra Zero W 上搭载了AIC8800DC模块,支持Wi-Fi AX协议(WIFI6),支持 2.4GHz Wi-Fi 和蓝牙5.2/BLE。开发板默认使用外接天线。如需切换至板载贴片天线,只需将引脚 55 拉高即可。具体操作步骤如下:
echo 55 > /sys/class/gpio/export
cd /sys/class/gpio/gpio55
echo out > direction
echo 1 > value
1. Wi-Fi 测试
在无线通信中,Wi-Fi 设备通常可以运行在 AP(Access Point,接入点)模式 和 STA(Station,站点)模式 这两种基本模式。AP 模式 让设备充当无线热点,允许其他设备连接,就像家庭中的 WiFi 路由器一样。而 STA 模式 则让设备作为 WiFi 客户端,连接到已有的无线网络,例如手机或笔记本电脑连接 WiFi 时所使用的模式。
1.1 Wi-Fi 连接(STA 模式)
-
使用 wifi-connect 连接 WiFi。
wifi-connect.sh Luckfox-2.4G luckfox12345Luckfox-2.4G:无线网络名称luckfox12345:无线网络的密码- 根据自己实际无线网络名称和密码修改,其它地方不用修改。
-
连接无线网络:
udhcpc -i wlan0 -
按照上述设置方法,重启后会失效,我们需要写开机脚本来启动。(板载天线)
#!/bin/shWIFI_SSID="Luckfox-2.4G"WIFI_PASSWD="luckfox12345"GPIO_NUM=55GPIO_PATH="/sys/class/gpio/gpio${GPIO_NUM}"echo_log() {echo "[wifi-autostart] $1"}wait_for_interface() {for i in $(seq 1 5); doif ip link show wlan0 > /dev/null 2>&1; thenreturn 0fiecho_log "Waiting for wlan0... ($i)"sleep 2donereturn 1}case "$1" instart)echo_log "Exporting and setting GPIO${GPIO_NUM}..."[ ! -d "$GPIO_PATH" ] && echo "$GPIO_NUM" > /sys/class/gpio/exportecho out > "${GPIO_PATH}/direction"echo 1 > "${GPIO_PATH}/value"echo_log "Checking wlan0 interface..."if ! wait_for_interface; thenecho_log "wlan0 not found after retries, aborting."exit 1fiecho_log "Connecting to WiFi: $WIFI_SSID"if [ -x /usr/bin/wifi-connect.sh ]; then/usr/bin/wifi-connect.sh "$WIFI_SSID" "$WIFI_PASSWD"elseecho_log "wifi-connect.sh not found or not executable"fiecho_log "Requesting IP via udhcpc..."udhcpc -i wlan0echo_log "WiFi setup done.";;stop)echo_log "Stop command received. No action defined.";;*)echo "Usage: $0 {start|stop}"exit 1;;esac -
按照上述设置方法,重启后会失效,我们需要写开机脚本来启动。(外接天线)
nano /etc/init.d/S99wlan0#!/bin/shcase $1 instart)sleep 5wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant.confudhcpc -i wlan0;;stop);;*)exit 1;;esacchmod +x /etc/init.d/S99wlan0reboot
1.2 AP 模式
本节使用 hostapd 和 dnsmasq,将开发板的 wlan0 接口配置为采用 WPA2-PSK 加密的 Wi-Fi 热点。配置完成后,手机或电脑可连接该热点,并通过局域网访问开发板。
本节仅创建本地 Wi-Fi 网络,不包含通过开发板共享互联网的配置。
1.2.1 配置说明
本示例使用以下参数:
| 参数 | 配置值 | 说明 |
|---|---|---|
| 无线接口 | wlan0 | 用于创建热点的 Wi-Fi 接口 |
| SSID | Luckfox_AP_2 | 手机或电脑搜索到的热点名称 |
| Wi-Fi 密码 | luckfox88 | WPA2 预共享密钥,长度必须为 8~63 个字符 |
| AP 地址 | 10.201.126.1/24 | 开发板在 AP 局域网中的地址 |
| DHCP 地址池 | 10.201.126.50~10.201.126.150 | 分配给客户端的 IPv4 地址范围 |
可根据实际需求修改示例中的 SSID 和密码,正式使用时建议设置安全性更高的密码。
1.2.2 检查前置条件
使用 root 账号登录开发板,并确认无线接口、hostapd 和 dnsmasq 均已存在:
ip link show wlan0
command -v hostapd
command -v dnsmasq
以上命令能够找到 wlan0、hostapd 和 dnsmasq 后,即可继续配置。
1.2.3 配置 hostapd
-
备份原始配置文件:
cp -p /etc/hostapd.conf /etc/hostapd.conf.bak如需恢复原始配置,可执行:
cp -p /etc/hostapd.conf.bak /etc/hostapd.conf -
设置 SSID 和 WPA2-PSK 参数:
sed -i \-e 's/^ssid=.*/ssid=Luckfox_AP_2/' \-e 's/^auth_algs=.*/auth_algs=1/' \-e 's/^wpa_passphrase=.*/wpa_passphrase=luckfox88/' \-e 's/^wpa_key_mgmt=.*/wpa_key_mgmt=WPA-PSK/' \-e '/^rsn_pairwise=/d' \-e '/^wpa_key_mgmt=/a rsn_pairwise=CCMP' \/etc/hostapd.conf关键参数说明如下:
ssid:热点名称。auth_algs=1:使用开放系统认证,WPA2 密钥认证在后续四次握手中完成。wpa=2:启用 WPA2,该参数应已存在于原始配置中。wpa_passphrase:热点密码,长度必须为 8~63 个字符。wpa_key_mgmt=WPA-PSK:使用预共享密钥认证。rsn_pairwise=CCMP:使用 AES-CCMP 加密算法。
-
检查关键配置:
grep -nE '^(interface|ssid|hw_mode|channel|auth_algs|wpa|wpa_passphrase|wpa_key_mgmt|rsn_pairwise)=' /etc/hostapd.conf输出应包含:
interface=wlan0ssid=Luckfox_AP_2hw_mode=gchannel=1auth_algs=1wpa=2wpa_passphrase=luckfox88wpa_key_mgmt=WPA-PSKrsn_pairwise=CCMP
1.2.4 启动 Wi-Fi 热点
停止已有的 hostapd 进程,然后在后台启动:
killall hostapd 2>/dev/null
hostapd -B /etc/hostapd.conf
出现以下输出表示 AP 已启动:
wlan0: interface state UNINITIALIZED->ENABLED
wlan0: AP-ENABLED
检查运行状态:
hostapd_cli -i wlan0 status
输出应包含:
state=ENABLED
channel=1
ssid[0]=Luckfox_AP_2
1.2.5 配置 AP 地址
为 wlan0 设置示例网关地址:
ip addr add 10.201.126.1/24 dev wlan0
检查地址配置:
ip -4 addr show dev wlan0
输出应包含:
inet 10.201.126.1/24 scope global wlan0
如果执行 ip addr add 时提示 RTNETLINK answers: File exists,表示该地址已经存在,无需重复添加。
1.2.6 配置并启动 DHCP 服务
确认 /etc/dnsmasq.conf 至少包含以下配置:
listen-address=10.201.126.1
dhcp-range=10.201.126.50,10.201.126.150
listen-address 必须与 wlan0 的 AP 地址一致。dnsmasq 默认会将开发板在该子网中的地址作为客户端的默认网关。
重启 dnsmasq:
killall dnsmasq 2>/dev/null
dnsmasq --pid-file=/var/run/dnsmasq.pid
检查 DNS 和 DHCP 监听端口:
ss -lunp
正常情况下,dnsmasq 应监听 UDP 端口 53(DNS)和 UDP 端口 67(DHCP)。
自定义 AP 网段
AP 网段不要求使用 10.201.126.0/24。修改网段时,必须同时调整 wlan0 地址和 /etc/dnsmasq.conf。例如,改用 192.168.50.0/24:
ip addr del 10.201.126.1/24 dev wlan0
ip addr add 192.168.50.1/24 dev wlan0
然后修改 /etc/dnsmasq.conf:
listen-address=192.168.50.1
dhcp-range=192.168.50.50,192.168.50.150,255.255.255.0,12h
dhcp-option=3,192.168.50.1
dhcp-option=3 用于显式指定客户端的默认网关;如果使用开发板在该子网中的地址作为网关,也可以省略此项。所选网段不能与 eth0、usb0 或上级路由器使用的网段重叠。修改完成后,需要重启 dnsmasq。
1.2.7 连接并验证
在手机或电脑上搜索并连接以下热点:
Wi-Fi 名称:Luckfox_AP_2
Wi-Fi 密码:luckfox88
连接成功后,客户端应获得 10.201.126.50~10.201.126.150 范围内的地址。可在客户端测试与开发板的连通性:
ping 10.201.126.1
在开发板上执行以下命令,可查看已连接的客户端:
hostapd_cli -i wlan0 all_sta
1.2.8 常见问题
hostapd提示密码长度无效:wpa_passphrase必须包含 8~63 个字符。修改为符合长度要求的密码后,重新启动hostapd。- 手机提示密码错误或无法连接: 如果日志中出现
AP-STA-POSSIBLE-PSK-MISMATCH,请在手机上忽略已保存的网络,然后重新输入当前密码。修改 SSID 也可以避免手机继续使用旧的 Wi-Fi 配置。 - 客户端一直停留在“正在获取 IP 地址”: 依次执行
ip -4 addr show dev wlan0、ps | grep '[d]nsmasq'和ss -lunp,确认wlan0已配置10.201.126.1/24,并且dnsmasq正在监听 UDP 端口67。 - 可以连接热点但无法访问互联网: 这是正常现象。如需共享互联网,还要配置 IPv4 转发、防火墙转发规则和 NAT,不在本节范围内。
1.2.9 重启后的配置
对 /etc/hostapd.conf 和 /etc/dnsmasq.conf 的修改会保留,但以下运行时操作需要在开发板重启后重新执行:
hostapd -B /etc/hostapd.conf
ip addr add 10.201.126.1/24 dev wlan0
dnsmasq --pid-file=/var/run/dnsmasq.pid
如需开机自动启用 AP 模式,应将接口地址以及 hostapd、dnsmasq 的启动命令加入系统启动脚本。
1.2.10 命令汇总
首次配置时执行:
cp -p /etc/hostapd.conf /etc/hostapd.conf.bak
sed -i \
-e 's/^ssid=.*/ssid=Luckfox_AP_2/' \
-e 's/^auth_algs=.*/auth_algs=1/' \
-e 's/^wpa_passphrase=.*/wpa_passphrase=luckfox88/' \
-e 's/^wpa_key_mgmt=.*/wpa_key_mgmt=WPA-PSK/' \
-e '/^rsn_pairwise=/d' \
-e '/^wpa_key_mgmt=/a rsn_pairwise=CCMP' \
/etc/hostapd.conf
killall hostapd 2>/dev/null
hostapd -B /etc/hostapd.conf
ip addr add 10.201.126.1/24 dev wlan0
killall dnsmasq 2>/dev/null
dnsmasq --pid-file=/var/run/dnsmasq.pid
后续重新启动 AP 模式时执行:
killall hostapd 2>/dev/null
hostapd -B /etc/hostapd.conf
ip addr add 10.201.126.1/24 dev wlan0
killall dnsmasq 2>/dev/null
dnsmasq --pid-file=/var/run/dnsmasq.pid
1.3 WiFi 速率测试
-
虚拟机或者主机端
iperf3 -s -i 10 -p 5001-s:指定 iperf3 运行在服务器模式-i:设置了报告间隔的时间为 10 秒-p:这个参数指定服务器端口为5001
-
开发板
iperf3 -c 192.168.10.176 -p 5001 -f m -i 2 -t 24-c:指定客户端模式,并设置要连接的服务器IP地址为192.168.10.176-p:指定服务器端口为5001-f:指定报告的格式。m代表 Mbps,即报告的带宽单位为兆比特每秒-i:指定报告的间隔时间为每1秒-t:指定测试的持续时间为30秒
2. 蓝牙测试
在系统默认配置中,Wi-Fi 模块与蓝牙功能采用低功耗协同工作模式(BLE),当需要连接传统蓝牙设备(如音箱)时,必须手动切换蓝牙工作模式。
-
在
/usr/bin/wifibt-util.sh中注释掉如下内容:# Aicsemi AIC8800DC a69c:88dc aic8800_fdrv.ko -
重启开发板。
reboot -
启动蓝牙。
root@luckfox:~# bluetoothctlhci0 new_settings: powered bondable ssp br/edr le secure-connAgent registered[CHG] Controller 38:54:39:03:10:70 Powered: yes[CHG] Controller 38:54:39:03:10:70 Pairable: yes[bluetooth]# power on #打开蓝牙[bluetooth]# scan on #扫描蓝牙设备[bluetooth]# trust 12:11:32:DE:A3:03 #信任蓝牙设备号[bluetooth]# pair 12:11:32:DE:A3:03[bluetooth]# connect 12:11:32:DE:A3:03 #连接蓝牙[M1]# exit