issue while using NVS and SoftAP

fdimarco
Posts: 3
Joined: Tue Dec 11, 2018 4:53 pm

issue while using NVS and SoftAP

Postby fdimarco » Fri Dec 14, 2018 2:09 pm

Hello everyone,

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

Source code:

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();
}
Here code in #include <nvs_util.h>

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");
}
Thanks all

Ritesh
Posts: 1365
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: issue while using NVS and SoftAP

Postby Ritesh » Sun Dec 16, 2018 4:48 pm

fdimarco wrote:
Fri Dec 14, 2018 2:09 pm
Hello everyone,

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

Source code:

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();
}
Here code in #include <nvs_util.h>

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");
}
Thanks all

Hi,

Did you check WiFi connectivity issue after disabling NVS code?
Because there is no any connection between WiFi connectivity and NVS code though WiFi is using NVS for WiFi configuration.

So, I don't think that configuration are overrides with your NVS code.

Would you please check with direct flash API access instead of NVS with network connectivity?
Regards,
Ritesh Prajapati

fdimarco
Posts: 3
Joined: Tue Dec 11, 2018 4:53 pm

Re: issue while using NVS and SoftAP

Postby fdimarco » Mon Dec 17, 2018 5:26 pm

Hi,

Did you check WiFi connectivity issue after disabling NVS code?
Because there is no any connection between WiFi connectivity and NVS code though WiFi is using NVS for WiFi configuration.

So, I don't think that configuration are overrides with your NVS code.

Would you please check with direct flash API access instead of NVS with network connectivity?
Hi Ritesh,

Thank you for the answer.

I tried to disable the NVS code and the issue disappeared.
I also tried to initialize soft ap before using Nvs, and the problem disappeared also in this configuration.
It seems like using NVS it's the actual issue.

Main code without the issue:

Code: Select all

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);

  wifi_init_softap(); // softap init

  // do some stuff with nvs...
  nvs_handle handle_nvs;
  openNvs(&handle_nvs);

  closeNvs(&handle_nvs);
}
Its output:

Code: Select all

..booting info...
I (254) MAIN: wifi_init_softap finished.SSID:esp-111111111111
I (254) MAIN: ***  DEFAULT EVENT*** 2

Opening Non-Volatile Storage (NVS) handle... Done
I (264) MAIN: ***  DEFAULT EVENT*** 13

Closing Non-Volatile Storage (NVS) handle...Done
I (19524) wifi: n:1 0, o:1 0, ap:1 1, sta:0 0, prof:1
I (19524) wifi: station: 74:da:38:ea:3a:43 join, AID=1, bg, 20
I (19524) MAIN: station:74:da:38:ea:3a:43 join, AID=1
I (19554) tcpip_adapter: softAP assign IP to station,IP is: 192.168.4.2
I (19554) MAIN: ***  DEFAULT EVENT*** 17
Anyway I would like to start the ap after some nvs operations, so changing the order it's not helping at all.


In Nvs procedure I simply open and close the nvs without performing any actions.
I think it's not evident why esp keep closing/reopening the connection with the client.

Do you think the problem might be in the ap initialization? Maybe the function esp_wifi_set_config (ESP_IF_WIFI_AP, &wifi_config) is not working properly in dual mode (AP_STA)?

Code: Select all

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);
} 
Do you have any other suggestion?


Soon I will try to use flash API access instead of NVS as you suggested.

Regards
F.

Ritesh
Posts: 1365
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: issue while using NVS and SoftAP

Postby Ritesh » Tue Dec 18, 2018 5:45 am

fdimarco wrote:
Mon Dec 17, 2018 5:26 pm
Hi,

Did you check WiFi connectivity issue after disabling NVS code?
Because there is no any connection between WiFi connectivity and NVS code though WiFi is using NVS for WiFi configuration.

So, I don't think that configuration are overrides with your NVS code.

Would you please check with direct flash API access instead of NVS with network connectivity?
Hi Ritesh,

Thank you for the answer.

I tried to disable the NVS code and the issue disappeared.
I also tried to initialize soft ap before using Nvs, and the problem disappeared also in this configuration.
It seems like using NVS it's the actual issue.

Main code without the issue:

Code: Select all

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);

  wifi_init_softap(); // softap init

  // do some stuff with nvs...
  nvs_handle handle_nvs;
  openNvs(&handle_nvs);

  closeNvs(&handle_nvs);
}
Its output:

Code: Select all

..booting info...
I (254) MAIN: wifi_init_softap finished.SSID:esp-111111111111
I (254) MAIN: ***  DEFAULT EVENT*** 2

