To test this, I'm doing a simple OTA that changes the LED blink interval.
However, I'm facing a panic issue due to a stack overflow. I think the problem is caused by having too much code and trying to implement too many features inside a single task.
I would appreciate it if you could give me some suggestions for improvement.
To resolve (or at least confirm) this issue, I tried to find the overflow checking menu, but I couldn’t locate it.
I also couldn't find a way to enable the Extra options menu in FreeRTOS.
I'm currently using version 5.4.2.
I'm a complete beginner, so I'm not familiar with these tools and also not confident in English. I hope you can understand.
Code: Select all
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "nvs_flash.h"
#include "esp_https_ota.h"
#include "esp_http_client.h"
#define LED_GPIO 7
#define WIFI_SSID ""
#define WIFI_PASS ""
#define OTA_BIN_URL ""
static const char *TAG = "OTA_APP";
static volatile bool is_ota_running = false;
static void init_led()
{
gpio_config_t io_conf = {
.pin_bit_mask = 1ULL << LED_GPIO,
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE};
gpio_config(&io_conf);
}
static void blink_led_task(void *arg)
{
while (1)
{
gpio_set_level(LED_GPIO, 1);
vTaskDelay(pdMS_TO_TICKS(is_ota_running ? 100 : 1000));
gpio_set_level(LED_GPIO, 0);
vTaskDelay(pdMS_TO_TICKS(is_ota_running ? 100 : 1000));
}
}
static void wifi_connect()
{
static bool wifi_initialized = false;
if (!wifi_initialized)
{
esp_netif_init();
esp_event_loop_create_default();
esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg);
wifi_initialized = true;
}
wifi_config_t wifi_config = {
.sta = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
},
};
esp_wifi_set_mode(WIFI_MODE_STA);
esp_wifi_set_config(WIFI_IF_STA, &wifi_config);
esp_wifi_start();
esp_wifi_connect();
vTaskDelay(pdMS_TO_TICKS(5000)); // 연결 대기
}
static void perform_ota()
{
esp_http_client_config_t http_cfg = {
.url = OTA_BIN_URL,
};
esp_https_ota_config_t ota_cfg = {
.http_config = &http_cfg,
};
ESP_LOGI(TAG, "Starting OTA...");
if (esp_https_ota(&ota_cfg) == ESP_OK)
{
ESP_LOGI(TAG, "OTA successful. Rebooting...");
esp_restart();
}
else
{
ESP_LOGE(TAG, "OTA failed.");
}
}
static void ota_timer_callback(TimerHandle_t xTimer)
{
if (is_ota_running)
{
ESP_LOGW(TAG, "OTA already running. Skipping.");
return;
}
is_ota_running = true;
wifi_connect();
perform_ota();
esp_wifi_stop();
is_ota_running = false;
}
void app_main()
{
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
nvs_flash_erase();
nvs_flash_init();
}
init_led();
xTaskCreate(&blink_led_task, "blink_led_task", 2048, NULL, 5, NULL);
TimerHandle_t ota_timer = xTimerCreate("ota_timer", pdMS_TO_TICKS(120000), pdTRUE, NULL, ota_timer_callback);
xTimerStart(ota_timer, 0);
}