在ESP32开发中,或多或少会看见有的工程添加的是ESP-WIFI库有的添加的是WIFI库。特意去查找了下,两者都是可以开发esp32/esp8266的WIFI功能。
两者的区别:
Esp-wifi库:
硬件平台:这个库是Espressif提供的专门为esp32开发wifi的库,是ESP-IDF(Espressif IoT Development Framework)的一部分,ESP-IDF是ESP32的官方开发框架。
功能和控制:esp-wifi库提供了更底层的控制和高级功能。
Wifi库:
硬件平台:这个库通常是Arduio框架下的一个库。
功能和控制:WiFi库提供了简化的API,使得WiFi连接变得更加容易。这对于初学者或者只需要进行基本WiFi连接的项目来说是非常方便的。WiFi库隐藏了底层的细节,使得连接到WiFi网络变得简单。
总结:
在实际应用中,如果是学习和做一些要求高一点的开发最好是用esp-wifi库,做一些简单的开发还是WIFI库香。
参考资料:
https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32/api-reference/network/esp_wifi.html
ESP32系列--第四篇 WiFi概述_esp_wifi_set_config-CSDN博客
1、ESP32 WiFi介绍
WIFI库支持配置及监控ESP32 Wi-Fi连网功能。有三种工作模式:station模式、AP模式、station/AP共存模式。
Station模式(STA模式或者wifi客户端模式):ESP32作为客户端连接路由器与接入该路由去的设备进行通信。
AP模式(Soft-AP模式或者接入点模式):ESP32做为服务器,创建一个网络(独立于互联网之外)等待其它设备接入该热点进行通信。
Station/AP共存模式:兼容了以上两种功能。ESP32接入网络可进行网络通信,又可以创建一个网络等待其它设备连接实现通信。
2、WiFi的启动(STA、AP、STA/AP)
2.1 STA模式
STA模式用于使ESP32模块连接到由接入点(AP)创建的WiFi中。在此模式下,模块相当于一个客户端,可以连接其他路由器发出的WiFi信号。
连接网络的基本步骤:
1、引入WiFi库
2、调用API函数配置WiFi工作模式,和相关IP
3、连接wifi和启动wifi。
2.2 AP模式
此模式下,模块相当于一个路由器,其它设备可以连接到该模块发送信号。主要应用在主从设备的主机部分。
创建AP网络的基本步骤:
1、引入WiFi库
2、调用API函数配置WiFi工作模式(AP),wifi名和密码。
3、启动AP
2.3 STA-AP共存模式
在此模式下,模块既可以作为STA客户端模式,又可以作为服务器模式。可以让其它设备进入此设备也可以接入路由器或者上位机服务器进行数据上传。
2.4 配置WiFi的基础步骤
esp_wifi_init(&cfg) # 初始化wifi
esp_wifi_set_config() # 设置wifi
esp_wifi_set_mode() # 模式设置
esp_wifi_start(); # wifi启动
3、API简介
这里说的API是esp-wifi库,也就参考资料里面的东西。
1.esp_err_t esp_wifi_init(const wifi_init_config_t *config)
函数功能:如果要使用esp32的wifi功能,这个API必须是第一个被调用的,这个函数的功能是为WIFI驱动分配资源。
输入参数:config:指向wifi初始化配置结构体的指针。
返回参数:有返回值
2.esp_err_t esp_wifi_deinit(void)
函数功能:释放esp_wifi_init中分配的所有资源,停止wifi任务。
输入参数:无
返回值 :有返回值
3. esp_err_t esp_wifi_set_mode(wifi_mode_t mode)
函数功能:设置wifi模式,STA、AP、STA/AP
输入参数:mode输入wifi模式
返回值 :有返回值
4. esp_err_t esp_wifi_get_mode(wifi_mode_t *mode)
函数功能:获取wifi当前模式,STA、AP、STA/AP
输入参数:*mode接收wifi当前模式
返回值 :有返回值
5. esp_err_t esp_wifi_start(void)
函数功能:根据当前配置启动wifi,如果配置的是STA则创建站控制块并启动站;如果配置的是AP则创建AP控制块并启动AP。如果模式为WIFI_MODE_APSTA,则创建软ap和站控制块并启动软ap;如果模式为WIFI_MODE_NAN,则创建NAN控制块并启动NAN。
输入参数:无
返回值 :有返回值
6. esp_err_t esp_wifi_stop(void)
函数功能:停止wifi运行并销毁一切wifi资源
输入参数:无
返回值 :有返回值
7. esp_err_t esp_wifi_restore(void)
函数功能:恢复wifi堆栈并把之前配置设置为默认
输入参数:无
返回值 :有返回值
8. esp_err_t esp_wifi_connect(void)
函数功能:连接热点,此功能只在STA或者STA/AP模式下生效
输入参数:无
返回值 :有返回值
9. esp_err_t esp_wifi_disconnect(void)
函数功能:断开连接
输入参数:无
返回值 :有返回值
10. esp_err_t esp_wifi_set_config(wifi_interface_t interface, wifi_config_t *conf);
函数功能:设置wifi配置
输入参数:interface结构体和* conf
返回值 :有返回值
针对:interface结构体和*conf两个的说明
/** Soft-AP configuration settings for the ESP32 */
typedef struct {
uint8_t ssid[32]; /**< SSID of ESP32 soft-AP. If ssid_len field is 0, this must be a Null terminated string. Otherwise, length is set according to ssid_len. */
uint8_t password[64]; /**< Password of ESP32 soft-AP. */
uint8_t ssid_len; /**< Optional length of SSID field. */
uint8_t channel; /**< Channel of ESP32 soft-AP */
wifi_auth_mode_t authmode; /**< Auth mode of ESP32 soft-AP. Do not support AUTH_WEP in soft-AP mode */
uint8_t ssid_hidden; /**< Broadcast SSID or not, default 0, broadcast the SSID */
uint8_t max_connection; /**< Max number of stations allowed to connect in, default 4, max 10 */
uint16_t beacon_interval; /**< Beacon interval which should be multiples of 100. Unit: TU(time unit, 1 TU = 1024 us). Range: 100 ~ 60000. Default value: 100 */
wifi_cipher_type_t pairwise_cipher; /**< pairwise cipher of SoftAP, group cipher will be derived using this. cipher values are valid starting from WIFI_CIPHER_TYPE_TKIP, enum values before that will be considered as invalid and default cipher suites(TKIP+CCMP) will be used. Valid cipher suites in softAP mode are WIFI_CIPHER_TYPE_TKIP, WIFI_CIPHER_TYPE_CCMP and WIFI_CIPHER_TYPE_TKIP_CCMP. */
bool ftm_responder; /**< Enable FTM Responder mode */
wifi_pmf_config_t pmf_cfg; /**< Configuration for Protected Management Frame */
} wifi_ap_config_t;
/** STA configuration settings for the ESP32 */
typedef struct {
uint8_t ssid[32]; /**< SSID of target AP. */
uint8_t password[64]; /**< Password of target AP. */
wifi_scan_method_t scan_method; /**< do all channel scan or fast scan */
bool bssid_set; /**< whether set MAC address of target AP or not. Generally, station_config.bssid_set needs to be 0; and it needs to be 1 only when users need to check the MAC address of the AP.*/
uint8_t bssid[6]; /**< MAC address of target AP*/
uint8_t channel; /**< channel of target AP. Set to 1~13 to scan starting from the specified channel before connecting to AP. If the channel of AP is unknown, set it to 0.*/
uint16_t listen_interval; /**< Listen interval for ESP32 station to receive beacon when WIFI_PS_MAX_MODEM is set. Units: AP beacon intervals. Defaults to 3 if set to 0. */
wifi_sort_method_t sort_method; /**< sort the connect AP in the list by rssi or security mode */
wifi_scan_threshold_t threshold; /**< When sort_method is set, only APs which have an auth mode that is more secure than the selected auth mode and a signal stronger than the minimum RSSI will be used. */
wifi_pmf_config_t pmf_cfg; /**< Configuration for Protected Management Frame. Will be advertized in RSN Capabilities in RSN IE. */
uint32_t rm_enabled:1; /**< Whether Radio Measurements are enabled for the connection */
uint32_t btm_enabled:1; /**< Whether BSS Transition Management is enabled for the connection */
uint32_t mbo_enabled:1; /**< Whether MBO is enabled for the connection */
uint32_t reserved:29; /**< Reserved for future feature set */
} wifi_sta_config_t;
/** Configuration data for ESP32 AP or STA.
*
* The usage of this union (for ap or sta configuration) is determined by the accompanying
* interface argument passed to esp_wifi_set_config() or esp_wifi_get_config()
*
*/
typedef union {
wifi_ap_config_t ap; /**< configuration of AP */
wifi_sta_config_t sta; /**< configuration of STA */
} wifi_config_t;
11.esp_err_t esp_wifi_clear_fast_connect(void)
此API只存根
12. esp_err_t esp_wifi_deauth_sta(uint16_t aid)
函数功能:取消所有站点的身份验证或关联ID等于aid
输入参数:aid=0->取消对所有站点的认证;aid=1->取消对id的站点验证
返回值 :有返回值
13. esp_err_t esp_wifi_scan_start(const wifi_scan_config_t *config, bool block)
函数功能:扫描所有的AP
输入参数: * config:扫描配置设置,如果为NULL则是默认设置,其中默认值为show_hidden:false, scan_type:active, scan_time.active; block:是否阻塞等待扫描结构
返回值 :有返回值
wifi_scan_config_t * config的字段:
/** Parameters for an SSID scan. */
typedef struct {
uint8_t *ssid; /**< SSID of AP */
uint8_t *bssid; /**< MAC address of AP */
uint8_t channel; /**< channel, scan the specific channel */
bool show_hidden; /**< enable to scan AP whose SSID is hidden */
wifi_scan_type_t scan_type; /**< scan type, active or passive */
wifi_scan_time_t scan_time; /**< scan time per channel */
} wifi_scan_config_t;
14. esp_err_t esp_wifi_scan_stop(void)
函数功能:停止正在扫描
输入参数:无
返回值 :有返回值
15. esp_err_t esp_wifi_scan_get_ap_num(uint16_t *number)
函数功能:获取上次扫描中找到的ap数目
输入参数:number[out]存储上次扫描中发现的ap的数量
返回值 :有返回值
16. esp_err_t esp_wifi_scan_get_ap_records(uint16_t *number, wifi_ap_record_t *ap_records)
函数功能:获取上次扫描中找到的AP列表
输入参数:number[inout]作为输入参数,它存储ap_records可以容纳的最大AP数。作为输出参数,它接收该API返回的实际AP编号。ap_records—wifi_ap_record_t数组,用于保存找到的ap
返回值 :有返回值
/** WiFi event declarations */
typedef enum {
WIFI_EVENT_WIFI_READY = 0, /**< ESP32 WiFi ready */
WIFI_EVENT_SCAN_DONE, /**< ESP32 finish scanning AP */
WIFI_EVENT_STA_START, /**< ESP32 station start */
WIFI_EVENT_STA_STOP, /**< ESP32 station stop */
WIFI_EVENT_STA_CONNECTED, /**< ESP32 station connected to AP */
WIFI_EVENT_STA_DISCONNECTED, /**< ESP32 station disconnected from AP */
WIFI_EVENT_STA_AUTHMODE_CHANGE, /**< the auth mode of AP connected by ESP32 station changed */
WIFI_EVENT_STA_WPS_ER_SUCCESS, /**< ESP32 station wps succeeds in enrollee mode */
WIFI_EVENT_STA_WPS_ER_FAILED, /**< ESP32 station wps fails in enrollee mode */
WIFI_EVENT_STA_WPS_ER_TIMEOUT, /**< ESP32 station wps timeout in enrollee mode */
WIFI_EVENT_STA_WPS_ER_PIN, /**< ESP32 station wps pin code in enrollee mode */
WIFI_EVENT_STA_WPS_ER_PBC_OVERLAP, /**< ESP32 station wps overlap in enrollee mode */
WIFI_EVENT_AP_START, /**< ESP32 soft-AP start */
WIFI_EVENT_AP_STOP, /**< ESP32 soft-AP stop */
WIFI_EVENT_AP_STACONNECTED, /**< a station connected to ESP32 soft-AP */
WIFI_EVENT_AP_STADISCONNECTED, /**< a station disconnected from ESP32 soft-AP */
WIFI_EVENT_AP_PROBEREQRECVED, /**< Receive probe request packet in soft-AP interface */
WIFI_EVENT_FTM_REPORT, /**< Receive report of FTM procedure */
/* Add next events after this only */
WIFI_EVENT_STA_BSS_RSSI_LOW, /**< AP's RSSI crossed configured threshold */
WIFI_EVENT_ACTION_TX_STATUS, /**< Status indication of Action Tx operation */
WIFI_EVENT_ROC_DONE, /**< Remain-on-Channel operation complete */
WIFI_EVENT_STA_BEACON_TIMEOUT, /**< ESP32 station beacon timeout */
WIFI_EVENT_MAX, /**< Invalid WiFi event ID */
} wifi_event_t;
17. esp_err_t esp_wifi_scan_get_ap_record(wifi_ap_record_t *ap_record)
函数功能:从扫描的AP列表中获取一条AP记录
输入参数:ap_record指向AP记录的[out]指针
返回值 :有返回值
18. esp_err_t esp_wifi_clear_ap_list(void)
函数功能:清楚上次扫描中发现的AP列表,释放AP列表中所占用的内存
输入参数:无
返回值 :有返回值
19. esp_err_t esp_wifi_sta_get_ap_info(wifi_ap_record_t *ap_info)
函数功能:获取设备所关联的AP信息
输入参数:ap_info用于保存AP信息的
返回值 :有返回值
20. esp_err_t esp_wifi_set_ps(wifi_ps_type_t type)
函数功能:设置WiFi省电类型,默认省电类型为WIFI_PS_MIN_MODEM。
输入参数:wifi_ps_type_t type设置节点类型
返回值 :有返回值
还有很多,可以参考乐鑫官方的手册:
https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32/api-reference/network/esp_wifi.html