1. AP(接入点)模式:
可以理解为一个无法访问外网的局域网WIFI路由器节点,可以接受各类设备的连接请求,并且可以与连接的设备进行TCP,UDP连接,实现数据流。
实现步骤:
- 初始化flash(我也不太明白,可能是flash中有关于wifi的配置吧)
- 初始化LwIP
- 创建系统事件任务并初始化应用程序事件的回调函数
- 创建具有TCP / IP堆栈的默认网络接口实例绑定AP
- 初始化WIFI
- 配置WIFI
- 启动WIFI
#include <stdio.h>
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_err.h"
#include "nvs_flash.h"
#include "esp_event.h"
static void event_hander(void* event_handler_arg,
esp_event_base_t event_base,
int32_t event_id,
void* event_data)
{
switch (event_id)
{
case SYSTEM_EVENT_AP_START:
printf("\nwifi_softap_start\n");
break;
case SYSTEM_EVENT_AP_STACONNECTED:
printf("\nwifi_softap_connectted\n");
break;
case SYSTEM_EVENT_AP_STADISCONNECTED:
printf("\nwifi_softap_disconnectted\n");
break;
default:
break;
}
}
void app_main(void) {
ESP_ERROR_CHECK( nvs_flash_init() );
esp_netif_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_ap();
wifi_init_config_t wifi_init = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&wifi_init));
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,ESP_EVENT_ANY_ID,&event_hander,NULL,NULL));
wifi_config_t wificonfig = {
.ap = {
.ssid = "esp32",
.ssid_len = 0,
.max_connection = 1,
.password = "12345678",
.authmode = WIFI_AUTH_WPA_WPA2_PSK,
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP,&wificonfig));
ESP_ERROR_CHECK(esp_wifi_start());
}
以下为关于WIFI的事件
SYSTEM_EVENT_WIFI_READY = 0,
SYSTEM_EVENT_SCAN_DONE,
SYSTEM_EVENT_STA_START,
SYSTEM_EVENT_STA_STOP,
SYSTEM_EVENT_STA_CONNECTED,
SYSTEM_EVENT_STA_DISCONNECTED,
SYSTEM_EVENT_STA_AUTHMODE_CHANGE,
SYSTEM_EVENT_STA_GOT_IP,
SYSTEM_EVENT_STA_LOST_IP,
SYSTEM_EVENT_STA_WPS_ER_SUCCESS,
SYSTEM_EVENT_STA_WPS_ER_FAILED,
SYSTEM_EVENT_STA_WPS_ER_TIMEOUT,
SYSTEM_EVENT_STA_WPS_ER_PIN,
SYSTEM_EVENT_AP_START,
SYSTEM_EVENT_AP_STOP,
SYSTEM_EVENT_AP_STACONNECTED,
SYSTEM_EVENT_AP_STADISCONNECTED,
SYSTEM_EVENT_AP_PROBEREQRECVED,
SYSTEM_EVENT_GOT_IP6,
SYSTEM_EVENT_ETH_START,
SYSTEM_EVENT_ETH_STOP,
SYSTEM_EVENT_ETH_CONNECTED,
SYSTEM_EVENT_ETH_DISCONNECTED,
SYSTEM_EVENT_ETH_GOT_IP,
2.STA(基站)模式
将ESP32连接至附近的AP
实现步骤:
- 初始化flash(我也不太明白,可能是flash中有关于wifi的配置吧)
- 初始化LwIP
- 创建系统事件任务并初始化应用程序事件的回调函数
- 创建具有TCP / IP堆栈的默认网络接口实例绑定AP
- 初始化WIFI
- 配置WIFI
- 启动WIFI
void event_hander(void* event_handler_arg,
esp_event_base_t event_base,
int32_t event_id,
void* event_data)
{
ip_event_got_ip_t *ip = event_data;
if(event_base == WIFI_EVENT){
switch (event_id)
{
case WIFI_EVENT_STA_START:
esp_wifi_connect();
printf("\nwifi_sta_start\n");
break;
case WIFI_EVENT_STA_DISCONNECTED:
printf("\nwifi_sta_fail\n");
esp_wifi_connect();
break;
}
}else if (event_base == IP_EVENT)
{
if(event_id == IP_EVENT_STA_GOT_IP){
printf("content : "IPSTR"\n" , IP2STR(&ip->ip_info.ip));
}
}
}
void app_main(void) {
ESP_ERROR_CHECK( nvs_flash_init() );
esp_netif_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
wifi_init_config_t wifi_init = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&wifi_init));
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,ESP_EVENT_ANY_ID,&event_hander,NULL,NULL));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,IP_EVENT_STA_GOT_IP,&event_hander,NULL,NULL));
wifi_config_t wificonfig = {
.ap = {
.ssid = "dream",
.password = "88886666",
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA,&wificonfig));
ESP_ERROR_CHECK(esp_wifi_start());
}
|