ESP32 app_main() loop

JJ8428
Posts: 9
Joined: Fri Jul 28, 2023 3:05 am

ESP32 app_main() loop

Postby JJ8428 » Fri Aug 04, 2023 5:10 am

This is some code I wrote for my ESP32S2. I am a bit slow since I am learning C and the ESP-IDF framework at the same time.

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()?
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <inttypes.h>
  4. #include "nvs_flash.h"
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/event_groups.h"
  7. #include "esp_event.h"
  8. #include "esp_log.h"
  9. #include "esp_err.h"
  10. #include "esp_wifi.h"
  11. #include "freertos/task.h"
  12. #include "esp_log.h"
  13. #include "sdkconfig.h"
  14.  
  15. // Define Wifi Characteristics
  16. const char* SSID = "JJ8428";
  17. const char* PWD = "Renualt88";
  18. wifi_config_t g_ap_config = {
  19.     .ap.max_connection = 1,
  20.     .ap.authmode = WIFI_AUTH_WPA2_PSK,
  21.     .ap.ftm_responder = true
  22. };
  23.  
  24. // Object to deliver Wifi events
  25. static EventGroupHandle_t s_wifi_event_group;
  26.  
  27. static const char *TAG_AP = "ftm_ap";
  28.  
  29. static void event_handler(void *arg, esp_event_base_t event_base,
  30.     int32_t event_id, void *event_data)
  31. {
  32.     if (event_id == WIFI_EVENT_AP_START) {
  33.         ESP_LOGI(TAG_AP, "SoftAP started with FTM Responder support");
  34.     } else if (event_id == WIFI_EVENT_AP_STOP) {
  35.         ESP_LOGI(TAG_AP, "SoftAP stopped"); // White LED off: Wifi SoftAP deactivated
  36.     }
  37. }
  38.  
  39. static int wifi_ap()
  40. {
  41.     strlcpy((char*) g_ap_config.ap.ssid, SSID, strlen(SSID));
  42.     strlcpy((char*) g_ap_config.ap.password, PWD, strlen(PWD));
  43.  
  44.     // SoftAP Configurations
  45.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
  46.     ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &g_ap_config));
  47.     esp_wifi_set_bandwidth(ESP_IF_WIFI_AP, WIFI_BW_HT20); // SoftAP Bandwidth: 20 MHz
  48.  
  49.     // Log an informational message indicating the start of the SoftAP configuration.
  50.     ESP_LOGI(TAG_AP, "SoftAP with the following credentials: SSID - %s, Password - %s",
  51.         SSID, PWD
  52.     );
  53.  
  54.     return true;
  55. }
  56.  
  57. void app_main(void)
  58. {
  59.     esp_err_t ret = nvs_flash_init();
  60.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  61.         ESP_ERROR_CHECK(nvs_flash_erase());
  62.         ret = nvs_flash_init();
  63.     }
  64.     ESP_ERROR_CHECK(ret);
  65.  
  66.     ESP_ERROR_CHECK(esp_netif_init());
  67.     ESP_ERROR_CHECK(esp_event_loop_create_default()); // Can be expanded on to handle gain/loss of client
  68.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  69.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  70.  
  71.     esp_event_handler_instance_t instance_any_id;
  72.     ESP_ERROR_CHECK(esp_event_handler_instance_register(
  73.             WIFI_EVENT,
  74.             ESP_EVENT_ANY_ID,
  75.             &event_handler,
  76.             NULL,
  77.             &instance_any_id
  78.         )
  79.     );
  80.  
  81.     ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  82.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
  83.     ESP_ERROR_CHECK(esp_wifi_start());
  84.  
  85.     // Start the SoftAP and keep the code looping to host the SoftAP forever
  86.     // THIS IS WHERE I AM CONFUSED
  87.     if (!wifi_ap()) {
  88.         ESP_LOGE(TAG_AP, "Failed to start SoftAP!");
  89.     } else {
  90.         while (true) {
  91.             vTaskDelay(1000 / portTICK_PERIOD_MS); // DOES THE SOFTAP CONTINUE TO EXIST IN A FTM FRIENDLY STATE? IS THE WIFI PAUSED?
  92.         }
  93.     }
  94. }
  95. [/code]

MicroController
Posts: 1734
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: ESP32 app_main() loop

Postby MicroController » Fri Aug 04, 2023 8:50 am

Yes, the WiFi/AP will remain active once it's started. The handling of WiFi happens concurrently to your application's tasks. The driver will create a new task automatically to handle WiFi which runs concurrently ("in the background") to your application's task(s) and communicates with your code via the event loop (which runs in its own task again).

You can do other things in your main task, or even exit it, without affecting WiFi.

As you noticed, you should just make sure that your main task (or any other task) does not keep using 100% of the CPU, as an empty while(true) { } loop will do, else you'll raise the anger of the watchdog.

JJ8428
Posts: 9
Joined: Fri Jul 28, 2023 3:05 am

Re: ESP32 app_main() loop

Postby JJ8428 » Sat Aug 05, 2023 2:46 pm

Thank you for your input.

Who is online

Users browsing this forum: Bing [Bot] and 206 guests