You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Arduino IDE中实现ESP32 WiFi中继器/扩展器?

在Arduino IDE中实现ESP32 WiFi中继器/扩展器

ESP32在Arduino环境下可以通过同时开启STA(连接主WiFi)和AP(发射热点)模式,再借助底层ESP-IDF的NAPT功能实现网络转发,不用手动修改IDF配置文件,直接在代码里调用API即可。以下是完整的实现方案:

步骤1:确认开发环境

  • 确保Arduino IDE已安装ESP32开发板支持包(版本建议在2.0.0以上,避免兼容性问题)
  • 选择对应的ESP32开发板型号(比如ESP32 Dev Module)

步骤2:完整实现代码

#include <WiFi.h>
#include <esp_wifi.h>
#include <esp_netif.h>
#include <esp_event.h>
#include <nvs_flash.h>

// 主WiFi信息
#define MAIN_WIFI_SSID "你的主WiFi名称"
#define MAIN_WIFI_PASS "你的主WiFi密码"
// 中继热点信息
#define AP_SSID "ESP32-Repeater"
#define AP_PASS "12345678" // 密码至少8位

esp_netif_t *sta_netif = nullptr;
esp_netif_t *ap_netif = nullptr;

void wifi_event_handler(void* arg, esp_event_base_t event_base,
                        int32_t event_id, void* event_data) {
    if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) {
        Serial.println("已连接主WiFi");
    } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
        ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
        Serial.print("STA获得IP: ");
        Serial.println(ip4addr_ntoa(&event->ip_info.ip));
        
        // 开启NAPT转发
        esp_netif_napt_enable(sta_netif, true);
        esp_netif_napt_enable(ap_netif, true);
        Serial.println("NAPT转发已开启");
    } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
        Serial.println("与主WiFi断开,尝试重连...");
        WiFi.begin(MAIN_WIFI_SSID, MAIN_WIFI_PASS);
    }
}

void setup() {
    Serial.begin(115200);
    delay(1000);

    // 初始化NVS
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    // 初始化TCP/IP栈
    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    // 配置STA接口
    sta_netif = esp_netif_create_default_wifi_sta();
    // 配置AP接口(设置静态IP避免冲突)
    esp_netif_ip_info_t ap_ip_info;
    IPAddress ap_ip(192, 168, 4, 1);
    IPAddress ap_gateway(192, 168, 4, 1);
    IPAddress ap_subnet(255, 255, 255, 0);
    memcpy(&ap_ip_info.ip, &ap_ip, sizeof(ap_ip));
    memcpy(&ap_ip_info.gw, &ap_gateway, sizeof(ap_gateway));
    memcpy(&ap_ip_info.netmask, &ap_subnet, sizeof(ap_subnet));
    ap_netif = esp_netif_create_default_wifi_ap();
    esp_netif_set_ip_info(ap_netif, &ap_ip_info);

    // 配置WiFi模式为STA+AP
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));

    // 配置STA参数
    wifi_config_t sta_config = {};
    strncpy((char*)sta_config.sta.ssid, MAIN_WIFI_SSID, sizeof(sta_config.sta.ssid) - 1);
    strncpy((char*)sta_config.sta.password, MAIN_WIFI_PASS, sizeof(sta_config.sta.password) - 1);
    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &sta_config));

    // 配置AP参数
    wifi_config_t ap_config = {};
    strncpy((char*)ap_config.ap.ssid, AP_SSID, sizeof(ap_config.ap.ssid) - 1);
    strncpy((char*)ap_config.ap.password, AP_PASS, sizeof(ap_config.ap.password) - 1);
    ap_config.ap.max_connection = 5; // 最多连接5个设备
    ap_config.ap.authmode = WIFI_AUTH_WPA2_PSK;
    // 强制AP与STA信道一致,减少干扰
    esp_wifi_get_config(WIFI_IF_STA, &sta_config);
    ap_config.ap.channel = sta_config.sta.channel;
    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_config));

    // 注册WiFi事件回调
    ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, nullptr));
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, nullptr));

    // 启动WiFi
    ESP_ERROR_CHECK(esp_wifi_start());
    Serial.println("WiFi中继器启动中...");
}

void loop() {
    // 无需额外操作,事件回调处理重连等逻辑
    delay(1000);
}

关键代码说明

  • STA+AP模式配置:通过esp_wifi_set_mode(WIFI_MODE_APSTA)让ESP32同时作为客户端连接主WiFi,又作为热点发射信号。
  • NAPT转发开启:在STA获取到IP后,调用esp_netif_napt_enable分别开启STA和AP接口的NAPT功能,实现网络地址转换,让连接AP的设备能通过STA访问外网。
  • 信道同步:强制AP信道和STA连接的主WiFi信道相同,减少WiFi干扰,提升稳定性。
  • 自动重连:当STA断开主WiFi时,自动触发重连逻辑。

注意事项

  • 主WiFi和中继热点的信道会自动同步,无需手动设置。
  • 中继热点密码必须至少8位,否则AP无法正常启动。
  • 避免ESP32所在位置离主WiFi过远,否则中继信号强度和稳定性会受影响。
  • 测试时,用手机或电脑连接ESP32-Repeater热点,检查能否正常访问外网。

内容的提问来源于stack exchange,提问作者someone idk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 02:22:43