Page 1 of 1

[SOLVED] Error when scanning WiFi with AP enabled

Posted: Mon Feb 14, 2022 3:51 pm
by filo_gr
Hello,

I'm trying to implement the following behavior: ESP32 should start its mode as STA, so it searches a particular SSID with a password.
If it doesn't work, it quits the STA mode, then creates an AP+STA mode.
Now it should behave like a webserver. I would be able to connect to it and read an HTML page that shows the SSID of the various stations (so it should scan the available WiFi stations).

I'm stuck when I enable the WiFi scanner. Here the log (where it searches for a specific WiFi, it doesn't find it, then starts the AP+STA mode and tries to scan):

Code: Select all

I (562) wifi_manage: START STATION MODE
W (2612) wifi_manage: Retry to connect to the AP
E (2612) wifi_manage: DISCONNECTED FROM AP
W (4652) wifi_manage: Retry to connect to the AP
E (4652) wifi_manage: DISCONNECTED FROM AP
W (6702) wifi_manage: Retry to connect to the AP
E (6702) wifi_manage: DISCONNECTED FROM AP
W (8752) wifi_manage: Retry to connect to the AP
E (8752) wifi_manage: DISCONNECTED FROM AP
W (10792) wifi_manage: Retry to connect to the AP
E (10792) wifi_manage: DISCONNECTED FROM AP
E (12842) wifi_manage: DISCONNECTED FROM AP
E (12842) wifi_manage: Failed to connect to SSID:esp32wifi, password:123456789
I (12842) core: SSID: connector_7C:DF:A1:61:E4:4C
I (12842) wifi_manage: AP - IP: 192.168.1.1
I (12852) wifi_manage: AP - GW: 192.168.1.1
I (12852) wifi_manage: AP - Mask: 255.255.255.0
I (12862) wifi:mode : null
I (12862) wifi_manage: CLOSE STATION MODE
I (12872) wifi:mode : sta (7c:df:a1:61:e4:4c) + softAP (7c:df:a1:61:e4:4d)
I (12872) wifi:enable tsf
I (12882) wifi:Total power save buffer number: 16
I (12882) wifi:Init max length of beacon: 752/752
chan:1,max_power:80
chan:2,max_power:80
chan:3,max_power:80
chan:4,max_power:80
chan:5,max_power:80
chan:6,max_power:80
chan:7,max_power:80
chan:8,max_power:80
chan:9,max_power:80
chan:10,max_power:80
chan:11,max_power:80
chan:12,max_power:80
chan:13,max_power:80
chan:14,max_power:80
I (12912) wifi:Init max length of beacon: 752/752
I (12932) wifi_manage: START STATION MODE
I (12932) wifi_manage: START AP MODE
I (12932) wifi:Total power save buffer number: 16
I (12932) wifi_manage: CLOSE AP MODE
I (12942) wifi_manage: START AP MODE
W (12942) wifi:sta_scan: STA is connecting, scan are not allowed!
I (12952) wifi_manage:
***ERROR*** A stack overflow in task main has been detected.
Stack dump detected
Core  0 register dump:
MEPC    : 0x40380776  RA      : 0x403864ac  SP      : 0x3fc914e0  GP      : 0x3fc8ea00  
0x40380776: panic_abort at C:/Espressif/frameworks/esp-idf-v4.4/components/esp_system/panic.c:402

0x403864ac: __ubsan_include at C:/Espressif/frameworks/esp-idf-v4.4/components/esp_system/ubsan.c:294

TP      : 0x3fc840a8  T0      : 0x4005890e  T1      : 0x0000000f  T2      : 0x00000001        
S0/FP   : 0x00000003  S1      : 0x80000001  A0      : 0x3fc914f8  A1      : 0x3c081ff8  
A2      : 0x00000003  A3      : 0x3fc91522  A4      : 0x00000001  A5      : 0x3fc94000        
A6      : 0x42002142  A7      : 0x0000000a  S2      : 0x00001881  S3      : 0x00000001        
0x42002142: console_write at C:/Espressif/frameworks/esp-idf-v4.4/components/vfs/vfs_console.c:71

