mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
example: adding wifi example wps and power save.
This commit is contained in:
parent
47b8f78cb0
commit
b0dd5d9157
@ -1,3 +1,25 @@
|
||||
# Wi-Fi Examples
|
||||
|
||||
Including some examples about wifi.
|
||||
|
||||
## wpa2_enterprise
|
||||
|
||||
Show how ESP32 connects to AP with wpa2 enterprise encryption.
|
||||
|
||||
See the [README.md](./wpa2_enterprise/README.md) file in the project [wpa2_enterprise](./wpa2_enterprise/).
|
||||
|
||||
## power_save
|
||||
|
||||
Show how to use power save mode of wifi.
|
||||
|
||||
See the [README.md](./power_save/README.md) file in the project [power_save](./power_save/).
|
||||
|
||||
## wps(Wifi Protected Setup)
|
||||
|
||||
shows how to use wps(Wifi Protected Setup).
|
||||
|
||||
See the [README.md](./wps/README.md) file in the project [wps](./wps/).
|
||||
|
||||
# More
|
||||
|
||||
See the [README.md](../README.md) file in the upper level [examples](../) directory for more information about examples.
|
||||
|
9
examples/wifi/power_save/Makefile
Normal file
9
examples/wifi/power_save/Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := power_save
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
11
examples/wifi/power_save/README.md
Normal file
11
examples/wifi/power_save/README.md
Normal file
@ -0,0 +1,11 @@
|
||||
# Wifi Power Save Example
|
||||
|
||||
This example shows how to use power save mode of wifi.
|
||||
|
||||
Power save mode only works in sta mode.
|
||||
|
||||
* No power save: This is default mode. And the esp32 will work at full speed.
|
||||
|
||||
* modem sleep: If you set power save mode as modem sleep, after connected to AP about 10s, esp32 will wake up and sleep periodically. When it sleeps, the current will be much lower than it works.
|
||||
|
||||
* others: not supported yet.
|
13
examples/wifi/power_save/main/Kconfig.projbuild
Normal file
13
examples/wifi/power_save/main/Kconfig.projbuild
Normal file
@ -0,0 +1,13 @@
|
||||
menu "Example Configuration"
|
||||
config WIFI_SSID
|
||||
string "WiFi SSID"
|
||||
default "myssid"
|
||||
help
|
||||
SSID (network name) for the example to connect to.
|
||||
config WIFI_PASSWORD
|
||||
string "WiFi Password"
|
||||
default "mypassword"
|
||||
help
|
||||
WiFi password (WPA or WPA2) for the example to use.
|
||||
|
||||
endmenu
|
7
examples/wifi/power_save/main/component.mk
Normal file
7
examples/wifi/power_save/main/component.mk
Normal file
@ -0,0 +1,7 @@
|
||||
#
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
|
||||
|
||||
|
69
examples/wifi/power_save/main/power_save.c
Normal file
69
examples/wifi/power_save/main/power_save.c
Normal file
@ -0,0 +1,69 @@
|
||||
/* Power save Example
|
||||
* 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 inter 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"
|
||||
|
||||
/*set the ssid and password via "make menuconfig"*/
|
||||
#define DEFAULT_SSID CONFIG_WIFI_SSID
|
||||
#define DEFAULT_PWD CONFIG_WIFI_PASSWORD
|
||||
/* FreeRTOS event group to signal when we are connected & ready to make a request */
|
||||
static EventGroupHandle_t wifi_event_group;
|
||||
|
||||
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 pawer save mode*/
|
||||
static void wifi_power_save(void)
|
||||
{
|
||||
tcpip_adapter_init();
|
||||
wifi_event_group = xEventGroupCreate();
|
||||
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
|
||||
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,
|
||||
.password = DEFAULT_PWD
|
||||
},
|
||||
};
|
||||
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(WIFI_PS_MODEM).");
|
||||
esp_wifi_set_ps(WIFI_PS_MODEM);
|
||||
}
|
||||
|
||||
void app_main()
|
||||
{
|
||||
wifi_power_save();
|
||||
}
|
9
examples/wifi/wps/Makefile
Normal file
9
examples/wifi/wps/Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := wps_example
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
11
examples/wifi/wps/README.md
Normal file
11
examples/wifi/wps/README.md
Normal file
@ -0,0 +1,11 @@
|
||||
# Wifi WPS Example
|
||||
|
||||
This example shows how to use wps in esp32.
|
||||
|
||||
The wps only works in station mode now.
|
||||
|
||||
* PBC_MODE: Start esp32 and push the buttom of wps on router, Then esp32 will get the ssid&password by wps PBC mode.
|
||||
|
||||
* PIN_MODE: Start esp32, It will inter wps mode and you'll see a pin code showing by COM. Enter this pin code in router and the esp32 can get ssid&password by wps PIN mode.
|
||||
|
||||
More info in the code [wps.c](./main/wps.c).
|
7
examples/wifi/wps/main/component.mk
Normal file
7
examples/wifi/wps/main/component.mk
Normal file
@ -0,0 +1,7 @@
|
||||
#
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
|
||||
|
||||
|
100
examples/wifi/wps/main/wps.c
Normal file
100
examples/wifi/wps/main/wps.c
Normal file
@ -0,0 +1,100 @@
|
||||
/* WiFi Connection Example using WPS
|
||||
*
|
||||
* WPS_TYPE_PBC: Start esp32 and it will inter wps PBC mode. Then push the buttom of wps on router down. The esp32 will connected to the router.
|
||||
*
|
||||
* WPS_TYPE_PIN: Start esp32, You'll see PIN code which is a eight-digit number showing on COM. Enter the PIN code in router and then the esp32 will connected to router.
|
||||
*/
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_wps.h"
|
||||
#include "esp_event_loop.h"
|
||||
|
||||
|
||||
/*set wps mode here*/
|
||||
#define WPS_TEST_MODE WPS_TYPE_PBC
|
||||
//#define WPS_TEST_MODE WPS_TYPE_PIN
|
||||
//#define WPS_TEST_MODE WPS_TYPE_MAX
|
||||
|
||||
|
||||
#ifndef PIN2STR
|
||||
#define PIN2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5], (a)[6], (a)[7]
|
||||
#define PINSTR "%c%c%c%c%c%c%c%c"
|
||||
#endif
|
||||
|
||||
/* FreeRTOS event group to signal when we are connected & ready to make a request */
|
||||
static EventGroupHandle_t wifi_event_group;
|
||||
|
||||
static const char *TAG = "example_wps";
|
||||
|
||||
|
||||
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");
|
||||
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;
|
||||
case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
|
||||
/*point: the function esp_wifi_wps_start() only get ssid & password
|
||||
* and we suggest you call the function esp_wifi_connect() here
|
||||
* */
|
||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_SUCCESS");
|
||||
ESP_ERROR_CHECK(esp_wifi_wps_disable());
|
||||
ESP_ERROR_CHECK(esp_wifi_connect());
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_WPS_ER_FAILED:
|
||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_FAILED");
|
||||
ESP_ERROR_CHECK(esp_wifi_wps_disable());
|
||||
ESP_ERROR_CHECK(esp_wifi_wps_enable(WPS_TEST_MODE));
|
||||
ESP_ERROR_CHECK(esp_wifi_wps_start(0));
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
|
||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_TIMEOUT");
|
||||
ESP_ERROR_CHECK(esp_wifi_wps_disable());
|
||||
ESP_ERROR_CHECK(esp_wifi_wps_enable(WPS_TEST_MODE));
|
||||
ESP_ERROR_CHECK(esp_wifi_wps_start(0));
|
||||
break;
|
||||
case SYSTEM_EVENT_STA_WPS_ER_PIN:
|
||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_PIN");
|
||||
/*show the PIN code here*/
|
||||
ESP_LOGI(TAG, "WPS_PIN = "PINSTR, PIN2STR(event->event_info.sta_er_pin.pin_code));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/*init wifi as sta and start wps*/
|
||||
static void start_wps(void)
|
||||
{
|
||||
tcpip_adapter_init();
|
||||
wifi_event_group = xEventGroupCreate();
|
||||
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
ESP_LOGI(TAG, "start wps...");
|
||||
//ESP_ERROR_CHECK(esp_wifi_wps_enable(WPS_TYPE_PBC));
|
||||
ESP_ERROR_CHECK(esp_wifi_wps_enable(WPS_TEST_MODE));
|
||||
ESP_ERROR_CHECK(esp_wifi_wps_start(0));
|
||||
}
|
||||
|
||||
void app_main()
|
||||
{
|
||||
start_wps();
|
||||
}
|
Loading…
Reference in New Issue
Block a user