一、概述
esp32-s3 模块自带wifi模块、这就使得在很多室内项目里面应用中,esps3模块拥有一种自带的联网外设,在项目使用和平时学习中都节约了一定的成本。以下是esp的wifi模块的三种模式:
STA 模式(即 STA 模式或 Wi-Fi 客户端模式),此时 ESP32-S3 连接到接入点 (AP)。
AP 模式(即 Soft-AP 模式或接入点模式),此时基站连接到 ESP32-S3。
AP-STA 共存模式(ESP32-S3 既是接入点,同时又作为基站连接到另外一个接入点)。
本文主要讲的是wifi的基础扫描功能和sta模式的使用,即通过作为联网设备,在知道其他联网WIFI的名称和密码下,链接到其他设备,实现联网功能。
二、sta模式使用
(一)扫描功能使用
#1、初始化wifi后开始扫描
#include "modem_wifi.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "string.h"
#include "stdio.h"
#include "esp_mac.h"
#define DEFAULT_SCAN_LIST_SIZE 10
static wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
static uint16_t ap_count = 0;
void wifiInitScan(){
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
uint16_t number = DEFAULT_SCAN_LIST_SIZE;
memset(ap_info, 0, sizeof(ap_info));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
esp_wifi_scan_start(NULL, true);
vTaskDelay(5000 / portTICK_PERIOD_MS); //等待扫描
}
2、打印扫描附近的wifi的ap信息
void getWifiListBssid(int i,char *bssid){
uint16_t number = DEFAULT_SCAN_LIST_SIZE;
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
ESP_LOGI(TAG, "Total APs scanned = %u", ap_count);
for (int i = 0; (i < DEFAULT_SCAN_LIST_SIZE) && (i < ap_count); i++) {
ESP_LOGI(TAG, "bssid \t\t(%02x:%02x:%02x:%02x:%02x:%02x)", MAC2STR(ap_info[i].bssid));
print_auth_mode(ap_info[i].authmode);
if (ap_info[i].authmode != wifi_AUTH_WEP) {
print_cipher_type(ap_info[i].pairwise_cipher, ap_info[i].group_cipher);
}
ESP_LOGI(TAG, "Channel \t\t%d\n", ap_info[i].primary);
}
}
3、关闭wifi
void wifiCloseScan(void)
{
memset(ap_info,0,sizeof(ap_info));
esp_wifi_stop();
esp_wifi_deinit();
}
(二)sta模式使用
1、初始化网卡和默认事件
void netif_init(){
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
}
2、初始化wifi的sta功能
```c
``c
#define DEFAULT_SSID "esp32s3" //链接wifi名称
#define DEFAULT_PWD "123465" //wifi密码
#define DEFAULT_RSSI -127
static esp_netif_t *sta_netif =NULL;
static EventGroupHandle_t wifi_event;
#define WIFICONFIG() { \
.sta = { \
.ssid = DEFAULT_SSID, \
.password = DEFAULT_PWD, \
.scan_method = WIFI_FAST_SCAN,\
.sort_method = WIFI_CONNECT_AP_BY_SIGNAL,\
.threshold.rssi = DEFAULT_RSSI,\
.threshold.authmode = WIFI_AUTH_OPEN,\
},\
}
void wifi_init(){
sta_netif= esp_netif_create_default_wifi_sta();
assert(sta_netif);
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL) );
ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL) );
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = WIFICONFIG();
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start());
wifi_event= xEventGroupCreate(); //创建一个事件标志组
xEventGroupWaitBits(wifi_event, CONNECT_DONE_BIT, 0, 1, pdMS_TO_TICKS(2*60*1000)); //等待链接成功后、ip生成
}
3、编写回调函数
static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
ESP_LOGI(TAG, "event_base=%s event_id=%d",event_base,event_id);
if (event_base == WIFI_EVENT&&event_id==WIFI_EVENT_STA_START){
ESP_LOGI(TAG, "wifi_EVENT_STA_START");
ESP_ERROR_CHECK(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;
xEventGroupSetBits(wifi_event, CONNECT_DONE_BIT);
}
}
4、关闭wifi
void wifiDeInit(void){
esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler) ;
esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler);
esp_wifi_stop();
esp_wifi_deinit();
esp_netif_destroy_default_wifi(sta_netif);
}
三、总结
本次分享到这里,下次会分享wifi的AP模式和AP-STA 共存模式的使用。本人是在一线开发工作了几年的嵌入式应用工程师,利用休息闲暇时间无偿分享这几年工作和学习中项目遇到的一些亮点,随缘更新,希望大家喜欢。
参考:
https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32s3/api-guides/memory-types.html#iram-ram
https://www.espressif.com.cn/sites/default/files/documentation/esp32-s3_technical_reference_manual_cn.pdf