S4      : 0x3fc959e8  S5      : 0x3fc96fe0  S6      : 0x3c083dc0  S7      : 0x00000000        
S8      : 0x00000005  S9      : 0x00000000  S10     : 0x3fc96ff4  S11     : 0x00000001        
T3      : 0x00000000  T4      : 0x00000000  T5      : 0x00000001  T6      : 0x00000000        
MSTATUS : 0x00001801  MTVEC   : 0x40380001  MCAUSE  : 0x00000007  MTVAL   : 0x00000000        
0x40380001: _vector_table at ??:?
It seems to disconnect the AP and reconnect it before scanning. But after that it fails with an error.

This is the function I use to enable AP+STA:
  1. esp_err_t
  2. wifi_ap_scan_init (const uint8_t ap_ssid[SSID_LENGTH],
  3.                    const uint8_t ap_password[PSWD_LENGTH])
  4. {
  5.     unsigned int index = 0;             /* Indice dei for. */
  6.     esp_err_t ret = ESP_FAIL;           /* Valore di ritorno. */
  7.  
  8.     esp_netif_t * p_wifi_ap = esp_netif_create_default_wifi_ap();
  9.  
  10.     esp_netif_ip_info_t ipInfo;
  11.  
  12.     esp_netif_dhcps_stop(p_wifi_ap);
  13.     IP4_ADDR(&ipInfo.ip, 192, 168, 1, 1);
  14.     IP4_ADDR(&ipInfo.gw, 192, 168, 1, 1);
  15.     IP4_ADDR(&ipInfo.netmask, 255, 255, 255, 0);
  16.  
  17.     esp_netif_set_ip_info(p_wifi_ap, &ipInfo);
  18.  
  19.     esp_netif_dhcps_start(p_wifi_ap);
  20.     ESP_LOGI(g_p_tag, "AP - IP: " IPSTR, IP2STR(&ipInfo.ip));
  21.     ESP_LOGI(g_p_tag, "AP - GW: " IPSTR, IP2STR(&ipInfo.gw));
  22.     ESP_LOGI(g_p_tag, "AP - Mask: " IPSTR, IP2STR(&ipInfo.netmask));
  23.  
  24.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
  25.  
  26.     wifi_config_t wifi_access_point_config = {
  27.         .ap = {
  28.             .ssid = "Default",
  29.             .ssid_len = strlen((const char *) ap_ssid),
  30.             .password = "Default",
  31.             .max_connection = MAX_STA_CONNECTED,
  32.             .authmode = WIFI_AUTH_WPA_WPA2_PSK
  33.         },
  34.     };
  35.  
  36.     if (strlen((const char *) ap_password) == 0)
  37.     {
  38.         wifi_access_point_config.ap.authmode = WIFI_AUTH_OPEN;
  39.     }
  40.  
  41.     for (index = 0; index < strlen((const char *) ap_ssid); ++index)
  42.     {
  43.         wifi_access_point_config.ap.ssid[index] = ap_ssid[index];
  44.         if ('\0' == ap_ssid[index])
  45.         {
  46.             break;
  47.         }
  48.     }
  49.     for (index = 0; index < strlen((const char *) ap_password); ++index)
  50.     {
  51.         wifi_access_point_config.ap.password[index] = ap_password[index];
  52.         if ('\0' == ap_password[index])
  53.         {
  54.             break;
  55.         }
  56.     }
  57.     (void) esp_wifi_set_mode(WIFI_MODE_APSTA);
  58.     (void) esp_wifi_set_config(WIFI_IF_AP, &wifi_access_point_config);
  59.     (void) esp_wifi_start();
  60.     return ret;
  61. }
