diff --git a/examples/wifi/power_save/main/CMakeLists.txt b/examples/wifi/power_save/main/CMakeLists.txt index ff5ba16ba5..8a184368d3 100644 --- a/examples/wifi/power_save/main/CMakeLists.txt +++ b/examples/wifi/power_save/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "power_save.c" + "get_ap_info.c" INCLUDE_DIRS ".") diff --git a/examples/wifi/power_save/main/Kconfig.projbuild b/examples/wifi/power_save/main/Kconfig.projbuild index 3b468d17bd..21a9f4c49c 100644 --- a/examples/wifi/power_save/main/Kconfig.projbuild +++ b/examples/wifi/power_save/main/Kconfig.projbuild @@ -1,14 +1,23 @@ menu "Example Configuration" + config EXAMPLE_GET_AP_INFO_FROM_STDIN + bool "Get AP info from console" + default n + help + If enabled, the example will prompt the user to enter the SSID and Password of the AP to be connected + via the console. + config EXAMPLE_WIFI_SSID string "WiFi SSID" default "myssid" + depends on !EXAMPLE_GET_AP_INFO_FROM_STDIN help SSID (network name) for the example to connect to. config EXAMPLE_WIFI_PASSWORD string "WiFi Password" default "mypassword" + depends on !EXAMPLE_GET_AP_INFO_FROM_STDIN help WiFi password (WPA or WPA2) for the example to use. diff --git a/examples/wifi/power_save/main/get_ap_info.c b/examples/wifi/power_save/main/get_ap_info.c new file mode 100644 index 0000000000..00c9558464 --- /dev/null +++ b/examples/wifi/power_save/main/get_ap_info.c @@ -0,0 +1,68 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include +#include "sdkconfig.h" +#include "esp_log.h" +#include "esp_vfs_dev.h" +#include "driver/uart.h" + +#if CONFIG_EXAMPLE_GET_AP_INFO_FROM_STDIN +static const char *TAG = "power_save"; +static char stdin_ssid[32]; +static char stdin_password[64]; + +void get_ap_info_from_stdin(void) +{ + // Initialize VFS & UART so we can use std::cout/cin + setvbuf(stdin, NULL, _IONBF, 0); + /* Install UART driver for interrupt-driven reads and writes */ + ESP_ERROR_CHECK( uart_driver_install( (uart_port_t)CONFIG_ESP_CONSOLE_UART_NUM, + 256, 0, 0, NULL, 0) ); + + /* Tell VFS to use UART driver */ + esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM); + esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR); + /* Move the caret to the beginning of the next line on '\n' */ + esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF); + + ESP_LOGI(TAG, "Input SSID:"); + + if (fgets(stdin_ssid, 32, stdin) == NULL) { + ESP_LOGE(TAG, "Failed to get SSID"); + } else { + stdin_ssid[strcspn(stdin_ssid, "\n")] = '\0'; + } + + ESP_LOGI(TAG, "Input Password:"); + if (fgets(stdin_password, 64, stdin) == NULL) { + ESP_LOGE(TAG, "Failed to get password"); + } else { + stdin_password[strcspn(stdin_password, "\n")] = '\0'; + } + + /* Back to use non-blocking vfs console*/ + esp_vfs_dev_uart_use_nonblocking(CONFIG_ESP_CONSOLE_UART_NUM); + uart_driver_delete(CONFIG_ESP_CONSOLE_UART_NUM); +} +#endif + +char *get_ap_ssid(void) +{ +#if CONFIG_EXAMPLE_GET_AP_INFO_FROM_STDIN + return stdin_ssid; +#else + return CONFIG_EXAMPLE_WIFI_SSID; +#endif +} + +char *get_ap_password(void) +{ +#if CONFIG_EXAMPLE_GET_AP_INFO_FROM_STDIN + return stdin_password; +#else + return CONFIG_EXAMPLE_WIFI_PASSWORD; +#endif +} diff --git a/examples/wifi/power_save/main/get_ap_info.h b/examples/wifi/power_save/main/get_ap_info.h new file mode 100644 index 0000000000..e41c83db4f --- /dev/null +++ b/examples/wifi/power_save/main/get_ap_info.h @@ -0,0 +1,34 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + + +#if CONFIG_EXAMPLE_GET_AP_INFO_FROM_STDIN +/** + * Initialize vfs console and prompt user input the AP info + */ +void get_ap_info_from_stdin(void); +#endif + +/** + * Get the AP ssid from user input or configure + * @return The string of SSID + */ +char *get_ap_ssid(void); + +/** + * Get the AP password from user input or configure + * @return The string of password + */ +char *get_ap_password(void); + +#ifdef __cplusplus +} +#endif diff --git a/examples/wifi/power_save/main/power_save.c b/examples/wifi/power_save/main/power_save.c index 5bd256b788..d804974d65 100644 --- a/examples/wifi/power_save/main/power_save.c +++ b/examples/wifi/power_save/main/power_save.c @@ -12,6 +12,8 @@ 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 +#include "sdkconfig.h" #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" #include "esp_wifi.h" @@ -19,10 +21,7 @@ #include "esp_event.h" #include "esp_pm.h" #include "nvs_flash.h" - -/*set the ssid and password via "idf.py menuconfig"*/ -#define DEFAULT_SSID CONFIG_EXAMPLE_WIFI_SSID -#define DEFAULT_PWD CONFIG_EXAMPLE_WIFI_PASSWORD +#include "get_ap_info.h" #define DEFAULT_LISTEN_INTERVAL CONFIG_EXAMPLE_WIFI_LISTEN_INTERVAL #define DEFAULT_BEACON_TIMEOUT CONFIG_EXAMPLE_WIFI_BEACON_TIMEOUT @@ -69,11 +68,15 @@ static void wifi_power_save(void) wifi_config_t wifi_config = { .sta = { - .ssid = DEFAULT_SSID, - .password = DEFAULT_PWD, .listen_interval = DEFAULT_LISTEN_INTERVAL, }, }; + + strcpy((char *)wifi_config.sta.ssid, get_ap_ssid()); + strcpy((char *)wifi_config.sta.password, get_ap_password()); + + ESP_LOGI(TAG, "Connecting AP: %s with password: %s", wifi_config.sta.ssid, wifi_config.sta.password); + ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); @@ -93,6 +96,10 @@ void app_main(void) } ESP_ERROR_CHECK( ret ); +#if CONFIG_EXAMPLE_GET_AP_INFO_FROM_STDIN + get_ap_info_from_stdin(); +#endif + #if CONFIG_PM_ENABLE // Configure dynamic frequency scaling: // maximum and minimum frequencies are set in sdkconfig,