Opening Non-Volatile Storage (NVS) handle... Done
I (264) MAIN: ***  DEFAULT EVENT*** 13

Closing Non-Volatile Storage (NVS) handle...Done
I (19524) wifi: n:1 0, o:1 0, ap:1 1, sta:0 0, prof:1
I (19524) wifi: station: 74:da:38:ea:3a:43 join, AID=1, bg, 20
I (19524) MAIN: station:74:da:38:ea:3a:43 join, AID=1
I (19554) tcpip_adapter: softAP assign IP to station,IP is: 192.168.4.2
I (19554) MAIN: ***  DEFAULT EVENT*** 17
Anyway I would like to start the ap after some nvs operations, so changing the order it's not helping at all.


In Nvs procedure I simply open and close the nvs without performing any actions.
I think it's not evident why esp keep closing/reopening the connection with the client.

Do you think the problem might be in the ap initialization? Maybe the function esp_wifi_set_config (ESP_IF_WIFI_AP, &wifi_config) is not working properly in dual mode (AP_STA)?

Code: Select all

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);
} 
Do you have any other suggestion?


Soon I will try to use flash API access instead of NVS as you suggested.

Regards
F.
Hi,

We have already test AP, STA and AP+STA with NVS as well into ESP32 IDF 3.0 and 3.1 Stable Release but didn't find any issue like you are facing right now.

Would you please use default example provided with IDF to make sure like it is working fine with that or not?

Also as i have suggested earlier like check with SPI Flash API with same scenario as well.
Regards,
Ritesh Prajapati

fdimarco
Posts: 3
Joined: Tue Dec 11, 2018 4:53 pm

Re: issue while using NVS and SoftAP

Postby fdimarco » Wed Dec 19, 2018 4:02 pm

Hi,

We have already test AP, STA and AP+STA with NVS as well into ESP32 IDF 3.0 and 3.1 Stable Release but didn't find any issue like you are facing right now.

Would you please use default example provided with IDF to make sure like it is working fine with that or not?

Also as i have suggested earlier like check with SPI Flash API with same scenario as well.
Hi,

Thank you again for the reply.

I tried to use the official example, but it was working only if the code was in the main function: writing the code inside another function wasn't working at all.

Anyway I find out the problem: the wifi_config_t structure needs to be fully initialized.

Here the code:

Code: Select all

void
wifi_init_softap ()
{
  getChipId(boardId);
  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<32; ++i) {
      wifi_config.ap.ssid[i]= 0;
      wifi_config.ap.password[i]= 0;
      wifi_config.ap.password[i+32]= 0;
  }
  wifi_config.ap.ssid_len = 0;
  wifi_config.ap.channel= 0;
  wifi_config.ap.authmode = WIFI_AUTH_OPEN;
  wifi_config.ap.ssid_hidden =0;
  wifi_config.ap.max_connection =0;
  wifi_config.ap.beacon_interval=100;


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

Thank you again.
F.

Ritesh
Posts: 1365
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: issue while using NVS and SoftAP

Postby Ritesh » Thu Dec 20, 2018 8:05 am

fdimarco wrote:
Wed Dec 19, 2018 4:02 pm
Hi,

We have already test AP, STA and AP+STA with NVS as well into ESP32 IDF 3.0 and 3.1 Stable Release but didn't find any issue like you are facing right now.

Would you please use default example provided with IDF to make sure like it is working fine with that or not?

Also as i have suggested earlier like check with SPI Flash API with same scenario as well.
Hi,

Thank you again for the reply.

I tried to use the official example, but it was working only if the code was in the main function: writing the code inside another function wasn't working at all.

Anyway I find out the problem: the wifi_config_t structure needs to be fully initialized.

Here the code:

Code: Select all

void
wifi_init_softap ()
{
  getChipId(boardId);
  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<32; ++i) {
      wifi_config.ap.ssid[i]= 0;
      wifi_config.ap.password[i]= 0;
      wifi_config.ap.password[i+32]= 0;
  }
  wifi_config.ap.ssid_len = 0;
  wifi_config.ap.channel= 0;
  wifi_config.ap.authmode = WIFI_AUTH_OPEN;
  wifi_config.ap.ssid_hidden =0;
  wifi_config.ap.max_connection =0;
  wifi_config.ap.beacon_interval=100;


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

Thank you again.
F.
Great. Thanks for update related to this which might be helpful to others as well in case of this type of issue
Regards,
Ritesh Prajapati

Who is online

Users browsing this forum: Majestic-12 [Bot] and 165 guests