And, after that, I call this function:
  1. void
  2. wifi_station_scanner (void)
  3. {
  4.     uint16_t number = CONFIG_WIFI_PROV_SCAN_MAX_ENTRIES;
  5.     wifi_ap_record_t ap_info[CONFIG_WIFI_PROV_SCAN_MAX_ENTRIES];
  6.     memset(ap_info, 0, sizeof(ap_info));
  7.     uint16_t ap_count = 0;
  8.  
  9.     (void) esp_wifi_scan_start(NULL, true);
  10.  
  11.     ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
  12.  
  13.     ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
  14.     ESP_LOGI(g_p_tag, "Total APs scanned = %u", ap_count);
  15.  
  16.     for (int idx = 0; (idx < number) && (idx < ap_count); ++idx)
  17.     {
  18.         ESP_LOGI(g_p_tag, "SSID \t\t%s", ap_info[idx].ssid);
  19.         ESP_LOGI(g_p_tag, "RSSI \t\t%d", ap_info[idx].rssi);
  20.         print_auth_mode(ap_info[idx].authmode);
  21.         if (ap_info[idx].authmode != WIFI_AUTH_WEP)
  22.         {
  23.             print_cipher_type(ap_info[idx].pairwise_cipher,
  24.                               ap_info[idx].group_cipher);
  25.         }
  26.         ESP_LOGI(g_p_tag, "Channel \t\t%d\n", ap_info[idx].primary);
  27.     }
  28. }

Re: Error when scanning WiFi with AP enabled

Posted: Mon Feb 14, 2022 4:27 pm
by Craige Hales
Add print statements to narrow down what happens just before

Code: Select all

***ERROR*** A stack overflow in task main has been detected.
Might be unintended recursion.

Re: Error when scanning WiFi with AP enabled

Posted: Mon Feb 14, 2022 6:42 pm
by WiFive
You put this big array on the stack so it overflowed

Code: Select all

wifi_ap_record_t ap_info[CONFIG_WIFI_PROV_SCAN_MAX_ENTRIES];

Re: Error when scanning WiFi with AP enabled

Posted: Tue Feb 15, 2022 12:58 am
by Craige Hales
Well spotted. ~1000 bytes I think.

Re: Error when scanning WiFi with AP enabled

Posted: Tue Feb 15, 2022 10:38 am
by filo_gr
WiFive wrote:
Mon Feb 14, 2022 6:42 pm
You put this big array on the stack so it overflowed

Code: Select all

wifi_ap_record_t ap_info[CONFIG_WIFI_PROV_SCAN_MAX_ENTRIES];
You are right, the problem was originated from the too much large array.
However solving it produces a new error (see below). I obtain this error if I force CONFIG_WIFI_PROV_SCAN_MAX_ENTRIES=1 but even if I use malloc to allocate the array in the heap.

Code: Select all

D (14937) esp_netif_lwip: esp_netif_update_default_netif_lwip 0x3fca3fe8
D (14947) esp_netif_lwip: call api in lwip: ret=0x0, give sem
D (14947) wifi:Start wifi scan
W (14957) wifi:sta_scan: STA is connecting, scan are not allowed!
I (14957) wifi_manage: Total APs scanned = 0
D (15007) wifi:scan end: arg=0x0, status=0, ss_state=0x3
D (15037) wifi:perform scan: ss_state=0x9, chan<4,0>, dur<0,120>
D (15157) wifi:scan end: arg=0x0, status=0, ss_state=0x3
D (15187) wifi:perform scan: ss_state=0x9, chan<5,0>, dur<0,120>
D (15307) wifi:scan end: arg=0x0, status=0, ss_state=0x3
D (15337) wifi:perform scan: ss_state=0x9, chan<6,0>, dur<0,120>
D (15457) wifi:scan end: arg=0x0, status=0, ss_state=0x3
D (15497) wifi:perform scan: ss_state=0x9, chan<7,0>, dur<0,120>
D (15617) wifi:scan end: arg=0x0, status=0, ss_state=0x3
D (15647) wifi:perform scan: ss_state=0x9, chan<8,0>, dur<0,120>
D (15767) wifi:scan end: arg=0x0, status=0, ss_state=0x3
D (15797) wifi:perform scan: ss_state=0x9, chan<9,0>, dur<0,120>
D (15917) wifi:scan end: arg=0x0, status=0, ss_state=0x3
D (15947) wifi:perform scan: ss_state=0x9, chan<10,0>, dur<0,120>
D (16067) wifi:scan end: arg=0x0, status=0, ss_state=0x3
D (16097) wifi:perform scan: ss_state=0x9, chan<11,0>, dur<0,120>
D (16217) wifi:scan end: arg=0x0, status=0, ss_state=0x3
D (16247) wifi:perform scan: ss_state=0x9, chan<12,0>, dur<360,360>
D (16617) wifi:scan end: arg=0x0, status=0, ss_state=0x3
D (16647) wifi:perform scan: ss_state=0x9, chan<13,0>, dur<360,360>
D (17007) wifi:scan end: arg=0x0, status=0, ss_state=0x3
D (17037) wifi:filter: set rx policy=4
D (17037) wifi:first chan=1
D (17037) wifi:handoff_cb: status=0
D (17037) wifi:clear rc list
D (17037) wifi:clear blacklist
D (17037) wifi:send disconnect event
D (17037) wifi:connect status 1 -> 3
D (17047) wifi:disable connect timer
D (17047) wifi:clear scan ap list
D (17047) event: running post WIFI_EVENT:5 with handler 0x420074aa and context 0x3fca3474 on loop 0x3fc9accc
0x420074aa: wifi_event_handler at c:\users\fgraziani\documents\lavoro\a800_connector\build/../components/wifi_manager_component/wifi_manage.c:99

