5

我正在尝试将 ESP8266 连接到由另一个 ESP8266 托管的 WiFi 网络。问题是 ESP8266 在 WiFi 扫描期间显示 WiFi 网络,但无法连接到它并出现错误:no espnetwork found, reconnect after 1s.

托管网络的 ESP8266 的代码:

#include <osapi.h>
#include <user_interface.h>

void ICACHE_FLASH_ATTR user_init(void) {
    // Delays 1 second for my serial monitor to catch up
    for (int i = 0; i < 200; i++) os_delay_us(5000);

    gpio_init();
    uart_init(115200, 115200);

    wifi_softap_dhcps_stop();

    wifi_set_opmode(SOFTAP_MODE);

    struct softap_config softAPConfig = {
        .ssid = {0},
        .password = {0},
        .ssid_len = sizeof("espnetwork"),
        .authmode = AUTH_OPEN,
        .max_connection = 4,
        .beacon_interval = 100,
    };

    os_memcpy(softAPConfig.ssid, "espnetwork", sizeof("espnetwork"));

    wifi_softap_set_config(&softAPConfig);

    wifi_softap_dhcps_start();
}

uint32 ICACHE_FLASH_ATTR user_rf_cal_sector_set(void) {
    enum flash_size_map size_map = system_get_flash_size_map();
    uint32 rf_cal_sec = 0;

    switch (size_map) {
        case FLASH_SIZE_4M_MAP_256_256:
            rf_cal_sec = 128 - 5;
            break;

        case FLASH_SIZE_8M_MAP_512_512:
            rf_cal_sec = 256 - 5;
            break;

        case FLASH_SIZE_16M_MAP_512_512:
        case FLASH_SIZE_16M_MAP_1024_1024:
            rf_cal_sec = 512 - 5;
            break;

        case FLASH_SIZE_32M_MAP_512_512:
        case FLASH_SIZE_32M_MAP_1024_1024:
            rf_cal_sec = 1024 - 5;
            break;

        case FLASH_SIZE_64M_MAP_1024_1024:
            rf_cal_sec = 2048 - 5;
            break;
        case FLASH_SIZE_128M_MAP_1024_1024:
            rf_cal_sec = 4096 - 5;
            break;
        default:
            rf_cal_sec = 0;
            break;
    }
    return rf_cal_sec;
}

ESP8266 搜索和连接网络的代码:

#include <osapi.h>
#include <user_interface.h>

static os_timer_t INIT_TIMER;

void ICACHE_FLASH_ATTR commScanCb(void *arg, STATUS status) {
    if (status != OK) return (void)os_printf("Scan Callback Error : %d", status);

    struct bss_info *scanInfo = (struct bss_info *)arg;
    struct bss_info *network  = NULL;

    while (scanInfo != NULL) {
        if (!os_strcmp(scanInfo->ssid, "espnetwork")) network = scanInfo;

        scanInfo = STAILQ_NEXT(scanInfo, next);
    }

    if (network == NULL) return (void)os_printf("Network Not Found");
    else os_printf("Found Network : %s\n", network->ssid);

    struct station_config config = { .bssid_set = 0 };

    os_memset(config.ssid, 0, 32);
    os_memcpy(config.ssid, network->ssid, 32);

    wifi_station_set_config(&config);

    wifi_station_connect() ? os_printf("WiFi Connect Started\n") : os_printf("WiFi Connect Failed\n");
}

void ICACHE_FLASH_ATTR afterInit(void *arg) {
    os_timer_disarm(&INIT_TIMER);

    wifi_station_scan(NULL, commScanCb);
}

void ICACHE_FLASH_ATTR user_init(void) {
    // Delays 1 second for my serial monitor to catch up
    for (int i = 0; i < 200; i++) os_delay_us(5000);

    gpio_init();
    uart_init(115200, 115200);
    
    wifi_station_set_auto_connect(0);

    wifi_set_opmode(STATION_MODE);

    os_timer_disarm(&INIT_TIMER);
    os_timer_setfn(&INIT_TIMER, (os_timer_func_t *)afterInit, NULL);
    os_timer_arm(&INIT_TIMER, 1000, 1);
}

