跳到主要内容

WIFI & Bluetooth

Luckfox Lyra Pi 上搭载了AIC8800DC模块,支持Wi-Fi AX协议(WIFI6),支持 2.4GHz Wi-Fi 和蓝牙5.2/BLE。开发板 WiFi 使用需要外接天线。

1. Wi-Fi 测试

在无线通信中,Wi-Fi 设备通常可以运行在 AP(Access Point,接入点)模式STA(Station,站点)模式 这两种基本模式。AP 模式 让设备充当无线热点,允许其他设备连接,就像家庭中的 WiFi 路由器一样。而 STA 模式 则让设备作为 WiFi 客户端,连接到已有的无线网络,例如手机或笔记本电脑连接 WiFi 时所使用的模式。

1.1 Wi-Fi 连接(STA 模式)

  1. 使用 wifi-connect 连接 WiFi。

    wifi-connect.sh Luckfox-2.4G luckfox12345
    • Luckfox-2.4G:无线网络名称
    • luckfox12345:无线网络的密码
    • 根据自己实际无线网络名称和密码修改,其它地方不用修改。
  2. 连接无线网络:

    udhcpc -i wlan0
  3. 按照上述设置方法,重启后会失效,我们需要写开机脚本来启动。(外接天线)

    nano /etc/init.d/S99wlan0
    #!/bin/sh

    WIFI_SSID="Luckfox-2.4G"
    WIFI_PASSWD="luckfox12345"


    echo_log() {
    echo "[wifi-autostart] $1"
    }

    wait_for_interface() {
    for i in $(seq 1 5); do
    if ip link show wlan0 > /dev/null 2>&1; then
    return 0
    fi
    echo_log "Waiting for wlan0... ($i)"
    sleep 2
    done
    return 1
    }

    case "$1" in
    start)

    echo_log "Checking wlan0 interface..."
    if ! wait_for_interface; then
    echo_log "wlan0 not found after retries, aborting."
    exit 1
    fi

    echo_log "Connecting to WiFi: $WIFI_SSID"
    if [ -x /usr/bin/wifi-connect.sh ]; then
    /usr/bin/wifi-connect.sh "$WIFI_SSID" "$WIFI_PASSWD"
    else
    echo_log "wifi-connect.sh not found or not executable"
    fi

    echo_log "Requesting IP via udhcpc..."
    udhcpc -i wlan0

    echo_log "WiFi setup done."
    ;;
    stop)
    echo_log "Stop command received. No action defined."
    ;;
    *)
    echo "Usage: $0 {start|stop}"
    exit 1
    ;;
    esac
    chmod +x /etc/init.d/S99wlan0
    reboot

1.2 AP 模式

本节使用 hostapddnsmasq,将开发板的 wlan0 接口配置为采用 WPA2-PSK 加密的 Wi-Fi 热点。配置完成后,手机或电脑可连接该热点,并通过局域网访问开发板。

备注

本节仅创建本地 Wi-Fi 网络,不包含通过开发板共享互联网的配置。

1.2.1 配置说明

本示例使用以下参数:

参数配置值说明
无线接口wlan0用于创建热点的 Wi-Fi 接口
SSIDLuckfox_AP_2手机或电脑搜索到的热点名称
Wi-Fi 密码luckfox88WPA2 预共享密钥,长度必须为 8~63 个字符
AP 地址10.201.126.1/24开发板在 AP 局域网中的地址
DHCP 地址池10.201.126.5010.201.126.150分配给客户端的 IPv4 地址范围

可根据实际需求修改示例中的 SSID 和密码,正式使用时建议设置安全性更高的密码。

1.2.2 检查前置条件

使用 root 账号登录开发板,并确认无线接口、hostapddnsmasq 均已存在:

ip link show wlan0
command -v hostapd
command -v dnsmasq

以上命令能够找到 wlan0hostapddnsmasq 后,即可继续配置。

1.2.3 配置 hostapd

  1. 备份原始配置文件:

    cp -p /etc/hostapd.conf /etc/hostapd.conf.bak

    如需恢复原始配置,可执行:

    cp -p /etc/hostapd.conf.bak /etc/hostapd.conf
  2. 设置 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 加密算法。
  3. 检查关键配置:

    grep -nE '^(interface|ssid|hw_mode|channel|auth_algs|wpa|wpa_passphrase|wpa_key_mgmt|rsn_pairwise)=' /etc/hostapd.conf

    输出应包含:

    interface=wlan0
    ssid=Luckfox_AP_2
    hw_mode=g
    channel=1
    auth_algs=1
    wpa=2
    wpa_passphrase=luckfox88
    wpa_key_mgmt=WPA-PSK
    rsn_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 用于显式指定客户端的默认网关;如果使用开发板在该子网中的地址作为网关,也可以省略此项。所选网段不能与 eth0usb0 或上级路由器使用的网段重叠。修改完成后,需要重启 dnsmasq

1.2.7 连接并验证

在手机或电脑上搜索并连接以下热点:

Wi-Fi 名称:Luckfox_AP_2
Wi-Fi 密码:luckfox88

连接成功后,客户端应获得 10.201.126.5010.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 wlan0ps | 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 模式,应将接口地址以及 hostapddnsmasq 的启动命令加入系统启动脚本。

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 速率测试

  1. 虚拟机或者主机端

    iperf3 -s -i 10 -p 5001
    • -s:指定 iperf3 运行在服务器模式
    • -i:设置了报告间隔的时间为 10 秒
    • -p:这个参数指定服务器端口为5001
  2. 开发板

    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),当需要连接传统蓝牙设备(如音箱)时,必须手动切换蓝牙工作模式。

  1. /usr/bin/wifibt-util.sh中注释掉如下内容:

    # Aicsemi AIC8800DC a69c:88dc aic8800_fdrv.ko
  2. 重启开发板。

    reboot
  3. 启动蓝牙。

    root@luckfox:~# bluetoothctl
    hci0 new_settings: powered bondable ssp br/edr le secure-conn
    Agent 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