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.
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.