I'm actually new to ESP-IDF and I've got an issue: I wonder why the station that connects to the esp (currently mode it's AP_STA) keep disconnecting and reconnecting.
This problem came out when I started using NVS functions. It seems like I can't use the NVS before I started the soft-ap.
I’m currently using the 3.1.1 (stable version).
Here an example of the log:
Code: Select all
...startup print...
Opening Non-Volatile Storage (NVS) handle... Done
Closing Non-Volatile Storage (NVS) handle...Done
BOARD ID: esp-111111111111
I (117) wifi: wifi driver task: 3ffc0dc8, prio:23, stack:3584, core=0
I (127) wifi: wifi firmware version: b2c9a19
I (127) wifi: config NVS flash: enabled
I (127) wifi: config nano formating: disabled
I (137) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (147) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (167) wifi: Init dynamic tx buffer num: 32
I (167) wifi: Init data frame dynamic rx buffer num: 32
I (167) wifi: Init management frame dynamic rx buffer num: 32
I (177) wifi: Init static rx buffer size: 1600
I (177) wifi: Init static rx buffer num: 10
I (177) wifi: Init dynamic rx buffer num: 32
I (287) phy: phy_version: 4000, b6198fa, Sep 3 2018, 15:11:06, 0, 0
I (287) wifi: mode : sta (24:0a:c4:9f:97:10) + softAP (24:0a:c4:9f:97:11)
I (287) wifi: Init max length of beacon: 752/752
I (287) wifi: Init max length of beacon: 752/752
I (297) MAIN: wifi_init_softap finished.SSID:esp-111111111111
I (297) MAIN: *** DEFAULT EVENT*** 2
I (307) MAIN: *** DEFAULT EVENT*** 13
I (351347) wifi: n:1 0, o:1 0, ap:1 1, sta:0 0, prof:1
I (351347) wifi: station: 74:da:38:ea:3a:43 join, AID=1, bg, 20
I (351347) MAIN: station:74:da:38:ea:3a:43 join, AID=1
I (360157) tcpip_adapter: softAP assign IP to station,IP is: 192.168.4.2
I (360157) MAIN: *** DEFAULT EVENT*** 17
I (362327) wifi: station: 74:da:38:ea:3a:43 leave, AID = 1
I (362327) wifi: n:1 0, o:1 0, ap:1 1, sta:0 0, prof:1
I (362327) MAIN: station:74:da:38:ea:3a:43leave, AID=1
I (363427) wifi: n:1 0, o:1 0, ap:1 1, sta:0 0, prof:1
I (363427) wifi: station: 74:da:38:ea:3a:43 join, AID=1, bg, 20
I (363427) MAIN: station:74:da:38:ea:3a:43 join, AID=1
I (382357) wifi: station: 74:da:38:ea:3a:43 leave, AID = 1
...loop of:
I (363427) wifi: station: 74:da:38:ea:3a:43 join, AID=1, bg, 20
I (363427) MAIN: station:74:da:38:ea:3a:43 join, AID=1
I (382357) wifi: station: 74:da:38:ea:3a:43 leave, AID = 1
Code: Select all
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include <string.h>
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "header/chip_info.h"
#include <nvs_util.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
#include "freertos/event_groups.h"
#include "esp_log.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "tcpip_adapter.h"
static char *boardId = "esp-111111111111";
#define MAIN_TAG "MAIN"
static esp_err_t
event_handler (void *ctx, system_event_t *event)
{
switch (event->event_id)
{
case SYSTEM_EVENT_STA_GOT_IP:
/*
* ESP_LOGI(MAIN_TAG, "*** SYSTEM_EVENT_STA_GOT_IP***");
* DO some stuff
*/
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
/*
* DO other stuff
*/
break;
case SYSTEM_EVENT_AP_STACONNECTED:
ESP_LOGI(MAIN_TAG, "station:"MACSTR" join, AID=%d",
MAC2STR(event->event_info.sta_connected.mac),
event->event_info.sta_connected.aid);
break;
case SYSTEM_EVENT_AP_STADISCONNECTED:
ESP_LOGI(MAIN_TAG, "station:"MACSTR"leave, AID=%d",
MAC2STR(event->event_info.sta_disconnected.mac),
event->event_info.sta_disconnected.aid);
break;
default:
ESP_LOGI(MAIN_TAG, "*** DEFAULT EVENT*** %i", event->event_id);
break;
}
return ESP_OK;
}
void
wifi_init_softap ()
{
printf ("BOARD ID: %s\n", boardId);
tcpip_adapter_init ();
tcpip_adapter_set_hostname (TCPIP_ADAPTER_IF_STA, "ESP32");
tcpip_adapter_set_hostname (TCPIP_ADAPTER_IF_AP, "ESP32");
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init (&cfg));
wifi_config_t wifi_config;
for (int i = 0; i < 17; ++i)
wifi_config.ap.ssid[i] = boardId[i];
wifi_config.ap.ssid_len = 16;
wifi_config.ap.max_connection = EXAMPLE_MAX_STA_CONN;
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
ESP_ERROR_CHECK(esp_wifi_set_mode (WIFI_MODE_APSTA));
ESP_ERROR_CHECK(esp_wifi_set_config (ESP_IF_WIFI_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start ());
ESP_LOGI(MAIN_TAG, "wifi_init_softap finished.SSID:%s", wifi_config.ap.ssid);
}
void
app_main ()
{
//Initialize NVS
esp_err_t ret = nvs_flash_init ();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
// NVS partition was truncated and needs to be erased
// Retry nvs_flash_init
ESP_ERROR_CHECK(nvs_flash_erase ());
ret = nvs_flash_init ();
}
ESP_ERROR_CHECK(ret);
//do stuff with NVS that actually does not affect the bug
nvs_handle handle_nvs;
openNvs(&handle_nvs);
closeNvs(&handle_nvs);
wifi_init_softap();
}
Code: Select all
vent->event_info.sta_connected.aid);
break;
case SYSTEM_EVENT_AP_STADISCONNECTED:
ESP_LOGI(MAIN_TAG, "station:"MACSTR"leave, AID=%d",
MAC2STR(event->event_info.sta_disconnected.mac),
event->event_info.sta_disconnected.aid);
break;
void openNvs (nvs_handle* handle)
{
// Open nvs
printf ("\n");
printf ("Opening Non-Volatile Storage (NVS) handle... ");
esp_err_t ret = nvs_open ("storage", NVS_READWRITE, handle);
if (ret != ESP_OK)
{
printf ("Error (%s) opening NVS handle!\n", esp_err_to_name (ret));
}
else
{
printf ("Done\n");
}
}
void closeNvs(nvs_handle* handle)
{
printf("\n");
printf("Closing Non-Volatile Storage (NVS) handle...");
nvs_close (*handle);
printf ("Done\n");
}