5

I am trying to connect a ESP8266 to a WiFi network hosted by another ESP8266. The problem is the ESP8266 shows the WiFi network during a WiFi scan but fails to connect to it with the error: no espnetwork found, reconnect after 1s.

Code for the ESP8266 hosting the network:

#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;
}

Code for the ESP8266 searching and connection to the network:

#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;
}

Log from the ESP8266 trying to connect to the network:

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

This log shows that the ESP8266 finds the network (line 4) but then fails to connect and then indefinitely tries to reconnect.

I have tried to change the auth mode of the WiFi network and give it a password, I have also tried setting bssid_set to 1 and passing in the bssid of the WiFi network but in both cases the same error arises.

Gus
  • 151
  • 2
  • 15
  • STATIONAP acts as an access point AND joins an existing network. You don't have the existing network. I think you want one in SOFTAP and the other in STATION mode. – stark Jul 26 '21 at 17:06
  • @stark I know. I would like to expand the network with more ESP8266s into a mesh, so all need to be able to host a softap and be a station. To test I tried making one only a softap and the other only a station but the station still receives the same error. – Gus Jul 26 '21 at 17:18
  • @stark I was trying to connect the ESP8266s together, so I swapped the CURRENT_SSID and OTHER_SSID when uploading to each ESP8266. Then I powered on the first which would host a softap and not find the other ESP8266. After I powered on the second which should host a softap and then find the other and connect to it but it gives the error. – Gus Jul 26 '21 at 17:30
  • @stark I have added more context. Sorry I didn't put many details in at the start. – Gus Jul 26 '21 at 18:08
  • I suggest you look into ESP-NOW instead of using classical WiFi for your setup: https://randomnerdtutorials.com/esp-now-esp8266-nodemcu-arduino-ide/ – Marcel Stör Jul 27 '21 at 05:30
  • @MarcelStör I have looked into ESP-NOW but I would like to use WiFi networks. I have rewrote my question and made it more concise on the problem. – Gus Jul 27 '21 at 09:32
  • I might be misreading (and have never touched this platform), but it looks like you aren't setting `softAPConfig.ssid` to your SSID. Is that a copying error? – Hasturkun Jul 29 '21 at 11:01
  • @Hasturkun Good spot, I did forget to add that but the SSID was already saved in the flash from other runs. So sadly that is not the problem. – Gus Jul 29 '21 at 11:53
  • @Gus Can you try out the example [here](https://www.espressif.com/sites/default/files/documentation/2c-esp8266_non_os_sdk_api_reference_en.pdf) so we can narrow down if it's a STA problem or an AP problem? – Anubhav Aug 05 '21 at 03:57

1 Answers1

0

The problem is with the SoftAP code. When setting the softap_config I used sizeof instead of strlen or not setting it at all, leading it to host a network with a null char in the SSID.

Fixed Example:

#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;
}

Thank you for all the help.

Gus
  • 151
  • 2
  • 15