基于ESP-IDF4.1版本
main.c文件如下:
#include
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_pm.h"
#include "lwip/err.h"
#include "lwip/sys.h"
//值在sdkconfig中配置
#define ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
#define ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
#define ESP_MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_RETRY
#define DEFAULT_LISTEN_INTERVAL CONFIG_WIFI_LISTEN_INTERVAL
#define DEFAULT_PS_MODE WIFI_PS_MIN_MODEM
//事件的位,连接成功和失败
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
//FreeRTOS事件组用于表示什么时候建立连接
static EventGroupHandle_t s_wifi_event_group;
static const char *TAG = "wifi station";
static int s_retry_num = 0;
//事件处理服务
static void event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (s_retry_num < ESP_MAXIMUM_RETRY) {
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
} else {
//事件组置位
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
ESP_LOGI(TAG,"connect to the AP fail");
} 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;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
//事件组置位
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
//wifi初始化
void wifi_init_sta(void)
{
//创建事件组
s_wifi_event_group = xEventGroupCreate();
ESP\_ERROR\_CHECK(esp\_netif\_init());
ESP\_ERROR\_CHECK(esp\_event\_loop\_create\_default());
esp\_netif\_t \*sta\_netif = esp\_netif\_create\_default\_wifi\_sta();
assert(sta\_netif);
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, &event\_handler, NULL));
ESP\_ERROR\_CHECK(esp\_event\_handler\_register(IP\_EVENT, IP\_EVENT\_STA\_GOT\_IP, &event\_handler, NULL));
wifi\_config\_t wifi\_config = {
.sta = {
.ssid = ESP\_WIFI\_SSID,
.password = ESP\_WIFI\_PASS,
//设备从AP监听信标的间隔
.listen\_interval = DEFAULT\_LISTEN\_INTERVAL,
//设置密码意味着设备将会连接所有安全的模式,包括WEP、WPA。然而这些模式不建议使用,如果你的接入点不支持WPA2,注释以下行来启用其他模式
.threshold.authmode = WIFI\_AUTH\_WPA2\_PSK,
.pmf\_cfg = {
.capable = true,
.required = false
},
},
};
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() );
ESP\_LOGI(TAG, "esp\_wifi\_set\_ps().");
esp\_wifi\_set\_ps(DEFAULT\_PS\_MODE);
ESP\_LOGI(TAG, "wifi\_init\_sta finished.");
//事件组等待位,等到连接建立或者超过最大重连数后连接仍然失败。位的设置是通过事件处理服务
EventBits\_t bits = xEventGroupWaitBits(s\_wifi\_event\_group,
WIFI\_CONNECTED\_BIT | WIFI\_FAIL\_BIT,
pdFALSE,
pdFALSE,
portMAX\_DELAY);
//通过事件组等待位返回位的值,判断事件做了什么
if (bits & WIFI\_CONNECTED\_BIT) {
ESP\_LOGI(TAG, "connected to ap SSID:%s password:%s",
ESP\_WIFI\_SSID, ESP\_WIFI\_PASS);
} else if (bits & WIFI\_FAIL\_BIT) {
ESP\_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
ESP\_WIFI\_SSID, ESP\_WIFI\_PASS);
} else {
ESP\_LOGE(TAG, "UNEXPECTED EVENT");
}
//注销事件处理服务,注销后,即便此前已注册的事件再次被发送时,也不会再执行了。
ESP\_ERROR\_CHECK(esp\_event\_handler\_unregister(IP\_EVENT, IP\_EVENT\_STA\_GOT\_IP, &event\_handler));
ESP\_ERROR\_CHECK(esp\_event\_handler\_unregister(WIFI\_EVENT, ESP\_EVENT\_ANY\_ID, &event\_handler));
vEventGroupDelete(s\_wifi\_event\_group);
}
//启动
void app_main(void)
{
//初始化非易失性存储
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);
//配置动态频率缩放:最高频率和最低频率在sdkconfig中配置
esp\_pm\_config\_esp32\_t pm\_config = {
.max\_freq\_mhz = CONFIG\_MAX\_CPU\_FREQ\_MHZ,
.min\_freq\_mhz = CONFIG\_MIN\_CPU\_FREQ\_MHZ,
.light\_sleep\_enable = true
};
ESP\_LOGI(TAG, "ESP\_WIFI\_MODE\_STA");
wifi\_init\_sta();
}
Kconfig.projbuild文件如下:
menu "Example Configuration"
config ESP\_WIFI\_SSID
string "WiFi SSID"
default "wifissid"
help
SSID (network name) for the example to connect to.
config ESP\_WIFI\_PASSWORD
string "WiFi Password"
default "wifipassword"
help
WiFi password (WPA or WPA2) for the example to use.
config ESP\_MAXIMUM\_RETRY
int "Maximum retry"
default 5
help
Set the Maximum retry to avoid station reconnecting to the AP unlimited when the AP is really inexistent.
config WIFI\_LISTEN\_INTERVAL
int "WiFi listen interval"
default 3
help
Interval for station to listen to beacon from AP. The unit of listen interval is one beacon interval.
For example, if beacon interval is 100 ms and listen interval is 3, the interval for station to listen
to beacon is 300 ms.
choice POWER\_SAVE\_MODE
prompt "power save mode"
default POWER\_SAVE\_MIN\_MODEM
help
Power save mode for the esp32 to use. Modem sleep mode includes minimum and maximum power save modes.
In minimum power save mode, station wakes up every DTIM to receive beacon. Broadcast data will not be
lost because it is transmitted after DTIM. However, it can not save much more power if DTIM is short
for DTIM is determined by AP.
In maximum power save mode, station wakes up every listen interval to receive beacon. Broadcast data
may be lost because station may be in sleep state at DTIM time. If listen interval is longer, more power
is saved but broadcast data is more easy to lose.
config POWER\_SAVE\_NONE
bool "none"
config POWER\_SAVE\_MIN\_MODEM
bool "minimum modem"
config POWER\_SAVE\_MAX\_MODEM
bool "maximum modem"
endchoice
choice MAX\_CPU\_FREQ
prompt "Maximum CPU frequency"
default MAX\_CPU\_FREQ\_80
help
Maximum CPU frequency to use for dynamic frequency scaling.
config MAX\_CPU\_FREQ\_80
bool "80 MHz"
config MAX\_CPU\_FREQ\_160
bool "160 MHz"
config MAX\_CPU\_FREQ\_240
bool "240 MHz"
endchoice
config MAX\_CPU\_FREQ\_MHZ
int
default 80 if MAX\_CPU\_FREQ\_80
default 160 if MAX\_CPU\_FREQ\_160
default 240 if MAX\_CPU\_FREQ\_240
choice MIN\_CPU\_FREQ
prompt "Minimum CPU frequency"
default MIN\_CPU\_FREQ\_10M
help
Minimum CPU frequency to use for dynamic frequency scaling.
Should be set to XTAL frequency or XTAL frequency divided by integer.
config MIN\_CPU\_FREQ\_40M
bool "40 MHz (use with 40MHz XTAL)"
depends on ESP32\_XTAL\_FREQ\_40 || ESP32\_XTAL\_FREQ\_AUTO
config MIN\_CPU\_FREQ\_20M
bool "20 MHz (use with 40MHz XTAL)"
depends on ESP32\_XTAL\_FREQ\_40 || ESP32\_XTAL\_FREQ\_AUTO
config MIN\_CPU\_FREQ\_10M
bool "10 MHz (use with 40MHz XTAL)"
depends on ESP32\_XTAL\_FREQ\_40 || ESP32\_XTAL\_FREQ\_AUTO
config MIN\_CPU\_FREQ\_26M
bool "26 MHz (use with 26MHz XTAL)"
depends on ESP32\_XTAL\_FREQ\_26 || ESP32\_XTAL\_FREQ\_AUTO
config MIN\_CPU\_FREQ\_13M
bool "13 MHz (use with 26MHz XTAL)"
depends on ESP32\_XTAL\_FREQ\_26 || ESP32\_XTAL\_FREQ\_AUTO
endchoice
config MIN\_CPU\_FREQ\_MHZ
int
default 40 if MIN\_CPU\_FREQ\_40M
default 20 if MIN\_CPU\_FREQ\_20M
default 10 if MIN\_CPU\_FREQ\_10M
default 26 if MIN\_CPU\_FREQ\_26M
default 13 if MIN\_CPU\_FREQ\_13M
endmenu
手机扫一扫
移动阅读更方便
你可能感兴趣的文章