2017-03-27 09:52:46 +00:00
|
|
|
/* Power save Example
|
2017-04-13 03:43:01 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2017-03-27 09:52:46 +00:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_event_loop.h"
|
2017-10-10 04:33:47 +00:00
|
|
|
#include "esp_pm.h"
|
2017-07-13 15:46:19 +00:00
|
|
|
#include "nvs_flash.h"
|
2017-03-27 09:52:46 +00:00
|
|
|
|
|
|
|
/*set the ssid and password via "make menuconfig"*/
|
|
|
|
#define DEFAULT_SSID CONFIG_WIFI_SSID
|
|
|
|
#define DEFAULT_PWD CONFIG_WIFI_PASSWORD
|
2017-03-29 07:33:05 +00:00
|
|
|
|
2017-11-10 02:54:50 +00:00
|
|
|
#define DEFAULT_LISTEN_INTERVAL CONFIG_WIFI_LISTEN_INTERVAL
|
|
|
|
|
|
|
|
#if CONFIG_POWER_SAVE_MIN_MODEM
|
|
|
|
#define DEFAULT_PS_MODE WIFI_PS_MIN_MODEM
|
|
|
|
#elif CONFIG_POWER_SAVE_MAX_MODEM
|
|
|
|
#define DEFAULT_PS_MODE WIFI_PS_MAX_MODEM
|
2017-04-13 03:43:01 +00:00
|
|
|
#elif CONFIG_POWER_SAVE_NONE
|
|
|
|
#define DEFAULT_PS_MODE WIFI_PS_NONE
|
2017-03-29 07:33:05 +00:00
|
|
|
#else
|
|
|
|
#define DEFAULT_PS_MODE WIFI_PS_NONE
|
2017-04-13 03:43:01 +00:00
|
|
|
#endif /*CONFIG_POWER_SAVE_MODEM*/
|
|
|
|
|
2017-03-27 09:52:46 +00:00
|
|
|
|
|
|
|
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());
|
2017-03-29 07:33:05 +00:00
|
|
|
break;
|
2017-03-27 09:52:46 +00:00
|
|
|
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());
|
2017-03-29 07:33:05 +00:00
|
|
|
break;
|
2017-03-27 09:52:46 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-29 07:33:05 +00:00
|
|
|
/*init wifi as sta and set power save mode*/
|
2017-03-27 09:52:46 +00:00
|
|
|
static void wifi_power_save(void)
|
|
|
|
{
|
|
|
|
tcpip_adapter_init();
|
|
|
|
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
|
2017-08-01 14:29:16 +00:00
|
|
|
|
2017-03-27 09:52:46 +00:00
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
|
|
wifi_config_t wifi_config = {
|
|
|
|
.sta = {
|
|
|
|
.ssid = DEFAULT_SSID,
|
2017-09-14 13:27:26 +00:00
|
|
|
.password = DEFAULT_PWD,
|
2017-11-10 02:54:50 +00:00
|
|
|
.listen_interval = DEFAULT_LISTEN_INTERVAL,
|
2017-03-27 09:52:46 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
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());
|
2017-03-29 07:33:05 +00:00
|
|
|
|
|
|
|
ESP_LOGI(TAG, "esp_wifi_set_ps().");
|
|
|
|
esp_wifi_set_ps(DEFAULT_PS_MODE);
|
2017-03-27 09:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void app_main()
|
|
|
|
{
|
2017-07-13 15:46:19 +00:00
|
|
|
// Initialize NVS
|
|
|
|
esp_err_t ret = nvs_flash_init();
|
2018-07-25 15:11:09 +00:00
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
2017-07-13 15:46:19 +00:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK( ret );
|
|
|
|
|
2017-10-10 04:33:47 +00:00
|
|
|
#if CONFIG_PM_ENABLE
|
2018-07-25 23:02:31 +00:00
|
|
|
// Configure dynamic frequency scaling:
|
|
|
|
// maximum and minimum frequencies are set in sdkconfig,
|
|
|
|
// automatic light sleep is enabled if tickless idle support is enabled.
|
2017-10-10 04:33:47 +00:00
|
|
|
esp_pm_config_esp32_t pm_config = {
|
2018-07-25 23:02:31 +00:00
|
|
|
.max_freq_mhz = CONFIG_EXAMPLE_MAX_CPU_FREQ_MHZ,
|
|
|
|
.min_freq_mhz = CONFIG_EXAMPLE_MIN_CPU_FREQ_MHZ,
|
2018-05-03 17:31:15 +00:00
|
|
|
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
|
|
|
|
.light_sleep_enable = true
|
|
|
|
#endif
|
2017-10-10 04:33:47 +00:00
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK( esp_pm_configure(&pm_config) );
|
|
|
|
#endif // CONFIG_PM_ENABLE
|
|
|
|
|
2017-03-27 09:52:46 +00:00
|
|
|
wifi_power_save();
|
|
|
|
}
|