开发板购买链接
https://item.taobao.com/item.htm?spm=a2oq0.12575281.0.0.50111deb2Ij1As&ft=t&id=626366733674
开发板简介
开发环境搭建 windows
源码示例:
0_Hello Bug (ESP_LOGX与printf) 工程模板/打印调试输出
1_LED LED亮灭控制
2_LED_Task 使用任务方式控制LED
3_LEDC_PWM 使用LEDC来控制LED实现呼吸灯效果
4_ADC_LightR 使用ADC读取光敏电阻实现光照传感
5_KEY_Short_Long 按钮长按短按实现
6_TouchPad_Interrupt 电容触摸中断实现
7_WS2812_RMT 使用RMT实现RGB_LED彩虹变色示例
8_DHT11_RMT 使用RMT实现读取DHT11温湿度传感器
9_SPI_SDCard 使用SPI总线实现TF卡文件系统示例
10_IIC_ADXL345 使用IIC总线实现读取ADXL345角度加速度传感器
11_IIC_AT24C02 使用IIC总线实现小容量数据储存测试
12_IR_Rev_RMT 使用RMT实现红外遥控接收解码(NEC编码)
13_IR_Send_RMT 使用RMT实现红外数据发送(NEC编码)
14_WIFI_Scan 附近WIFI信号扫描示例
15_WIFI_AP 创建软AP示例
16_WIFI_AP_TCP_Server 在软AP模式下实现TCP服务端
17_WIFI_AP_TCP_Client 在软AP模式下实现TCP客户端
18_WIFI_AP_UDP 在软AP模式下实现UDP通讯
19_WIFI_STA 创建STA站模
20_WIFI_STA_TCP_Server 在站模式STA下实现TCP服务端
21_WIFI_STA_TCP_Client 在站模式STA下实现TCP客户端
22_WIFI_STA_UDP 在站模式STA下实现UDP通讯
23_LVGL_Test LVGL图形库简单示例
在ESP32的设计开发中,我们必然会需要使用到wifi或ble功能,今天就讲解下如何将WIFI功能纳入到ESP32中来。
在 ESP-IDF 中,整个网络协议栈包含的状态定义在头文件 components/esp32/include/esp_event.h
中,由枚举类型 system_event_id_t
定义
SYSTEM_EVENT_WIFI_READY = 0, /*!< ESP32 WiFi ready */
SYSTEM_EVENT_SCAN_DONE, /*!< ESP32 finish scanning AP */
SYSTEM_EVENT_STA_START, /*!< ESP32 station start */
SYSTEM_EVENT_STA_STOP, /*!< ESP32 station stop */
SYSTEM_EVENT_STA_CONNECTED, /*!< ESP32 station connected to AP */
SYSTEM_EVENT_STA_DISCONNECTED, /*!< ESP32 station disconnected from AP */
SYSTEM_EVENT_STA_AUTHMODE_CHANGE, /*!< the auth mode of AP connected by ESP32 station changed */
SYSTEM_EVENT_STA_GOT_IP, /*!< ESP32 station got IP from connected AP */
SYSTEM_EVENT_STA_LOST_IP, /*!< ESP32 station lost IP and the IP is reset to 0 */
SYSTEM_EVENT_STA_WPS_ER_SUCCESS, /*!< ESP32 station wps succeeds in enrollee mode */
SYSTEM_EVENT_STA_WPS_ER_FAILED, /*!< ESP32 station wps fails in enrollee mode */
SYSTEM_EVENT_STA_WPS_ER_TIMEOUT, /*!< ESP32 station wps timeout in enrollee mode */
SYSTEM_EVENT_STA_WPS_ER_PIN, /*!< ESP32 station wps pin code in enrollee mode */
SYSTEM_EVENT_STA_WPS_ER_PBC_OVERLAP, /*!< ESP32 station wps overlap in enrollee mode */
SYSTEM_EVENT_AP_START, /*!< ESP32 soft-AP start */
SYSTEM_EVENT_AP_STOP, /*!< ESP32 soft-AP stop */
SYSTEM_EVENT_AP_STACONNECTED, /*!< a station connected to ESP32 soft-AP */
SYSTEM_EVENT_AP_STADISCONNECTED, /*!< a station disconnected from ESP32 soft-AP */
SYSTEM_EVENT_AP_STAIPASSIGNED, /*!< ESP32 soft-AP assign an IP to a connected station */
SYSTEM_EVENT_AP_PROBEREQRECVED, /*!< Receive probe request packet in soft-AP interface */
SYSTEM_EVENT_GOT_IP6, /*!< ESP32 station or ap or ethernet interface v6IP addr is preferred */
SYSTEM_EVENT_ETH_START, /*!< ESP32 ethernet start */
SYSTEM_EVENT_ETH_STOP, /*!< ESP32 ethernet stop */
SYSTEM_EVENT_ETH_CONNECTED, /*!< ESP32 ethernet phy link up */
SYSTEM_EVENT_ETH_DISCONNECTED, /*!< ESP32 ethernet phy link down */
SYSTEM_EVENT_ETH_GOT_IP, /*!< ESP32 ethernet got IP from connected AP */
认证方式
WIFI_AUTH_OPEN = 0, /**< authenticate mode : open */
WIFI_AUTH_WEP, /**< authenticate mode : WEP */
WIFI_AUTH_WPA_PSK, /**< authenticate mode : WPA_PSK */
WIFI_AUTH_WPA2_PSK, /**< authenticate mode : WPA2_PSK */
WIFI_AUTH_WPA_WPA2_PSK, /**< authenticate mode : WPA_WPA2_PSK */
WIFI_AUTH_WPA2_ENTERPRISE, /**< authenticate mode : WPA2_ENTERPRISE */
WIFI_AUTH_WPA3_PSK, /**< authenticate mode : WPA3_PSK */
WIFI_AUTH_WPA2_WPA3_PSK, /**< authenticate mode : WPA2_WPA3_PSK */
事件循环库是esp提供的一种事件处理方法,而默认事件循环是用于系统事件(例如WiFi事件)的特殊循环类型,这里创建一个默认事件循环用以处理wifi连接事件
ESP_ERROR_CHECK(esp_event_loop_create_default()); // 创建默认事件循环
先引用必要头文件
// WIFI_Scan Example
#include
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_log.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include "freertos/event_groups.h"
WIFI事件处理函数
// wifi 事件
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch (event->event_id)
{
case SYSTEM_EVENT_WIFI_READY: // ESP32 WIFI就绪
ESP_LOGI(TAG, "event-->ESP32 WiFi ready");
break;
case SYSTEM_EVENT_SCAN_DONE: // 扫描AP完成
ESP_LOGI(TAG, "event-->ESP32 finish scanning AP");
break;
case SYSTEM_EVENT_STA_START: // 开始STA模式
ESP_LOGI(TAG, "event-->ESP32 station start");
break;
case SYSTEM_EVENT_STA_STOP: // 停止STA模式
ESP_LOGI(TAG, "event-->ESP32 station stop");
break;
case SYSTEM_EVENT_STA_CONNECTED: // STA模式连接到AP
ESP_LOGI(TAG, "event-->ESP32 station connected to AP");
break;
case SYSTEM_EVENT_STA_DISCONNECTED: // STA模式断开与AP的连接
ESP_LOGI(TAG, "event-->ESP32 station disconnected from AP");
break;
case SYSTEM_EVENT_STA_AUTHMODE_CHANGE: // ESP32 Station连接的AP的验证模式已更改
ESP_LOGI(TAG, "event-->the auth mode of AP connected by ESP32 station changed");
break;
case SYSTEM_EVENT_STA_GOT_IP: // ESP32 Station从连接的AP获取到IP
ESP_LOGI(TAG, "event-->ESP32 station got IP from connected AP");
break;
case SYSTEM_EVENT_STA_LOST_IP: // ESP32 Station丢失IP或IP重置
ESP_LOGI(TAG, "event-->ESP32 station lost IP and the IP is reset to 0");
break;
case SYSTEM_EVENT_STA_WPS_ER_SUCCESS: // ESP32 station wps在注册模式下成功
ESP_LOGI(TAG, "event--> ESP32 station wps succeeds in enrollee mode");
break;
case SYSTEM_EVENT_STA_WPS_ER_FAILED: // ESP32 station wps在注册模式下失败
ESP_LOGI(TAG, "event--> ESP32 station wps fails in enrollee mode");
break;
case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT: // ESP32 station wps在注册模式超时
ESP_LOGI(TAG, "event--> ESP32 station wps timeout in enrollee mode");
break;
case SYSTEM_EVENT_STA_WPS_ER_PIN: // ESP32 Station WPS密码在登记人模式下
ESP_LOGI(TAG, "event--> ESP32 station wps pin code in enrollee mode");
break;
case SYSTEM_EVENT_STA_WPS_ER_PBC_OVERLAP:// ESP32 station wps在登记人模式下重叠
ESP_LOGI(TAG, "event--> ESP32 station wps overlap in enrollee mode");
break;
case SYSTEM_EVENT_AP_START: // ESP32 AP模式开始
ESP_LOGI(TAG, "event--> ESP32 soft-AP start");
break;
case SYSTEM_EVENT_AP_STOP: // ESP32 AP模式停止
ESP_LOGI(TAG, "event--> ESP32 soft-AP stop");
break;
case SYSTEM_EVENT_AP_STACONNECTED: // ESP32 AP模式下,有站接入此AP
ESP_LOGI(TAG, "event--> a station connected to ESP32 soft-AP ");
break;
case SYSTEM_EVENT_AP_STADISCONNECTED: // ESP32 AP模式下,有站断开此AP
ESP_LOGI(TAG, "event--> a station disconnected from ESP32 soft-AP ");
break;
case SYSTEM_EVENT_AP_STAIPASSIGNED: // ESP32 AP模式下,为连接的站分配IP
ESP_LOGI(TAG, "event--> ESP32 soft-AP assign an IP to a connected station");
break;
case SYSTEM_EVENT_AP_PROBEREQRECVED: // ESP32 AP模式下,在soft-AP接口中接收探测请求数据包
ESP_LOGI(TAG, "event--> Receive probe request packet in soft-AP interface");
break;
default:
break;
}
return ESP_OK;
}
打印WIFI的认证方式
static void print_auth_mode(int authmode)
{
switch (authmode) {
case WIFI_AUTH_OPEN:
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_OPEN");
break;
case WIFI_AUTH_WEP:
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WEP");
break;
case WIFI_AUTH_WPA_PSK:
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA_PSK");
break;
case WIFI_AUTH_WPA2_PSK:
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA2_PSK");
break;
case WIFI_AUTH_WPA_WPA2_PSK:
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA_WPA2_PSK");
break;
case WIFI_AUTH_WPA2_ENTERPRISE:
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA2_ENTERPRISE");
break;
case WIFI_AUTH_WPA3_PSK:
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA3_PSK");
break;
case WIFI_AUTH_WPA2_WPA3_PSK:
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA2_WPA3_PSK");
break;
default:
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_UNKNOWN");
break;
}
}
打印WIFI的加密方式
static void print_cipher_type(int pairwise_cipher, int group_cipher)
{
switch (pairwise_cipher) {
case WIFI_CIPHER_TYPE_NONE:
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_NONE");
break;
case WIFI_CIPHER_TYPE_WEP40:
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_WEP40");
break;
case WIFI_CIPHER_TYPE_WEP104:
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_WEP104");
break;
case WIFI_CIPHER_TYPE_TKIP:
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_TKIP");
break;
case WIFI_CIPHER_TYPE_CCMP:
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_CCMP");
break;
case WIFI_CIPHER_TYPE_TKIP_CCMP:
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_TKIP_CCMP");
break;
default:
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_UNKNOWN");
break;
}
switch (group_cipher) {
case WIFI_CIPHER_TYPE_NONE:
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_NONE");
break;
case WIFI_CIPHER_TYPE_WEP40:
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_WEP40");
break;
case WIFI_CIPHER_TYPE_WEP104:
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_WEP104");
break;
case WIFI_CIPHER_TYPE_TKIP:
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_TKIP");
break;
case WIFI_CIPHER_TYPE_CCMP:
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_CCMP");
break;
case WIFI_CIPHER_TYPE_TKIP_CCMP:
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_TKIP_CCMP");
break;
default:
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_UNKNOWN");
break;
}
}
初始化WIFI 配置扫描模式 开始扫描 打印扫描到的AP信息
// 初始化WIFI 配置扫描模式 开始扫描 打印扫描到的AP信息
static void wifi_scan(void)
{
ESP_ERROR_CHECK(esp_netif_init()); // 初始化底层TCP/IP堆栈。在应用程序启动时,应该调用此函数一次
ESP_ERROR_CHECK(esp_event_loop_create_default()); // 创建默认事件循环
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();// 创建一个默认的WIFI-STA网络接口,
assert(sta_netif); // 如果初始化错误,此API将中止
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); // 使用默认wifi初始化配置
uint16_t number = DEFAULT_SCAN_LIST_SIZE; // 默认扫描列表大小
wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE]; // AP信息结构体大小
uint16_t ap_count = 0; // 初始化AP数量0
memset(ap_info, 0, sizeof(ap_info)); // 初始化AP信息结构体
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); // 设置WiFi的工作模式为 STA
ESP_ERROR_CHECK(esp_wifi_start()); // 启动WiFi连接
ESP_ERROR_CHECK(esp_wifi_scan_start(NULL, true)); // 开始扫描WIFI(默认配置,阻塞方式)
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));// 获取搜索的具体AP信息
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, "SSID \t\t%s", ap_info[i].ssid); // 打印WIFI名称
ESP_LOGI(TAG, "RSSI \t\t%d", ap_info[i].rssi); // 打印信号强度
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);
}
}
主函数
// 主函数
void app_main(void)
{
ESP_LOGI(TAG, "APP Start......");
// 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 );
wifi_scan();
}
打开ESP-IDF Command Prompt
cd命令进入此工程目录
cd F:\ESP32_DevBoard_File\14_WIFI_Scan
查看电脑设备管理器中开发板的串口号
执行idf.py -p COM9 flash monitor从串口9下载并运行打开口显示设备调试信息 Ctrl+c退出运行