Wifi FTM stops after 2 or 3 minutes
Posted: Thu Aug 10, 2023 4:54 pm
Hey, I have 2 ESP32s2 on my hand. One is acting as an initiator. The other is acting as the responder. The wifi FTM works for a while, however, after 2 or 3 minutes, the FTM stops. I'm not exactly sure what is going on. I don't believe my code has any data leaks, and I browsed the forums to find a similar post (viewtopic.php?t=24226), but there is no solution or follow up afterwards. Does anyone have any input on what I could be overlooking? I am fairly certain its the responder having some issue. I attached 2 screenshots which shows what each board with their respective code, outputs to the terminal. In the responder screenshot, everything seems fine, but for the initiator screenshot, it just stops in the middle of a FTM request to the SoftAP at the responder.
FTM Responder Code: Just serves as a Soft AP for the FTM initiator to connect to
FTM Initiator Code: Performs wifi FTM measurements within a while loop near the end. There are some extra imports and variables not used, but I have some additional functions commented out that relate to the example code.
Additionally, I faced the same issue on Arduino, but when I made a post on that here, nobody responded, so I thought the ESP-IDF would resolve the issue.
I should also be clear I have not touched the menu config at all.
FTM Responder Code: Just serves as a Soft AP for the FTM initiator to connect to
- #include "esp_log.h"
- #include "esp_err.h"
- #include "esp_wifi.h"
- #include "nvs_flash.h"
- #include "sdkconfig.h"
- #include <string.h>
- // Wifi Credentials
- const char* SSID = "JJ8428";
- const char* PWD = "Renualt88";
- wifi_config_t g_ap_config = {
- .ap.max_connection = 1,
- .ap.authmode = WIFI_AUTH_WPA2_PSK,
- .ap.ftm_responder = true
- };
- static const char *TAG_AP = "ftm_ap";
- static void event_handler(void *arg, esp_event_base_t event_base,
- int32_t event_id, void *event_data)
- {
- if (event_id == WIFI_EVENT_AP_START) {
- ESP_LOGI(TAG_AP, "SoftAP started with FTM Responder support");
- } else if (event_id == WIFI_EVENT_AP_STOP) {
- ESP_LOGI(TAG_AP, "SoftAP stopped");
- }
- }
- static void wifi_ap()
- {
- // Check if Wifi modules are ready/functional
- ESP_ERROR_CHECK(esp_netif_init());
- ESP_ERROR_CHECK(esp_event_loop_create_default());
- wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
- ESP_ERROR_CHECK(esp_wifi_init(&cfg));
- strlcpy((char*) g_ap_config.ap.ssid, SSID, strlen(SSID) + 1);
- strlcpy((char*) g_ap_config.ap.password, PWD, strlen(PWD) + 1);
- // SoftAP Wifi configurations
- ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
- ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &g_ap_config));
- esp_wifi_set_bandwidth(ESP_IF_WIFI_AP, WIFI_BW_HT40); // SoftAP Bandwidth: 20 MHz
- ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
- // Log an informational message indicating the start of the SoftAP configuration.
- ESP_LOGI(TAG_AP, "SoftAP Configurued: SSID - %s, Password - %s",
- SSID, PWD
- );
- // Start the Wifi modules
- ESP_ERROR_CHECK(esp_wifi_start());
- }
- void app_main(void)
- {
- esp_err_t ret = nvs_flash_init();
- if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
- ESP_ERROR_CHECK(nvs_flash_erase());
- ret = nvs_flash_init();
- }
- ESP_ERROR_CHECK(ret);
- /*
- esp_event_handler_instance_t instance_any_id;
- ESP_ERROR_CHECK(esp_event_handler_instance_register(
- WIFI_EVENT,
- ESP_EVENT_ANY_ID,
- &event_handler,
- NULL,
- &instance_any_id
- )
- );
- */
- // Start the SoftAP and keep the code looping to host the SoftAP forever
- wifi_ap();
- while (true) {
- vTaskDelay(3000 / portTICK_PERIOD_MS);
- printf(xPortGetFreeHeapSize());
- }
- }
- #include <errno.h>
- #include <string.h>
- #include <inttypes.h>
- #include <stdio.h>
- #include "nvs_flash.h"
- #include "cmd_system.h"
- #include "argtable3/argtable3.h"
- #include "freertos/FreeRTOS.h"
- #include "freertos/event_groups.h"
- #include "esp_event.h"
- #include "esp_log.h"
- #include "esp_err.h"
- #include "esp_wifi.h"
- #include "esp_console.h"
- #include "esp_mac.h"
- const char *SSID = "JJ8428";
- typedef struct {
- struct arg_lit *initiator;
- struct arg_int *frm_count;
- struct arg_int *burst_period;
- struct arg_str *ssid;
- } wifi_ftm_args_t;
- static wifi_ftm_args_t ftm_args;
- static const char *TAG_STA = "ftm_station";
- static EventGroupHandle_t s_ftm_event_group;
- static const int FTM_REPORT_BIT = BIT0;
- static const int FTM_FAILURE_BIT = BIT1;
- static uint32_t s_rtt_est, s_dist_est;
- const int g_report_lvl = 0;
- uint16_t g_scan_ap_num;
- wifi_ap_record_t *g_ap_list_buffer;
- static void event_handler(void *arg, esp_event_base_t event_base,
- int32_t event_id, void *event_data)
- {
- if (event_id == WIFI_EVENT_FTM_REPORT) {
- wifi_event_ftm_report_t *event = (wifi_event_ftm_report_t *) event_data;
- if (event->status == FTM_STATUS_SUCCESS) {
- s_rtt_est = event->rtt_est;
- s_dist_est = event->dist_est;
- xEventGroupSetBits(s_ftm_event_group, FTM_REPORT_BIT);
- }
- }
- }
- void initialize_wifi(void)
- {
- esp_log_level_set("wifi", ESP_LOG_WARN);
- ESP_ERROR_CHECK(esp_netif_init());
- s_ftm_event_group = xEventGroupCreate();
- ESP_ERROR_CHECK( esp_event_loop_create_default());
- wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
- ESP_ERROR_CHECK(esp_wifi_init(&cfg));
- esp_event_handler_instance_t instance_any_id;
- ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
- ESP_EVENT_ANY_ID,
- &event_handler,
- NULL,
- &instance_any_id));
- ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
- ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
- ESP_ERROR_CHECK(esp_wifi_start());
- }
- // wifi_ap_record_t *find_ftm_responder_ap(const char *ssid)
- wifi_ap_record_t *find_ftm_responder_ap()
- {
- // Make this defined outside as a const as opposed to declaring it everytime
- wifi_scan_config_t scan_config = { 0 };
- scan_config.ssid = (uint8_t *) SSID;
- ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
- esp_wifi_scan_start(&scan_config, true);
- esp_wifi_scan_get_ap_num(&g_scan_ap_num);
- g_ap_list_buffer = malloc(g_scan_ap_num * sizeof(wifi_ap_record_t));
- esp_wifi_scan_get_ap_records(&g_scan_ap_num, (wifi_ap_record_t *)g_ap_list_buffer);
- for (int i = 0; i < g_scan_ap_num; i++) {
- if (strcmp((const char *)g_ap_list_buffer[i].ssid, SSID) == 0)
- return &g_ap_list_buffer[i];
- }
- return NULL;
- }
- void app_main(void)
- {
- esp_err_t ret = nvs_flash_init();
- if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
- ESP_ERROR_CHECK(nvs_flash_erase());
- ret = nvs_flash_init();
- }
- ESP_ERROR_CHECK(ret);
- initialize_wifi();
- wifi_ap_record_t *ap_record = find_ftm_responder_ap();
- free(g_ap_list_buffer);
- wifi_ftm_initiator_cfg_t ftmi_cfg = {
- .frm_count = 32,
- .burst_period = 2,
- };
- memcpy(ftmi_cfg.resp_mac, ap_record->bssid, 6);
- ftmi_cfg.channel = ap_record->primary;
- while (true) {
- ESP_LOGI(TAG_STA, "Requesting FTM session with Frm Count - %d, Burst Period - %dmSec (0: No Preference)",
- ftmi_cfg.frm_count, ftmi_cfg.burst_period*100);
- esp_wifi_ftm_initiate_session(&ftmi_cfg);
- EventBits_t bits = xEventGroupWaitBits(s_ftm_event_group, FTM_REPORT_BIT | FTM_FAILURE_BIT,
- pdTRUE, pdFALSE, portMAX_DELAY);
- if (bits & FTM_REPORT_BIT) {
- ESP_LOGI(TAG_STA, "Estimated RTT - %" PRId32 " nSec, Estimated Distance - %" PRId32 ".%02" PRId32 " meters",
- s_rtt_est, s_dist_est / 100, s_dist_est % 100);
- } else {
- ESP_LOGI(TAG_STA, "FTM Failed");
- }
- }
- }
I should also be clear I have not touched the menu config at all.