esp_wifi: Additional code snippets in esp_wifi_scan_example (backport v3.2)
This commit is contained in:
parent
5ddeab1409
commit
254dcb755c
11 changed files with 379 additions and 151 deletions
|
@ -26,6 +26,18 @@ shows how to use espnow.
|
||||||
|
|
||||||
See the [README.md](./espnow/README.md) file in the project [espnow](./espnow/).
|
See the [README.md](./espnow/README.md) file in the project [espnow](./espnow/).
|
||||||
|
|
||||||
|
## fast_scan
|
||||||
|
|
||||||
|
Show how to use fast scan while connecting to an AP.
|
||||||
|
|
||||||
|
See the [README.md](./fast_scan/README.md) file in the project [espnow](./espnow/).
|
||||||
|
|
||||||
|
## scan
|
||||||
|
|
||||||
|
Show how to scan for all the available APs.
|
||||||
|
|
||||||
|
See the [README.md](./scan/README.md) file in the project [espnow](./espnow/).
|
||||||
|
|
||||||
# More
|
# More
|
||||||
|
|
||||||
See the [README.md](../README.md) file in the upper level [examples](../) directory for more information about examples.
|
See the [README.md](../README.md) file in the upper level [examples](../) directory for more information about examples.
|
||||||
|
|
6
examples/wifi/fast_scan/CMakeLists.txt
Normal file
6
examples/wifi/fast_scan/CMakeLists.txt
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||||
|
# in this exact order for cmake to work correctly
|
||||||
|
cmake_minimum_required(VERSION 3.5)
|
||||||
|
|
||||||
|
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||||
|
project(fast_scan)
|
9
examples/wifi/fast_scan/Makefile
Normal file
9
examples/wifi/fast_scan/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 := fast_scan
|
||||||
|
|
||||||
|
include $(IDF_PATH)/make/project.mk
|
||||||
|
|
11
examples/wifi/fast_scan/README.md
Normal file
11
examples/wifi/fast_scan/README.md
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
# Wifi Fast Scan Example
|
||||||
|
|
||||||
|
This example shows how to use the scan functionality of the Wi-Fi driver of ESP32 for connecting to an AP.
|
||||||
|
|
||||||
|
Two scan methods are supported: fast scan and all channel scan.
|
||||||
|
|
||||||
|
* fast scan: in this mode, scan finishes right after a matching AP is detected, even if channels are not completely scanned. You can set thresholds for signal strength, as well as select desired authentication modes provided by the AP's. The Wi-Fi driver will ignore AP's that fail to meet mentioned criteria.
|
||||||
|
|
||||||
|
* all channel scan: scan will end only after all channels are scanned; the Wi-Fi driver will store 4 of the fully matching AP's. Sort methods for AP's include rssi and authmode. After the scan, the Wi-Fi driver selects the AP that fits best based on the sort.
|
||||||
|
|
||||||
|
After the scan, the Wi-Fi driver will try to connect. Because it needs to to allocate precious dynamic memory to store matching AP's, and, most of the cases, connect to the AP with the strongest reception, it does not need to record all the AP's matched. The number of matches stored is limited to 4 in order to limit dynamic memory usage. Among the 4 matches, AP's are allowed to carry the same SSID name and all possible authentication modes - Open, WEP, WPA and WPA2.
|
4
examples/wifi/fast_scan/main/CMakeLists.txt
Normal file
4
examples/wifi/fast_scan/main/CMakeLists.txt
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
set(COMPONENT_SRCS "fast_scan.c")
|
||||||
|
set(COMPONENT_ADD_INCLUDEDIRS ".")
|
||||||
|
|
||||||
|
register_component()
|
68
examples/wifi/fast_scan/main/Kconfig.projbuild
Normal file
68
examples/wifi/fast_scan/main/Kconfig.projbuild
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
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.
|
||||||
|
|
||||||
|
choice SCAN_METHOD
|
||||||
|
prompt "scan method"
|
||||||
|
default WIFI_FAST_SCAN
|
||||||
|
help
|
||||||
|
scan method for the esp32 to use
|
||||||
|
|
||||||
|
config WIFI_FAST_SCAN
|
||||||
|
bool "fast"
|
||||||
|
config WIFI_ALL_CHANNEL_SCAN
|
||||||
|
bool "all"
|
||||||
|
endchoice
|
||||||
|
|
||||||
|
choice SORT_METHOD
|
||||||
|
prompt "sort method"
|
||||||
|
default WIFI_CONNECT_AP_BY_SIGNAL
|
||||||
|
help
|
||||||
|
sort method for the esp32 to use
|
||||||
|
|
||||||
|
config WIFI_CONNECT_AP_BY_SIGNAL
|
||||||
|
bool "rssi"
|
||||||
|
config WIFI_CONNECT_AP_BY_SECURITY
|
||||||
|
bool "authmode"
|
||||||
|
endchoice
|
||||||
|
|
||||||
|
config FAST_SCAN_THRESHOLD
|
||||||
|
bool "fast scan threshold"
|
||||||
|
default y
|
||||||
|
help
|
||||||
|
wifi fast scan threshold
|
||||||
|
|
||||||
|
config FAST_SCAN_MINIMUM_SIGNAL
|
||||||
|
int "fast scan minimum rssi"
|
||||||
|
depends on FAST_SCAN_THRESHOLD
|
||||||
|
range -127 0
|
||||||
|
default -127
|
||||||
|
help
|
||||||
|
rssi is use to measure the signal
|
||||||
|
|
||||||
|
choice FAST_SCAN_WEAKEST_AUTHMODE
|
||||||
|
prompt "fast scan weakest authmode"
|
||||||
|
depends on FAST_SCAN_THRESHOLD
|
||||||
|
default EXAMPLE_OPEN
|
||||||
|
|
||||||
|
config EXAMPLE_OPEN
|
||||||
|
bool "open"
|
||||||
|
config EXAMPLE_WEP
|
||||||
|
bool "wep"
|
||||||
|
config EXAMPLE_WPA
|
||||||
|
bool "wpa"
|
||||||
|
config EXAMPLE_WPA2
|
||||||
|
bool "wpa2"
|
||||||
|
endchoice
|
||||||
|
|
||||||
|
endmenu
|
7
examples/wifi/fast_scan/main/component.mk
Normal file
7
examples/wifi/fast_scan/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.)
|
||||||
|
|
||||||
|
|
||||||
|
|
127
examples/wifi/fast_scan/main/fast_scan.c
Normal file
127
examples/wifi/fast_scan/main/fast_scan.c
Normal file
|
@ -0,0 +1,127 @@
|
||||||
|
/* Scan 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 the All Channel Scan or Fast Scan to connect
|
||||||
|
to a Wi-Fi network.
|
||||||
|
|
||||||
|
In the Fast Scan mode, the scan will stop as soon as the first network matching
|
||||||
|
the SSID is found. In this mode, an application can set threshold for the
|
||||||
|
authentication mode and the Signal strength. Networks that do not meet the
|
||||||
|
threshold requirements will be ignored.
|
||||||
|
|
||||||
|
In the All Channel Scan mode, the scan will end only after all the channels
|
||||||
|
are scanned, and connection will start with the best network. The networks
|
||||||
|
can be sorted based on Authentication Mode or Signal Strength. The priority
|
||||||
|
for the Authentication mode is: WPA2 > WPA > WEP > Open
|
||||||
|
*/
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
#include "freertos/event_groups.h"
|
||||||
|
#include "esp_wifi.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "esp_event_loop.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_WIFI_ALL_CHANNEL_SCAN
|
||||||
|
#define DEFAULT_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
|
||||||
|
#elif CONFIG_WIFI_FAST_SCAN
|
||||||
|
#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
|
||||||
|
#else
|
||||||
|
#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
|
||||||
|
#endif /*CONFIG_SCAN_METHOD*/
|
||||||
|
|
||||||
|
#if CONFIG_WIFI_CONNECT_AP_BY_SIGNAL
|
||||||
|
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
|
||||||
|
#elif CONFIG_WIFI_CONNECT_AP_BY_SECURITY
|
||||||
|
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
|
||||||
|
#else
|
||||||
|
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
|
||||||
|
#endif /*CONFIG_SORT_METHOD*/
|
||||||
|
|
||||||
|
#if CONFIG_FAST_SCAN_THRESHOLD
|
||||||
|
#define DEFAULT_RSSI CONFIG_FAST_SCAN_MINIMUM_SIGNAL
|
||||||
|
#if CONFIG_EXAMPLE_OPEN
|
||||||
|
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
|
||||||
|
#elif CONFIG_EXAMPLE_WEP
|
||||||
|
#define DEFAULT_AUTHMODE WIFI_AUTH_WEP
|
||||||
|
#elif CONFIG_EXAMPLE_WPA
|
||||||
|
#define DEFAULT_AUTHMODE WIFI_AUTH_WPA_PSK
|
||||||
|
#elif CONFIG_EXAMPLE_WPA2
|
||||||
|
#define DEFAULT_AUTHMODE WIFI_AUTH_WPA2_PSK
|
||||||
|
#else
|
||||||
|
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
|
||||||
|
#endif
|
||||||
|
#else
|
||||||
|
#define DEFAULT_RSSI -127
|
||||||
|
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
|
||||||
|
#endif /*CONFIG_FAST_SCAN_THRESHOLD*/
|
||||||
|
|
||||||
|
static const char *TAG = "scan";
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Initialize Wi-Fi as sta and set scan method */
|
||||||
|
static void wifi_scan(void)
|
||||||
|
{
|
||||||
|
tcpip_adapter_init();
|
||||||
|
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,
|
||||||
|
.scan_method = DEFAULT_SCAN_METHOD,
|
||||||
|
.sort_method = DEFAULT_SORT_METHOD,
|
||||||
|
.threshold.rssi = DEFAULT_RSSI,
|
||||||
|
.threshold.authmode = DEFAULT_AUTHMODE,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
void app_main()
|
||||||
|
{
|
||||||
|
// Initialize NVS
|
||||||
|
esp_err_t ret = nvs_flash_init();
|
||||||
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||||
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||||
|
ret = nvs_flash_init();
|
||||||
|
}
|
||||||
|
ESP_ERROR_CHECK( ret );
|
||||||
|
|
||||||
|
wifi_scan();
|
||||||
|
}
|
|
@ -1,11 +1,5 @@
|
||||||
# Wifi SCAN Example
|
# Wifi SCAN Example
|
||||||
|
|
||||||
This example shows how to use scan of ESP32.
|
This example shows how to scan for available set of APs.
|
||||||
|
|
||||||
We have two way to scan, fast scan and all channel scan:
|
* The SCAN_LIST_SIZE parameter needs to be set from the example configuration menu. It represents maximum number of scan list entries that driver can populate.
|
||||||
|
|
||||||
* fast scan: in this mode, scan will finish after find match AP even didn't scan all the channel, you can set thresholds for signal and authmode, it will ignore the AP which below the thresholds.
|
|
||||||
|
|
||||||
* all channel scan : scan will end after checked all the channel, it will store four of the whole matched AP, you can set the sort method base on rssi or authmode, after scan, it will choose the best one
|
|
||||||
|
|
||||||
and try to connect. Because it need malloc dynamic memory to store match AP, and most of cases is to connect to better signal AP, so it needn't record all the AP matched. The number of matches is limited to 4 in order to limit dynamic memory usage. Four matches allows APs with the same SSID name and all possible auth modes - Open, WEP, WPA and WPA2.
|
|
||||||
|
|
|
@ -1,68 +1,11 @@
|
||||||
menu "Example Configuration"
|
menu "Example Configuration"
|
||||||
|
|
||||||
config WIFI_SSID
|
config EXAMPLE_SCAN_LIST_SIZE
|
||||||
string "WiFi SSID"
|
int "Max size of scan list"
|
||||||
default "myssid"
|
range 0 20
|
||||||
|
default 10
|
||||||
help
|
help
|
||||||
SSID (network name) for the example to connect to.
|
The size of array that will be used to retrieve the list of access points.
|
||||||
|
|
||||||
config WIFI_PASSWORD
|
|
||||||
string "WiFi Password"
|
|
||||||
default "mypassword"
|
|
||||||
help
|
|
||||||
WiFi password (WPA or WPA2) for the example to use.
|
|
||||||
|
|
||||||
choice SCAN_METHOD
|
|
||||||
prompt "scan method"
|
|
||||||
default WIFI_FAST_SCAN
|
|
||||||
help
|
|
||||||
scan method for the esp32 to use
|
|
||||||
|
|
||||||
config WIFI_FAST_SCAN
|
|
||||||
bool "fast"
|
|
||||||
config WIFI_ALL_CHANNEL_SCAN
|
|
||||||
bool "all"
|
|
||||||
endchoice
|
|
||||||
|
|
||||||
choice SORT_METHOD
|
|
||||||
prompt "sort method"
|
|
||||||
default WIFI_CONNECT_AP_BY_SIGNAL
|
|
||||||
help
|
|
||||||
sort method for the esp32 to use
|
|
||||||
|
|
||||||
config WIFI_CONNECT_AP_BY_SIGNAL
|
|
||||||
bool "rssi"
|
|
||||||
config WIFI_CONNECT_AP_BY_SECURITY
|
|
||||||
bool "authmode"
|
|
||||||
endchoice
|
|
||||||
|
|
||||||
config FAST_SCAN_THRESHOLD
|
|
||||||
bool "fast scan threshold"
|
|
||||||
default y
|
|
||||||
help
|
|
||||||
wifi fast scan threshold
|
|
||||||
|
|
||||||
config FAST_SCAN_MINIMUM_SIGNAL
|
|
||||||
int "fast scan minimum rssi"
|
|
||||||
depends on FAST_SCAN_THRESHOLD
|
|
||||||
range -127 0
|
|
||||||
default -127
|
|
||||||
help
|
|
||||||
rssi is use to measure the signal
|
|
||||||
|
|
||||||
choice FAST_SCAN_WEAKEST_AUTHMODE
|
|
||||||
prompt "fast scan weakest authmode"
|
|
||||||
depends on FAST_SCAN_THRESHOLD
|
|
||||||
default EXAMPLE_OPEN
|
|
||||||
|
|
||||||
config EXAMPLE_OPEN
|
|
||||||
bool "open"
|
|
||||||
config EXAMPLE_WEP
|
|
||||||
bool "wep"
|
|
||||||
config EXAMPLE_WPA
|
|
||||||
bool "wpa"
|
|
||||||
config EXAMPLE_WPA2
|
|
||||||
bool "wpa2"
|
|
||||||
endchoice
|
|
||||||
|
|
||||||
endmenu
|
endmenu
|
||||||
|
|
||||||
|
|
|
@ -8,19 +8,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
This example shows how to use the All Channel Scan or Fast Scan to connect
|
This example shows how to scan for available set of APs.
|
||||||
to a Wi-Fi network.
|
|
||||||
|
|
||||||
In the Fast Scan mode, the scan will stop as soon as the first network matching
|
|
||||||
the SSID is found. In this mode, an application can set threshold for the
|
|
||||||
authentication mode and the Signal strength. Networks that do not meet the
|
|
||||||
threshold requirements will be ignored.
|
|
||||||
|
|
||||||
In the All Channel Scan mode, the scan will end only after all the channels
|
|
||||||
are scanned, and connection will start with the best network. The networks
|
|
||||||
can be sorted based on Authentication Mode or Signal Strength. The priority
|
|
||||||
for the Authentication mode is: WPA2 > WPA > WEP > Open
|
|
||||||
*/
|
*/
|
||||||
|
#include <string.h>
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/event_groups.h"
|
#include "freertos/event_groups.h"
|
||||||
#include "esp_wifi.h"
|
#include "esp_wifi.h"
|
||||||
|
@ -28,61 +18,17 @@
|
||||||
#include "esp_event_loop.h"
|
#include "esp_event_loop.h"
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
|
|
||||||
/*Set the SSID and Password via "make menuconfig"*/
|
#define DEFAULT_SCAN_LIST_SIZE CONFIG_EXAMPLE_SCAN_LIST_SIZE
|
||||||
#define DEFAULT_SSID CONFIG_WIFI_SSID
|
|
||||||
#define DEFAULT_PWD CONFIG_WIFI_PASSWORD
|
|
||||||
|
|
||||||
#if CONFIG_WIFI_ALL_CHANNEL_SCAN
|
|
||||||
#define DEFAULT_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
|
|
||||||
#elif CONFIG_WIFI_FAST_SCAN
|
|
||||||
#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
|
|
||||||
#else
|
|
||||||
#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
|
|
||||||
#endif /*CONFIG_SCAN_METHOD*/
|
|
||||||
|
|
||||||
#if CONFIG_WIFI_CONNECT_AP_BY_SIGNAL
|
|
||||||
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
|
|
||||||
#elif CONFIG_WIFI_CONNECT_AP_BY_SECURITY
|
|
||||||
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
|
|
||||||
#else
|
|
||||||
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
|
|
||||||
#endif /*CONFIG_SORT_METHOD*/
|
|
||||||
|
|
||||||
#if CONFIG_FAST_SCAN_THRESHOLD
|
|
||||||
#define DEFAULT_RSSI CONFIG_FAST_SCAN_MINIMUM_SIGNAL
|
|
||||||
#if CONFIG_EXAMPLE_OPEN
|
|
||||||
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
|
|
||||||
#elif CONFIG_EXAMPLE_WEP
|
|
||||||
#define DEFAULT_AUTHMODE WIFI_AUTH_WEP
|
|
||||||
#elif CONFIG_EXAMPLE_WPA
|
|
||||||
#define DEFAULT_AUTHMODE WIFI_AUTH_WPA_PSK
|
|
||||||
#elif CONFIG_EXAMPLE_WPA2
|
|
||||||
#define DEFAULT_AUTHMODE WIFI_AUTH_WPA2_PSK
|
|
||||||
#else
|
|
||||||
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
|
|
||||||
#endif
|
|
||||||
#else
|
|
||||||
#define DEFAULT_RSSI -127
|
|
||||||
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
|
|
||||||
#endif /*CONFIG_FAST_SCAN_THRESHOLD*/
|
|
||||||
|
|
||||||
static const char *TAG = "scan";
|
static const char *TAG = "scan";
|
||||||
|
static void perform_scan(void);
|
||||||
|
static bool got_scan_done_event= false;
|
||||||
|
|
||||||
static esp_err_t event_handler(void *ctx, system_event_t *event)
|
static esp_err_t event_handler(void *ctx, system_event_t *event)
|
||||||
{
|
{
|
||||||
switch (event->event_id) {
|
switch(event->event_id) {
|
||||||
case SYSTEM_EVENT_STA_START:
|
case SYSTEM_EVENT_SCAN_DONE:
|
||||||
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
|
got_scan_done_event = true;
|
||||||
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;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
@ -90,27 +36,128 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize Wi-Fi as sta and set scan method */
|
static void print_auth_mode(int authmode)
|
||||||
static void wifi_scan(void)
|
{
|
||||||
|
switch (authmode) {
|
||||||
|
case WIFI_AUTH_OPEN:
|
||||||
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_OPEN");
|
||||||
|
break;
|
||||||
|
case WIFI_AUTH_WEP:
|
||||||
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WEP");
|
||||||
|
break;
|
||||||
|
case WIFI_AUTH_WPA_PSK:
|
||||||
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA_PSK");
|
||||||
|
break;
|
||||||
|
case WIFI_AUTH_WPA2_PSK:
|
||||||
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA2_PSK");
|
||||||
|
break;
|
||||||
|
case WIFI_AUTH_WPA_WPA2_PSK:
|
||||||
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA_WPA2_PSK");
|
||||||
|
break;
|
||||||
|
case WIFI_AUTH_WPA2_ENTERPRISE:
|
||||||
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_WPA2_ENTERPRISE");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ESP_LOGI(TAG, "Authmode \tWIFI_AUTH_UNKNOWN");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print_cipher_type(int pairwise_cipher, int group_cipher)
|
||||||
|
{
|
||||||
|
switch (pairwise_cipher) {
|
||||||
|
case WIFI_CIPHER_TYPE_NONE:
|
||||||
|
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_NONE");
|
||||||
|
break;
|
||||||
|
case WIFI_CIPHER_TYPE_WEP40:
|
||||||
|
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_WEP40");
|
||||||
|
break;
|
||||||
|
case WIFI_CIPHER_TYPE_WEP104:
|
||||||
|
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_WEP104");
|
||||||
|
break;
|
||||||
|
case WIFI_CIPHER_TYPE_TKIP:
|
||||||
|
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_TKIP");
|
||||||
|
break;
|
||||||
|
case WIFI_CIPHER_TYPE_CCMP:
|
||||||
|
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_CCMP");
|
||||||
|
break;
|
||||||
|
case WIFI_CIPHER_TYPE_TKIP_CCMP:
|
||||||
|
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_TKIP_CCMP");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ESP_LOGI(TAG, "Pairwise Cipher \tWIFI_CIPHER_TYPE_UNKNOWN");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (group_cipher) {
|
||||||
|
case WIFI_CIPHER_TYPE_NONE:
|
||||||
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_NONE");
|
||||||
|
break;
|
||||||
|
case WIFI_CIPHER_TYPE_WEP40:
|
||||||
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_WEP40");
|
||||||
|
break;
|
||||||
|
case WIFI_CIPHER_TYPE_WEP104:
|
||||||
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_WEP104");
|
||||||
|
break;
|
||||||
|
case WIFI_CIPHER_TYPE_TKIP:
|
||||||
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_TKIP");
|
||||||
|
break;
|
||||||
|
case WIFI_CIPHER_TYPE_CCMP:
|
||||||
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_CCMP");
|
||||||
|
break;
|
||||||
|
case WIFI_CIPHER_TYPE_TKIP_CCMP:
|
||||||
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_TKIP_CCMP");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ESP_LOGI(TAG, "Group Cipher \tWIFI_CIPHER_TYPE_UNKNOWN");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Initialise a wifi_ap_record_t, get it populated and display scanned data */
|
||||||
|
static void perform_scan(void)
|
||||||
|
{
|
||||||
|
uint16_t number = DEFAULT_SCAN_LIST_SIZE;
|
||||||
|
wifi_ap_record_t ap_info[DEFAULT_SCAN_LIST_SIZE];
|
||||||
|
uint16_t ap_count = 0;
|
||||||
|
memset(ap_info, 0, sizeof(ap_info));
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, ap_info));
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
|
||||||
|
ESP_LOGI(TAG, "Total APs scanned = %u", ap_count);
|
||||||
|
for (int i = 0; (i < DEFAULT_SCAN_LIST_SIZE) && (i < ap_count); i++) {
|
||||||
|
ESP_LOGI(TAG, "SSID \t\t%s", ap_info[i].ssid);
|
||||||
|
ESP_LOGI(TAG, "RSSI \t\t%d", ap_info[i].rssi);
|
||||||
|
print_auth_mode(ap_info[i].authmode);
|
||||||
|
if (ap_info[i].authmode != WIFI_AUTH_WEP) {
|
||||||
|
print_cipher_type(ap_info[i].pairwise_cipher, ap_info[i].group_cipher);
|
||||||
|
}
|
||||||
|
ESP_LOGI(TAG, "Channel \t\t%d\n", ap_info[i].primary);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Initialize Wi-Fi as sta and start scan */
|
||||||
|
static void init_wifi(void)
|
||||||
{
|
{
|
||||||
tcpip_adapter_init();
|
tcpip_adapter_init();
|
||||||
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
|
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
|
||||||
|
|
||||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||||
wifi_config_t wifi_config = {
|
|
||||||
.sta = {
|
|
||||||
.ssid = DEFAULT_SSID,
|
|
||||||
.password = DEFAULT_PWD,
|
|
||||||
.scan_method = DEFAULT_SCAN_METHOD,
|
|
||||||
.sort_method = DEFAULT_SORT_METHOD,
|
|
||||||
.threshold.rssi = DEFAULT_RSSI,
|
|
||||||
.threshold.authmode = DEFAULT_AUTHMODE,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
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_ERROR_CHECK(esp_wifi_start());
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_scan_start(NULL, true));
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
if (got_scan_done_event == true) {
|
||||||
|
perform_scan();
|
||||||
|
got_scan_done_event = false;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
vTaskDelay(100 / portTICK_RATE_MS);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void app_main()
|
void app_main()
|
||||||
|
@ -123,5 +170,5 @@ void app_main()
|
||||||
}
|
}
|
||||||
ESP_ERROR_CHECK( ret );
|
ESP_ERROR_CHECK( ret );
|
||||||
|
|
||||||
wifi_scan();
|
init_wifi();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue