The purpose of this code is supposed to a Wifi FTM Responder. I plan on writing code for the Wifi FTM Initiator that checks the distance every quarter second in the future.
In my app_main() function, I am calling wifi_ap() near the end. Then I have a while loop with vTaskDelay() command for 1 full second. I know that the vTaskDelay() code is halting the code for a full second, but my question is does the SoftAP (this is created/registered in the wifi_ap() function) continue to exist during that halt? If so, can some please suggest a different way to loop the app_main() near the end where the SoftAP continues to exist?
I have also tried just doing while(1); (There is no vTaskDelay), but the "Task Watchdog Error" appears, so I think I need the vTaskDelay()?
- #include <stdio.h>
- #include <string.h>
- #include <inttypes.h>
- #include "nvs_flash.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 "freertos/task.h"
- #include "esp_log.h"
- #include "sdkconfig.h"
- // Define Wifi Characteristics
- 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
- };
- // Object to deliver Wifi events
- static EventGroupHandle_t s_wifi_event_group;
- 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"); // White LED off: Wifi SoftAP deactivated
- }
- }
- static int wifi_ap()
- {
- strlcpy((char*) g_ap_config.ap.ssid, SSID, strlen(SSID));
- strlcpy((char*) g_ap_config.ap.password, PWD, strlen(PWD));
- // SoftAP 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_HT20); // SoftAP Bandwidth: 20 MHz
- // Log an informational message indicating the start of the SoftAP configuration.
- ESP_LOGI(TAG_AP, "SoftAP with the following credentials: SSID - %s, Password - %s",
- SSID, PWD
- );
- return true;
- }
- 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_ERROR_CHECK(esp_netif_init());
- ESP_ERROR_CHECK(esp_event_loop_create_default()); // Can be expanded on to handle gain/loss of client
- 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());
- // Start the SoftAP and keep the code looping to host the SoftAP forever
- // THIS IS WHERE I AM CONFUSED
- if (!wifi_ap()) {
- ESP_LOGE(TAG_AP, "Failed to start SoftAP!");
- } else {
- while (true) {
- vTaskDelay(1000 / portTICK_PERIOD_MS); // DOES THE SOFTAP CONTINUE TO EXIST IN A FTM FRIENDLY STATE? IS THE WIFI PAUSED?
- }
- }
- }
- [/code]