uint32 ICACHE_FLASH_ATTR user_rf_cal_sector_set(void) {
    enum flash_size_map size_map = system_get_flash_size_map();
    uint32 rf_cal_sec = 0;

    switch (size_map) {
        case FLASH_SIZE_4M_MAP_256_256:
            rf_cal_sec = 128 - 5;
            break;

        case FLASH_SIZE_8M_MAP_512_512:
            rf_cal_sec = 256 - 5;
            break;

        case FLASH_SIZE_16M_MAP_512_512:
        case FLASH_SIZE_16M_MAP_1024_1024:
            rf_cal_sec = 512 - 5;
            break;

        case FLASH_SIZE_32M_MAP_512_512:
        case FLASH_SIZE_32M_MAP_1024_1024:
            rf_cal_sec = 1024 - 5;
            break;

        case FLASH_SIZE_64M_MAP_1024_1024:
            rf_cal_sec = 2048 - 5;
            break;
        case FLASH_SIZE_128M_MAP_1024_1024:
            rf_cal_sec = 4096 - 5;
            break;
        default:
            rf_cal_sec = 0;
            break;
    }
    return rf_cal_sec;
}

从 ESP8266 尝试连接到网络的日志:

mode : sta(5c:cf:7f:f6:54:cb)
add if0
scandone
Found Network : espnetwork
WiFi Connect Started
scandone
no espnetwork found, reconnect after 1s
reconnect
scandone
no espnetwork found, reconnect after 1s
reconnect
scandone
no espnetwork found, reconnect after 1s
reconnect

此日志显示 ESP8266 找到了网络(第 4 行)但随后无法连接,然后无限期地尝试重新连接。

我尝试更改 WiFi 网络的身份验证模式并给它一个密码,我也尝试设置bssid_set为 1 并传入 WiFi 网络的 bssid,但在这两种情况下都会出现相同的错误。

4

1 回答 1

0

问题出在 SoftAP 代码上。设置时softap_config我使用 sizeof 而不是 strlen 或根本不设置它,导致它托管一个在 SSID 中具有空字符的网络。

固定示例:

#include <osapi.h>
#include <user_interface.h>

void ICACHE_FLASH_ATTR user_init(void) {
    // Delays 1 second for my serial monitor to catch up
    for (int i = 0; i < 200; i++) os_delay_us(5000);

    gpio_init();
    uart_init(115200, 115200);

    wifi_softap_dhcps_stop();

    wifi_set_opmode(SOFTAP_MODE);

    struct softap_config softAPConfig = {
        .ssid = {0},
        .password = {0},
        .authmode = AUTH_OPEN,
        .max_connection = 4,
        .beacon_interval = 100,
    };

    os_memcpy(softAPConfig.ssid, "espnetwork", sizeof("espnetwork"));

    wifi_softap_set_config(&softAPConfig);

    wifi_softap_dhcps_start();
}

uint32 ICACHE_FLASH_ATTR user_rf_cal_sector_set(void) {
    enum flash_size_map size_map = system_get_flash_size_map();
    uint32 rf_cal_sec = 0;

    switch (size_map) {
        case FLASH_SIZE_4M_MAP_256_256:
            rf_cal_sec = 128 - 5;
            break;

        case FLASH_SIZE_8M_MAP_512_512:
            rf_cal_sec = 256 - 5;
            break;

        case FLASH_SIZE_16M_MAP_512_512:
        case FLASH_SIZE_16M_MAP_1024_1024:
            rf_cal_sec = 512 - 5;
            break;

        case FLASH_SIZE_32M_MAP_512_512:
        case FLASH_SIZE_32M_MAP_1024_1024:
            rf_cal_sec = 1024 - 5;
            break;

        case FLASH_SIZE_64M_MAP_1024_1024:
            rf_cal_sec = 2048 - 5;
            break;
        case FLASH_SIZE_128M_MAP_1024_1024:
            rf_cal_sec = 4096 - 5;
            break;
        default:
            rf_cal_sec = 0;
            break;
    }
    return rf_cal_sec;
}

谢谢大家的帮助。

于 2021-08-05T12:24:23.187 回答