2017-09-28 08:17:06 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2020-02-25 06:22:58 +00:00
|
|
|
This example shows how to scan for available set of APs.
|
2017-09-28 08:17:06 +00:00
|
|
|
*/
|
2020-02-25 06:22:58 +00:00
|
|
|
#include <string.h>
|
2017-09-28 08:17:06 +00:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
#include "esp_log.h"
|
2018-11-20 10:26:53 +00:00
|
|
|
#include "esp_event.h"
|
2017-09-28 08:17:06 +00:00
|
|
|
#include "nvs_flash.h"
|
|
|
|
|
2020-02-25 06:22:58 +00:00
|
|
|
#define DEFAULT_SCAN_LIST_SIZE CONFIG_EXAMPLE_SCAN_LIST_SIZE
|
2017-09-28 08:17:06 +00:00
|
|
|
|
|
|
|
static const char *TAG = "scan";
|
|
|
|
|
2020-02-25 06:22:58 +00:00
|
|
|
static bool scan_done = false;
|
|
|
|
static void display_scan_result(void);
|
|
|
|
|
|
|
|
static void event_handler(void* arg, esp_event_base_t event_base,
|
2018-11-20 10:26:53 +00:00
|
|
|
int32_t event_id, void* event_data)
|
2017-09-28 08:17:06 +00:00
|
|
|
{
|
2020-02-25 06:22:58 +00:00
|
|
|
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_SCAN_DONE) {
|
|
|
|
scan_done = true;
|
2017-09-28 08:17:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 06:22:58 +00:00
|
|
|
static void print_auth_mode(int authmode)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2018-11-20 10:26:53 +00:00
|
|
|
|
2020-02-25 06:22:58 +00:00
|
|
|
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 display_scan_result(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)
|
2017-09-28 08:17:06 +00:00
|
|
|
{
|
|
|
|
tcpip_adapter_init();
|
2018-11-20 10:26:53 +00:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
2017-11-15 07:34:21 +00:00
|
|
|
|
2017-09-28 08:17:06 +00:00
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
2018-11-20 10:26:53 +00:00
|
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
|
2017-09-28 08:17:06 +00:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
2020-02-25 06:22:58 +00:00
|
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_scan_start(NULL, true));
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (scan_done == true) {
|
|
|
|
display_scan_result();
|
|
|
|
scan_done = false;
|
|
|
|
} else {
|
|
|
|
vTaskDelay(100 / portTICK_RATE_MS);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2017-09-28 08:17:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void app_main()
|
|
|
|
{
|
|
|
|
// 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-09-28 08:17:06 +00:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK( ret );
|
|
|
|
|
2020-02-25 06:22:58 +00:00
|
|
|
init_wifi();
|
2017-09-28 08:17:06 +00:00
|
|
|
}
|