2017-03-27 05:52:46 -04:00
|
|
|
/* Power save Example
|
2017-04-12 23:43:01 -04: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 05:52:46 -04:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
#include "esp_log.h"
|
2018-11-20 05:26:53 -05:00
|
|
|
#include "esp_event.h"
|
2017-10-10 00:33:47 -04:00
|
|
|
#include "esp_pm.h"
|
2017-07-13 11:46:19 -04:00
|
|
|
#include "nvs_flash.h"
|
2017-03-27 05:52:46 -04:00
|
|
|
|
2019-08-01 23:31:20 -04:00
|
|
|
/*set the ssid and password via "idf.py menuconfig"*/
|
2019-05-09 10:43:06 -04:00
|
|
|
#define DEFAULT_SSID CONFIG_EXAMPLE_WIFI_SSID
|
|
|
|
#define DEFAULT_PWD CONFIG_EXAMPLE_WIFI_PASSWORD
|
2017-03-29 03:33:05 -04:00
|
|
|
|
2019-05-09 10:43:06 -04:00
|
|
|
#define DEFAULT_LISTEN_INTERVAL CONFIG_EXAMPLE_WIFI_LISTEN_INTERVAL
|
2017-11-09 21:54:50 -05:00
|
|
|
|
2019-05-09 10:43:06 -04:00
|
|
|
#if CONFIG_EXAMPLE_POWER_SAVE_MIN_MODEM
|
2017-11-09 21:54:50 -05:00
|
|
|
#define DEFAULT_PS_MODE WIFI_PS_MIN_MODEM
|
2019-05-09 10:43:06 -04:00
|
|
|
#elif CONFIG_EXAMPLE_POWER_SAVE_MAX_MODEM
|
2017-11-09 21:54:50 -05:00
|
|
|
#define DEFAULT_PS_MODE WIFI_PS_MAX_MODEM
|
2019-05-09 10:43:06 -04:00
|
|
|
#elif CONFIG_EXAMPLE_POWER_SAVE_NONE
|
2017-04-12 23:43:01 -04:00
|
|
|
#define DEFAULT_PS_MODE WIFI_PS_NONE
|
2017-03-29 03:33:05 -04:00
|
|
|
#else
|
|
|
|
#define DEFAULT_PS_MODE WIFI_PS_NONE
|
2017-04-12 23:43:01 -04:00
|
|
|
#endif /*CONFIG_POWER_SAVE_MODEM*/
|
|
|
|
|
2017-03-27 05:52:46 -04:00
|
|
|
|
|
|
|
static const char *TAG = "power_save";
|
|
|
|
|
2018-11-20 05:26:53 -05:00
|
|
|
static void event_handler(void* arg, esp_event_base_t event_base,
|
|
|
|
int32_t event_id, void* event_data)
|
2017-03-27 05:52:46 -04:00
|
|
|
{
|
2018-11-20 05:26:53 -05:00
|
|
|
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) {
|
|
|
|
esp_wifi_connect();
|
|
|
|
} 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;
|
2019-08-20 08:40:34 -04:00
|
|
|
ESP_LOGI(TAG, "got ip: " IPSTR, IP2STR(&event->ip_info.ip));
|
2017-03-27 05:52:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 03:33:05 -04:00
|
|
|
/*init wifi as sta and set power save mode*/
|
2017-03-27 05:52:46 -04:00
|
|
|
static void wifi_power_save(void)
|
|
|
|
{
|
2019-11-29 04:54:02 -05:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2018-11-20 05:26:53 -05:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
2019-10-17 05:36:34 -04:00
|
|
|
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
|
|
|
|
assert(sta_netif);
|
2018-11-20 05:26:53 -05:00
|
|
|
|
2017-03-27 05:52:46 -04:00
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
2018-11-20 05:26:53 -05:00
|
|
|
|
2020-03-10 05:23:03 -04:00
|
|
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, NULL));
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, NULL));
|
2018-11-20 05:26:53 -05:00
|
|
|
|
2017-03-27 05:52:46 -04:00
|
|
|
wifi_config_t wifi_config = {
|
2018-11-20 05:26:53 -05:00
|
|
|
.sta = {
|
|
|
|
.ssid = DEFAULT_SSID,
|
|
|
|
.password = DEFAULT_PWD,
|
|
|
|
.listen_interval = DEFAULT_LISTEN_INTERVAL,
|
|
|
|
},
|
2017-03-27 05:52:46 -04:00
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
2021-01-18 22:55:44 -05:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
2017-03-27 05:52:46 -04:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
2017-03-29 03:33:05 -04:00
|
|
|
|
|
|
|
ESP_LOGI(TAG, "esp_wifi_set_ps().");
|
|
|
|
esp_wifi_set_ps(DEFAULT_PS_MODE);
|
2017-03-27 05:52:46 -04:00
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2017-03-27 05:52:46 -04:00
|
|
|
{
|
2017-07-13 11:46:19 -04:00
|
|
|
// Initialize NVS
|
|
|
|
esp_err_t ret = nvs_flash_init();
|
2018-07-25 11:11:09 -04:00
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
2017-07-13 11:46:19 -04:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK( ret );
|
|
|
|
|
2017-10-10 00:33:47 -04:00
|
|
|
#if CONFIG_PM_ENABLE
|
2018-07-25 19:02:31 -04:00
|
|
|
// Configure dynamic frequency scaling:
|
|
|
|
// maximum and minimum frequencies are set in sdkconfig,
|
|
|
|
// automatic light sleep is enabled if tickless idle support is enabled.
|
2023-02-22 01:03:39 -05:00
|
|
|
esp_pm_config_t pm_config = {
|
2018-07-25 19:02:31 -04:00
|
|
|
.max_freq_mhz = CONFIG_EXAMPLE_MAX_CPU_FREQ_MHZ,
|
|
|
|
.min_freq_mhz = CONFIG_EXAMPLE_MIN_CPU_FREQ_MHZ,
|
2018-05-03 13:31:15 -04:00
|
|
|
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
|
|
|
|
.light_sleep_enable = true
|
|
|
|
#endif
|
2017-10-10 00:33:47 -04:00
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK( esp_pm_configure(&pm_config) );
|
|
|
|
#endif // CONFIG_PM_ENABLE
|
|
|
|
|
2017-03-27 05:52:46 -04:00
|
|
|
wifi_power_save();
|
|
|
|
}
|