中继
WiFi STA + SoftAP 双模桥接
学习目标
- 理解 WiFi 中继的工作原理——STA 连上级路由器 + SoftAP 给下游设备,在两个接口间转发数据
- 掌握 STA + SoftAP 共存的配置方法和信道约束
- 能够在 WS63 上实现 WiFi 中继,扩展网络覆盖范围
基本概念
中继的数据转发模型
flowchart LR
subgraph 上游
R[上级路由器]
end
subgraph WS63中继
S[STA 接口<br/>连上级路由器]
A[SoftAP 接口<br/>创建热点]
S <-->|IP Forward| A
end
subgraph 下游
P[手机/传感器]
end
R <-->|WiFi| S
A <-->|WiFi| P
上行:下游设备 → SoftAP → WS63 转发 → STA → 上级路由器 → 互联网
下行:互联网 → 上级路由器 → STA → WS63 转发 → SoftAP → 下游设备
信道约束
WS63 只有一套射频——STA 和 SoftAP 必须在同一信道。STA 连接路由器后 SoftAP 自动对齐信道。
典型使用场景
| 场景 | 说明 |
|---|---|
| 家庭网络扩展 | 路由器客厅,卧室信号弱 → WS63 中继放在中间 |
| 工业现场覆盖 | 大车间远端设备信号差 → 多级中继链式覆盖 |
| 智能家居网关 | WS63 同时上网 + 创建本地热点给传感器 |
SoftAP 连接限制
WS63 SoftAP 最多支持 4~5 个 STA 同时连接——中继场景下这些设备共享 STA 上行带宽。
涉及 API
| API | 用途 |
|---|---|
wifi_sta_enable() |
使能 STA 模式 |
wifi_sta_connect(&config) |
STA 连接上级路由器 |
wifi_softap_enable(&config) |
使能 SoftAP 模式 |
wifi_register_event_cb(&event) |
监听 STA/SoftAP 连接断开事件 |
数据转发由 lwIP 的
IP_FORWARD或应用层桥接实现。
案例说明
做什么
WS63 STA 连接上级路由器 → 获取 IP → 使能 SoftAP → 手机连接 SoftAP → 手机通过 WS63 中继访问互联网。
规格与功能
| 规格项 | 说明 |
|---|---|
| STA SSID | 上级路由器 SSID(Kconfig 可配) |
| SoftAP SSID | WS63-Repeater |
| SoftAP 信道 | 与 STA 信道自动对齐 |
| 最大下游设备 | 4~5 个 |
| 转发机制 | lwIP IP Forward |
程序运行流程:STA 初始化 → 连接路由器 → 获取信道 → SoftAP 同信道启动 → lwIP 自动转发。
案例流程
sequenceDiagram
participant R as 路由器
participant W as WS63
participant P as 手机
W->>R: wifi_sta_connect
R-->>W: 连接成功 + DHCP IP
Note over W: 获取 STA 信道=6
W->>W: wifi_softap_enable(channel=6)
Note over W: SoftAP 启动: WS63-Repeater
P->>W: 连接 SoftAP
W->>P: DHCP 分配 IP (由路由器分配)
P->>W: ping baidu.com
W->>R: 转发 ICMP
R-->>W: 回复
W-->>P: 转发回复
案例操作指导
第一步:编译
Kconfig 启用 WiFi STA + SoftAP + CONFIG_LWIP_IP_FORWARD。
第二步:烧录并运行
放置在路由器和手机之间。
第三步:验证
- 手机连接 WS63 SoftAP:IP 由上级路由器 DHCP 分配
ping baidu.com成功- WS63 移出路由器范围:外网不可达,但局域网仍可互通
关键配置
| 参数 | 推荐值 | 说明 |
|---|---|---|
| SoftAP 信道 | 自动对齐 STA | 不可手动设不同信道 |
| IP Forward | CONFIG_LWIP_IP_FORWARD=y |
lwIP 自动桥接两个 netif |
| SoftAP SSID | WS63-Repeater |
建议与上级路由器不同名 |
| SoftAP 最大连接数 | 4~5 | WS63 硬件限制 |
代码详解
STA 连接与信道获取
/* STA 连接路由器 */
wifi_sta_config_t sta_cfg = {
.ssid = CONFIG_STA_SSID,
.password = CONFIG_STA_PASSWORD,
.security = WIFI_SEC_TYPE_WPA2_PSK
};
wifi_sta_connect(&sta_cfg);
/* 连接成功回调中获取信道 */
static void wifi_event_cb(wifi_event_t event, void *data) {
if (event == WIFI_EVENT_CONNECTION_CHANGED) {
wifi_linked_info_t info;
wifi_sta_get_ap_info(&info);
int sta_channel = info.channel;
/* 同信道启动 SoftAP */
start_softap(sta_channel);
}
}
SoftAP 同信道启动
void start_softap(int channel) {
softap_config_t ap_cfg = {
.ssid = "WS63-Repeater",
.channel = channel, // 必须与 STA 同信道
.security = WIFI_SEC_TYPE_OPEN,
.max_conn = 4
};
wifi_softap_enable(&ap_cfg);
}
STA 断线处理
if (event == WIFI_EVENT_DISCONNECTED) {
/* STA 断开——SoftAP 继续运行(下游局域网互通)
但外网不可达,STA 自动重连后恢复 */
printf("STA lost, repeater still running (LAN only)\n");
}
状态监控
/* 定期打印中继状态 */
wifi_linked_info_t sta_info;
wifi_sta_get_ap_info(&sta_info);
printf("Uplink RSSI: %d dBm\n", sta_info.rssi);
wifi_sta_info_t clients[5];
int num = 5;
wifi_softap_get_sta_list(clients, &num);
printf("Downstream clients: %d\n", num);