/* Power save Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
/*
this example shows how to use power save mode
set a router or a AP using the same SSID&PASSWORD as configuration of this example.
start esp32 and when it connected to AP it will enter power save mode
*/
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_log.h"
#include "esp_event_loop.h"
#include "esp_pm.h"
#include "nvs_flash.h"
/*set the ssid and password via "make menuconfig"*/
#define DEFAULT_SSID CONFIG_WIFI_SSID
#define DEFAULT_PWD CONFIG_WIFI_PASSWORD
#if CONFIG_POWER_SAVE_MODEM
#define DEFAULT_PS_MODE WIFI_PS_MODEM
#elif CONFIG_POWER_SAVE_NONE
#define DEFAULT_PS_MODE WIFI_PS_NONE
#else
#define DEFAULT_PS_MODE WIFI_PS_NONE
#endif
/*CONFIG_POWER_SAVE_MODEM*/
static const char *TAG = "power_save";
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
ESP_ERROR_CHECK(esp_wifi_connect());
break;
case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
ESP_LOGI(TAG, "got ip:%s\n",
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
ESP_ERROR_CHECK(esp_wifi_connect());
break;
default:
break;
}
return ESP_OK;
}
/*init wifi as sta and set power save mode*/
static void wifi_power_save(void)
{
用来初始化 TCP/IP 协议栈
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
//////esp_event_loop_init(event_handler, NULL) 用 来 初 始 化 事 件 event_handler, 回 调
event_handler 即我们刚刚处理的各种 WIFI 事件。ESP_ERROR_CHECK 用来检查返回值是否为
ESP_OK.
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
/// esp_wifi_init 用上述信息来初始化 WIFI 硬
件
wifi_config_t wifi_config = {
.sta = {
.ssid = DEFAULT_SSID,
.password = DEFAULT_PWD,
},
// wifi_config 是 WIFI 连接时的配置信息,作为 STA 时只需要考虑 sta 的参数信息,上述
代码只是制定了最基本的 ssid 和 password 信息,除此之外,还可以指定 bssid 和 channel 等
相关信息。
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
// esp_wifi_set_mode 设置 WIFI 模式,除了 WIFI_MODE_STA 以外还包括 WIFI_MODE_AP,
WIFI_MODE_APSTA。
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
// esp_wifi_set_config 设置 STA 模式的配置信息。
ESP_ERROR_CHECK(esp_wifi_start());
esp_wifi_start()根据当前配置信息开启 WIFI。
ESP_LOGI(TAG, "esp_wifi_set_ps().");//ESP_LOGI 是 espressif 的格式化输出信息,其他还包
括 ESP_LOGE(错误信息)、ESP_LOGD(调试信息)。
esp_wifi_set_ps(DEFAULT_PS_MODE);
}
void app_main()
{
// Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
///nvs_flash_init 是初始化 NVS 存储
}
ESP_ERROR_CHECK( ret );
#if CONFIG_PM_ENABLE
// Configure dynamic frequency scaling: maximum frequency is set in sdkconfig,
// minimum frequency is XTAL.
rtc_cpu_freq_t max_freq;
rtc_clk_cpu_freq_from_mhz(CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ, &max_freq);
esp_pm_config_esp32_t pm_config = {
.max_cpu_freq = max_freq,
.min_cpu_freq = RTC_CPU_FREQ_XTAL
};
ESP_ERROR_CHECK( esp_pm_configure(&pm_config) );
#endif // CONFIG_PM_ENABLE
wifi_power_save();
}