#include
#include
#include
#include "nvs_flash.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "esp_log.h"
#include "wifi.h"
static const char *TAG = "MAIN";
void app_main(void)
{
ESP_LOGI(TAG, "[APP] Startup..");
ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
esp_log_level_set("*", ESP_LOG_INFO);
ESP_ERROR_CHECK(nvs_flash_init());
//wifi_sta_init();
// wifi_ap_init();
wifi_apsta_init();
while(1)
{
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
#include "wifi.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "stdio.h"
#include "string.h"
#define WIFI_STA_SSID "***********" // STA模式WIFI名称
#define WIFI_STA_PASS "***********" // STA模式WIFI密码
#define WIFI_AP_SSID "***********"// AP模式WIFI名称
#define WIFI_AP_PASS "***********"// AP模式WIFI密码
static const char *TAG = "WIFI";
/*************************************************************************************************************/
static void wifi_sta_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
switch (event_id)
{
case WIFI_EVENT_STA_START:
{
ESP_LOGI(TAG, "Connecting to %s", WIFI_STA_SSID);
esp_wifi_connect();
break;
}
case WIFI_EVENT_STA_CONNECTED:
{
ESP_LOGI(TAG, "Connected to %s", WIFI_STA_SSID);
break;
}
case WIFI_EVENT_STA_DISCONNECTED:
{
ESP_LOGI(TAG, "Disconnected from %s", WIFI_STA_SSID);
esp_wifi_connect();
break;
}
case IP_EVENT_STA_GOT_IP:
{
ESP_LOGI(TAG, "Got IP address");
break;
}
default:
{
break;
}
}
}
void wifi_sta_init(void)
{
wifi_config_t wifi_config = {// 配置WiFi连接信息
.sta = {
.ssid = WIFI_STA_SSID,
.password = WIFI_STA_PASS,
},
};
tcpip_adapter_init();// 初始化TCP/IP协议栈
ESP_ERROR_CHECK(esp_event_loop_create_default());// 初始化WiFi事件处理程序
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();// 配置WiFi连接参数
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_sta_event_handler, NULL));// 注册WiFi事件处理程序
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_sta_event_handler, NULL));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));// 设置WiFi工作模式为Station模式
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));// 配置Wi-Fi STA
ESP_ERROR_CHECK(esp_wifi_start());// 启动WiFi
}
/*************************************************************************************************************/
void wifi_ap_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
switch (event_id)
{
case IP_EVENT_STA_GOT_IP: {ESP_LOGI(TAG, "Got IP address"); break;}
case IP_EVENT_STA_LOST_IP: {ESP_LOGI(TAG,"AP stop"); break;}
case IP_EVENT_AP_STAIPASSIGNED: {ESP_LOGI(TAG,"AP stop"); break;}
default: {break;}
}
}
void wifi_ap_init(void)
{
// 配置Wi-Fi AP配置
wifi_config_t wifi_config = {
.ap = {
.ssid = WIFI_AP_SSID,
.ssid_len = strlen(WIFI_AP_SSID),
.password = WIFI_AP_PASS,
.max_connection = 4,
.authmode = WIFI_AUTH_WPA_WPA2_PSK
},
};
tcpip_adapter_init();// 初始化TCP/IP协议栈
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_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_ap_event_handler, NULL));// 初始化WiFi事件处理程序
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));// 设置Wi-Fi工作模式为AP模式
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));// 配置Wi-Fi AP
ESP_ERROR_CHECK(esp_wifi_start());
}
/*************************************************************************************************************/
void wifi_apsta_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
if( event_base == WIFI_EVENT )
{
switch (event_id)
{
case WIFI_EVENT_AP_START:
{
ESP_LOGI(TAG, "WiFi AP started");
break;
}
case WIFI_EVENT_AP_STOP:
{
ESP_LOGI(TAG, "WiFi AP stopped");
break;
}
case WIFI_EVENT_STA_START:
{
ESP_LOGI(TAG, "Connecting to %s", WIFI_STA_SSID);
esp_wifi_connect();
break;
}
case WIFI_EVENT_STA_CONNECTED:
{
ESP_LOGI(TAG, "Connected to %s", WIFI_STA_SSID);
break;
}
case WIFI_EVENT_STA_DISCONNECTED:
{
ESP_LOGI(TAG, "Disconnected from %s", WIFI_STA_SSID);
esp_wifi_connect();
break;
}
default:
break;
}
}
else if( event_base == IP_EVENT )
{
switch (event_id)
{
case IP_EVENT_STA_GOT_IP: {ESP_LOGI(TAG, "Got IP address"); break;}
case IP_EVENT_STA_LOST_IP: {ESP_LOGI(TAG,"STA stop"); break;}
case IP_EVENT_AP_STAIPASSIGNED: {ESP_LOGI(TAG,"AP stop"); break;}
default: {break;}
}
}
}
void wifi_apsta_init(void)
{
wifi_config_t wifi_ap_config = {
.ap = {
.ssid = WIFI_AP_SSID,
.ssid_len = strlen(WIFI_AP_SSID),
.password = WIFI_AP_PASS,
.max_connection = 4,
.authmode = WIFI_AUTH_WPA_WPA2_PSK
},
};
wifi_config_t wifi_sta_config = {// 配置WiFi连接信息
.sta = {
.ssid = WIFI_STA_SSID,
.password = WIFI_STA_PASS,
},
};
tcpip_adapter_init();// 初始化TCP/IP协议栈
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_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_apsta_event_handler, NULL));// 初始化WiFi事件处理程序
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_apsta_event_handler, NULL));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));// 设置Wi-Fi工作模式为APSTA模式
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_ap_config));// 配置Wi-Fi AP
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_sta_config));// 配置Wi-Fi STA
ESP_ERROR_CHECK(esp_wifi_start());
}
吾辈必当勤勉,持书仗剑耀中华。