✅ 添加(WiFi app):添加wifi任务,可以运行
This commit is contained in:
parent
6bfd4ffdee
commit
c3143ab956
133
main/APP/app_task_wifi/app_task_wifi.c
Normal file
133
main/APP/app_task_wifi/app_task_wifi.c
Normal file
@ -0,0 +1,133 @@
|
||||
/**
|
||||
* @file app_task_wifi.c
|
||||
* @author impressionyang (impressionyang@outlook.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2022-09-29
|
||||
* _ _
|
||||
* (_)_ _ ___ _______ ___ ___ (_)__ ___ __ _____ ____ ___ _
|
||||
* / / ' \/ _ \/ __/ -_|_-<(_-</ / _ \/ _ \/ // / _ `/ _ \/ _ `/
|
||||
* /_/_/_/_/ .__/_/ \__/___/___/_/\___/_//_/\_, /\_,_/_//_/\_, /
|
||||
* /_/ /___/ /___/
|
||||
* @copyright Copyright (c) 2022 impressionyang
|
||||
*
|
||||
* @par 修改日志:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2022-09-29 <td>v1.0 <td>impressionyang <td>内容
|
||||
* </table>
|
||||
*/
|
||||
#include "app_task_wifi.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_event.h"
|
||||
|
||||
/* Set the SSID and Password via project configuration, or can set directly here */
|
||||
#define DEFAULT_SSID "1022"
|
||||
#define DEFAULT_PWD "w15077040648"
|
||||
|
||||
#if CONFIG_EXAMPLE_WIFI_ALL_CHANNEL_SCAN
|
||||
#define DEFAULT_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
|
||||
#elif CONFIG_EXAMPLE_WIFI_FAST_SCAN
|
||||
#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
|
||||
#else
|
||||
#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
|
||||
#endif /*CONFIG_EXAMPLE_SCAN_METHOD*/
|
||||
|
||||
#if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL
|
||||
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
|
||||
#elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY
|
||||
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
|
||||
#else
|
||||
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
|
||||
#endif /*CONFIG_EXAMPLE_SORT_METHOD*/
|
||||
|
||||
#if CONFIG_EXAMPLE_FAST_SCAN_THRESHOLD
|
||||
#define DEFAULT_RSSI CONFIG_EXAMPLE_FAST_SCAN_MINIMUM_SIGNAL
|
||||
#if CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_OPEN
|
||||
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
|
||||
#elif CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_WEP
|
||||
#define DEFAULT_AUTHMODE WIFI_AUTH_WEP
|
||||
#elif CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_WPA
|
||||
#define DEFAULT_AUTHMODE WIFI_AUTH_WPA_PSK
|
||||
#elif CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_WPA2
|
||||
#define DEFAULT_AUTHMODE WIFI_AUTH_WPA2_PSK
|
||||
#else
|
||||
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
|
||||
#endif
|
||||
#else
|
||||
#define DEFAULT_RSSI -127
|
||||
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
|
||||
#endif /*CONFIG_EXAMPLE_FAST_SCAN_THRESHOLD*/
|
||||
|
||||
static const char *TAG = "scan";
|
||||
|
||||
static void event_handler(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data)
|
||||
{
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||
esp_wifi_connect();
|
||||
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
esp_wifi_connect();
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
|
||||
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Initialize Wi-Fi as sta and set scan method */
|
||||
static void fast_scan(void)
|
||||
{
|
||||
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));
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, NULL));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, NULL));
|
||||
|
||||
// Initialize default station as network interface instance (esp-netif)
|
||||
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
|
||||
assert(sta_netif);
|
||||
|
||||
// Initialize and start WiFi
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
.ssid = DEFAULT_SSID,
|
||||
.password = DEFAULT_PWD,
|
||||
.scan_method = DEFAULT_SCAN_METHOD,
|
||||
.sort_method = DEFAULT_SORT_METHOD,
|
||||
.threshold.rssi = DEFAULT_RSSI,
|
||||
.threshold.authmode = DEFAULT_AUTHMODE,
|
||||
},
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
}
|
||||
|
||||
static void _app_wifi_task_run(void *param)
|
||||
{
|
||||
fast_scan();
|
||||
while(1) {
|
||||
vTaskDelay(100);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t app_task_wifi_start()
|
||||
{
|
||||
vTaskDelay(10);
|
||||
xTaskCreatePinnedToCore(_app_wifi_task_run, "wifi app task", 4096, NULL, 5, NULL, tskNO_AFFINITY);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t app_task_wifi_stop()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
36
main/APP/app_task_wifi/app_task_wifi.h
Normal file
36
main/APP/app_task_wifi/app_task_wifi.h
Normal file
@ -0,0 +1,36 @@
|
||||
/**
|
||||
* @file app_task_wifi.h
|
||||
* @author impressionyang (impressionyang@outlook.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2022-09-29
|
||||
* _ _
|
||||
* (_)_ _ ___ _______ ___ ___ (_)__ ___ __ _____ ____ ___ _
|
||||
* / / ' \/ _ \/ __/ -_|_-<(_-</ / _ \/ _ \/ // / _ `/ _ \/ _ `/
|
||||
* /_/_/_/_/ .__/_/ \__/___/___/_/\___/_//_/\_, /\_,_/_//_/\_, /
|
||||
* /_/ /___/ /___/
|
||||
* @copyright Copyright (c) 2022 impressionyang
|
||||
*
|
||||
* @par 修改日志:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2022-09-29 <td>v1.0 <td>impressionyang <td>内容
|
||||
* </table>
|
||||
*/
|
||||
#ifndef __APP_TASK_WIFI_H__
|
||||
#define __APP_TASK_WIFI_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
uint8_t app_task_wifi_start();
|
||||
|
||||
uint8_t app_task_wifi_stop();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif//__APP_TASK_WIFI_H__
|
||||
@ -2,7 +2,9 @@ idf_component_register(
|
||||
SRC_DIRS
|
||||
"."
|
||||
"APP/app_task_hello"
|
||||
"APP/app_task_wifi"
|
||||
INCLUDE_DIRS
|
||||
"."
|
||||
"APP/app_task_hello"
|
||||
"APP/app_task_wifi"
|
||||
)
|
||||
|
||||
@ -12,13 +12,31 @@
|
||||
#include "freertos/task.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_spi_flash.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "app_task_hello.h"
|
||||
#include "app_task_wifi.h"
|
||||
|
||||
static void _init_esp()
|
||||
{
|
||||
// Initialize NVS
|
||||
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 );
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
printf("Hello world!\n");
|
||||
|
||||
vTaskDelay(10);
|
||||
|
||||
_init_esp();
|
||||
app_hello_task_start();
|
||||
app_task_wifi_start();
|
||||
|
||||
/* Print chip information */
|
||||
esp_chip_info_t chip_info;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user