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-21 06:48:56 +00:00
|
|
|
This example shows how to scan for available set of APs.
|
2017-09-28 08:17:06 +00:00
|
|
|
*/
|
2020-02-21 06:48:56 +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"
|
|
|
|
#include "esp_event_loop.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
|
2020-02-21 06:48:56 +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-21 06:48:56 +00:00
|
|
|
static void perform_scan(void);
|
|
|
|
static bool got_scan_done_event= false;
|
2017-09-28 08:17:06 +00:00
|
|
|
|
|
|
|
static esp_err_t event_handler(void *ctx, system_event_t *event)
|
|
|
|
{
|
2020-02-21 06:48:56 +00:00
|
|
|
switch(event->event_id) {
|
|
|
|
case SYSTEM_EVENT_SCAN_DONE:
|
|
|
|
got_scan_done_event = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2017-09-28 08:17:06 +00:00
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2020-02-21 06:48:56 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2017-09-28 08:17:06 +00:00
|
|
|
{
|
|
|
|
tcpip_adapter_init();
|
|
|
|
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
|
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));
|
2020-02-21 06:48:56 +00:00
|
|
|
|
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-21 06:48:56 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
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-21 06:48:56 +00:00
|
|
|
init_wifi();
|
2017-09-28 08:17:06 +00:00
|
|
|
}
|