You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ESP-IDF问题:vTaskDelay()调用后STA管理任务停止运行

问题描述

我正在构建一个运行APSTA模式的ESP系统,用户可通过向URL提交JSON字符串,控制ESP连接/断开其他AP(如路由器)。操作完成后,布尔值_STA_START会被设为true,登录参数存入_STA_WIFI_SSID和_STA_WIFI_PASS。

编写STA连接管理任务时遇到问题:调用vTaskDelay()后,vTask_Manage_STA_Connection()代码停止运行;删除该语句后,代码会快速循环导致栈溢出并复位ESP。目前监控显示该任务仅运行一次。start_Manage_STA_Connection()是在初始化ESP为AP后于main.c中调用的。


相关代码

Task.h

#ifndef INCLUDE
#define INCLUDE
#include "config.h"
#endif

#ifndef INCLUDE_WLAN_STA
#define INCLUDE_WLAN_STA
#include "WLAN_STA.h"
#endif

 // Dimensions the buffer that the task being created will use as its stack.
 // NOTE:  This is the number of words the stack will hold, not the number of
 // bytes.  For example, if each stack item is 32-bits, and this is set to 100,
 // then 400 bytes (100 * 32-bits) will be allocated.
#define STACK_SIZE 2048


/// @brief Starts a Task to Manage the STA Connection 
/// @param pvParameters 
void vTask_Manage_STA_Connection( void * pvParameters );

/// @brief Calls vTask_Manage_STA_Connection in main.c
/// @param  
void start_Manage_STA_Connection( void );

Tasks.c

#include "Tasks.h"

static const char *TAG_Manage_STA = "Manage STA Connection";

// Task to be created.
void vTask_Manage_STA_Connection(void *pvParameters)
{
    for (;;)
    {
        // Task code goes here.

        if (_STA_START == true)
        {
            ESP_LOGI(TAG_Manage_STA, "Connecting to Station...");

            wifi_init_sta(
                _STA_WIFI_SSID,
                _STA_WIFI_PASS);

            _STA_START = false;
        }

        ESP_LOGI(TAG_Manage_STA, "Managing STA Connection...");

        vTaskDelay(100/portTICK_PERIOD_MS);
    }
}

// Function that creates a task.
void start_Manage_STA_Connection(void)
{
    static uint8_t ucParameterToPass;
    TaskHandle_t xHandle = NULL;

    // Create the task pinned to core 0, storing the handle.  Note that the passed parameter ucParameterToPass
    // must exist for the lifetime of the task, so in this case is declared static.  If it was just an
    // an automatic stack variable it might no longer exist, or at least have been corrupted, by the time
    // the new task attempts to access it.
    xTaskCreatePinnedToCore(vTask_Manage_STA_Connection,
                            "Manage_STA_Connection",
                            STACK_SIZE,
                            &ucParameterToPass,
                            (configMAX_PRIORITIES-1),
                            &xHandle, 0);

    configASSERT(xHandle);

    // Use the handle to delete the task.
    if (xHandle != NULL)
    {
        vTaskDelete(xHandle);
    }
}

监控输出

I (0) cpu_start: App cpu up.
I (388) cpu_start: Pro cpu start user code
I (388) cpu_start: cpu freq: 160000000
I (388) cpu_start: Application information:
I (392) cpu_start: Project name:     wifi_softAP
I (398) cpu_start: App version:      1
I (402) cpu_start: Compile time:     Dec  5 2022 11:50:36
I (408) cpu_start: ELF file SHA256:  3597702b82953470...
I (414) cpu_start: ESP-IDF:          v4.4.2
I (419) heap_init: Initializing. RAM available for dynamic allocation:
I (426) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (432) heap_init: At 3FFB7508 len 00028AF8 (162 KiB): DRAM
I (438) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (445) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (451) heap_init: At 400944E0 len 0000BB20 (46 KiB): IRAM
I (459) spi_flash: detected chip: gd
I (462) spi_flash: flash io: dio
I (467) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (559) MAIN: ESP_WIFI_MODE_AP/STA
I (569) WLAN AP: Wifi Loop Started
I (579) wifi:wifi driver task: 3ffc0378, prio:23, stack:6656, core=0
I (579) system_api: Base MAC address is not set
I (579) system_api: read default base MAC address from EFUSE
I (609) wifi:wifi firmware version: eeaa27d
I (609) wifi:wifi certification version: v7.0
I (609) wifi:config NVS flash: enabled
I (609) wifi:config nano formating: disabled
I (609) wifi:Init data frame dynamic rx buffer num: 32
I (619) wifi:Init management frame dynamic rx buffer num: 32
I (619) wifi:Init management short buffer num: 32
I (629) wifi:Init dynamic tx buffer num: 32
I (629) wifi:Init static rx buffer size: 1600
I (629) wifi:Init static rx buffer num: 10
I (639) wifi:Init dynamic rx buffer num: 32
I (639) wifi_init: rx ba win: 6
I (649) wifi_init: tcpip mbox: 32
I (649) wifi_init: udp mbox: 6
I (649) wifi_init: tcp mbox: 6
I (659) wifi_init: tcp tx win: 5744
I (659) wifi_init: tcp rx win: 5744
I (669) wifi_init: tcp mss: 1440
I (669) wifi_init: WiFi IRAM OP enabled
I (669) wifi_init: WiFi RX IRAM OP enabled
I (689) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07
I (789) wifi:mode : sta (30:ae:a4:80:4e:88) + softAP (30:ae:a4:80:4e:89)
I (789) wifi:enable tsf
I (789) wifi:Total power save buffer number: 16
I (789) wifi:Init max length of beacon: 752/752
I (789) wifi:Init max length of beacon: 752/752
I (799) WLAN AP: Error: Unknown AP Exception
I (799) WLAN AP: wifi_init_softap finished. SSID:myssid password:mypassword channel:4
I (799) WLAN AP: Access point started!
I (819) Manage STA Connection: Managing STA Connection...
I (91529) wifi:new:<1,1>, old:<1,1>, ap:<1,1>, sta:<0,0>, prof:1
I (91529) wifi:station: 1c:bf:ce:ca:79:de join, AID=1, bgn, 40U
I (91549) WLAN AP: station 1c:bf:ce:ca:79:de join, AID=1
I (91559) esp_netif_lwip: DHCP server assigned IP to a station, IP is: 192.168.5.2
I (91559) AP Webserver: Starting webserver
I (91559) AP Webserver: Starting server on port: '80'
I (91569) AP Webserver: Registering URI handlers
W (95189) wifi:<ba-add>idx:2 (ifx:1, 1c:bf:ce:ca:79:de), tid:0, ssn:190, winSize:64

内容的提问来源于stack exchange,提问作者Emre Mutlu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 01:35:18