[SOLVED] Error when scanning WiFi with AP enabled
Posted: Mon Feb 14, 2022 3:51 pm
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):
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:
And, after that, I call this function:
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 ??:?
This is the function I use to enable AP+STA:
- esp_err_t
- wifi_ap_scan_init (const uint8_t ap_ssid[SSID_LENGTH],
- const uint8_t ap_password[PSWD_LENGTH])
- {
- unsigned int index = 0; /* Indice dei for. */
- esp_err_t ret = ESP_FAIL; /* Valore di ritorno. */
- esp_netif_t * p_wifi_ap = esp_netif_create_default_wifi_ap();
- esp_netif_ip_info_t ipInfo;
- esp_netif_dhcps_stop(p_wifi_ap);
- IP4_ADDR(&ipInfo.ip, 192, 168, 1, 1);
- IP4_ADDR(&ipInfo.gw, 192, 168, 1, 1);
- IP4_ADDR(&ipInfo.netmask, 255, 255, 255, 0);
- esp_netif_set_ip_info(p_wifi_ap, &ipInfo);
- esp_netif_dhcps_start(p_wifi_ap);
- ESP_LOGI(g_p_tag, "AP - IP: " IPSTR, IP2STR(&ipInfo.ip));
- ESP_LOGI(g_p_tag, "AP - GW: " IPSTR, IP2STR(&ipInfo.gw));
- ESP_LOGI(g_p_tag, "AP - Mask: " IPSTR, IP2STR(&ipInfo.netmask));
- ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
- wifi_config_t wifi_access_point_config = {
- .ap = {
- .ssid = "Default",
- .ssid_len = strlen((const char *) ap_ssid),
- .password = "Default",
- .max_connection = MAX_STA_CONNECTED,
- .authmode = WIFI_AUTH_WPA_WPA2_PSK
- },
- };
- if (strlen((const char *) ap_password) == 0)
- {
- wifi_access_point_config.ap.authmode = WIFI_AUTH_OPEN;
- }
- for (index = 0; index < strlen((const char *) ap_ssid); ++index)
- {
- wifi_access_point_config.ap.ssid[index] = ap_ssid[index];
- if ('\0' == ap_ssid[index])
- {
- break;
- }
- }
- for (index = 0; index < strlen((const char *) ap_password); ++index)
- {
- wifi_access_point_config.ap.password[index] = ap_password[index];
- if ('\0' == ap_password[index])
- {
- break;
- }
- }
- (void) esp_wifi_set_mode(WIFI_MODE_APSTA);
- (void) esp_wifi_set_config(WIFI_IF_AP, &wifi_access_point_config);
- (void) esp_wifi_start();
- return ret;
- }
- void
- wifi_station_scanner (void)
- {
- uint16_t number = CONFIG_WIFI_PROV_SCAN_MAX_ENTRIES;
- wifi_ap_record_t ap_info[CONFIG_WIFI_PROV_SCAN_MAX_ENTRIES];
- memset(ap_info, 0, sizeof(ap_info));
- uint16_t ap_count = 0;
- (void) esp_wifi_scan_start(NULL, true);
- ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
- ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
- ESP_LOGI(g_p_tag, "Total APs scanned = %u", ap_count);
- for (int idx = 0; (idx < number) && (idx < ap_count); ++idx)
- {
- ESP_LOGI(g_p_tag, "SSID \t\t%s", ap_info[idx].ssid);
- ESP_LOGI(g_p_tag, "RSSI \t\t%d", ap_info[idx].rssi);
- print_auth_mode(ap_info[idx].authmode);
- if (ap_info[idx].authmode != WIFI_AUTH_WEP)
- {
- print_cipher_type(ap_info[idx].pairwise_cipher,
- ap_info[idx].group_cipher);
- }
- ESP_LOGI(g_p_tag, "Channel \t\t%d\n", ap_info[idx].primary);
- }
- }