Guru Meditation Error: Core  0 panic'ed (Load access fault). Exception was unhandled.

Stack dump detected
Core  0 register dump:
MEPC    : 0x40389312  RA      : 0x403892aa  SP      : 0x3fc9b930  GP      : 0x3fc8ea00
0x40389312: xEventGroupSetBits at C:/Espressif/frameworks/esp-idf-v4.4/components/freertos/event_groups.c:617

0x403892aa: xEventGroupSetBits at C:/Espressif/frameworks/esp-idf-v4.4/components/freertos/event_groups.c:609

TP      : 0x3fc84d20  T0      : 0x4005890e  T1      : 0x10000000  T2      : 0xffffffff
S0/FP   : 0x3fca385c  S1      : 0x00000002  A0      : 0x49464957  A1      : 0x3fca3fea
A2      : 0x00000005  A3      : 0x00000004  A4      : 0x00000001  A5      : 0x3fc94000
A6      : 0x4200220c  A7      : 0x0000000a  S2      : 0x3fca3868  S3      : 0x00000000
0x4200220c: console_write at C:/Espressif/frameworks/esp-idf-v4.4/components/vfs/vfs_console.c:71

S4      : 0x00000005  S5      : 0xffffffff  S6      : 0x00000000  S7      : 0xffffffff
S8      : 0x3fca3494  S9      : 0x00000000  S10     : 0x00000000  S11     : 0x00000000
T3      : 0x00000000  T4      : 0x00000000  T5      : 0x00000000  T6      : 0x00000000
MSTATUS : 0x00001881  MTVEC   : 0x40380001  MCAUSE  : 0x00000005  MTVAL   : 0x4946495b
0x40380001: _vector_table at ??:?
Basically it warns me that STA is connecting, scans are not allowed! and finds 0 APs (that is impossible, I see 7 WiFi networks on my PC). After that, it enters into the WiFi handler and it fails.

The official example https://github.com/espressif/esp-idf/tr ... /wifi/scan works well, I think the problem could be the enablet AP. I don't know what other could be.

Re: Error when scanning WiFi with AP enabled

Posted: Tue Feb 15, 2022 11:09 am
by filo_gr
Craige Hales wrote:
Mon Feb 14, 2022 4:27 pm
Add print statements to narrow down what happens just before

Code: Select all

***ERROR*** A stack overflow in task main has been detected.
Might be unintended recursion.
In the new log (my previous post) I updated the Default log verbosity to Debug, I hope it cloud be easier to understand what goes wrong

Re: Error when scanning WiFi with AP enabled

Posted: Thu Feb 17, 2022 10:02 am
by filo_gr
I solved the bugs.

Firstly, I sometimes have doubts about how to switch correcly between WiFi modes. But now I think I understood how to do it.

The second problem was inside my event handler. I was using a condition that stop the WiFi network when some unknow event appears. But in this case it wasn't necessary and caused the bug.