2017-09-28 04:17:06 -04: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-11-15 04:29:13 -05:00
|
|
|
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
|
2017-09-28 04:17:06 -04:00
|
|
|
*/
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
#include "esp_log.h"
|
2018-11-20 05:26:53 -05:00
|
|
|
#include "esp_event.h"
|
2017-09-28 04:17:06 -04:00
|
|
|
#include "nvs_flash.h"
|
|
|
|
|
2019-06-22 21:54:31 -04:00
|
|
|
/* Set the SSID and Password via project configuration, or can set directly here */
|
2019-05-09 10:43:06 -04:00
|
|
|
#define DEFAULT_SSID CONFIG_EXAMPLE_WIFI_SSID
|
|
|
|
#define DEFAULT_PWD CONFIG_EXAMPLE_WIFI_PASSWORD
|
2017-09-28 04:17:06 -04:00
|
|
|
|
2019-05-09 10:43:06 -04:00
|
|
|
#if CONFIG_EXAMPLE_WIFI_ALL_CHANNEL_SCAN
|
2017-09-28 04:17:06 -04:00
|
|
|
#define DEFAULT_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
|
2019-05-09 10:43:06 -04:00
|
|
|
#elif CONFIG_EXAMPLE_WIFI_FAST_SCAN
|
2017-09-28 04:17:06 -04:00
|
|
|
#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
|
|
|
|
#else
|
|
|
|
#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
|
2019-05-09 10:43:06 -04:00
|
|
|
#endif /*CONFIG_EXAMPLE_SCAN_METHOD*/
|
2017-09-28 04:17:06 -04:00
|
|
|
|
2019-05-09 10:43:06 -04:00
|
|
|
#if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL
|
2017-09-28 04:17:06 -04:00
|
|
|
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
|
2019-05-09 10:43:06 -04:00
|
|
|
#elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY
|
2017-09-28 04:17:06 -04:00
|
|
|
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
|
|
|
|
#else
|
|
|
|
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
|
2019-05-09 10:43:06 -04:00
|
|
|
#endif /*CONFIG_EXAMPLE_SORT_METHOD*/
|
2017-09-28 04:17:06 -04:00
|
|
|
|
2019-05-09 10:43:06 -04:00
|
|
|
#if CONFIG_EXAMPLE_FAST_SCAN_THRESHOLD
|
|
|
|
#define DEFAULT_RSSI CONFIG_EXAMPLE_FAST_SCAN_MINIMUM_SIGNAL
|
|
|
|
#if CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_OPEN
|
2017-09-28 04:17:06 -04:00
|
|
|
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
|
2019-05-09 10:43:06 -04:00
|
|
|
#elif CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_WEP
|
2017-09-28 04:17:06 -04:00
|
|
|
#define DEFAULT_AUTHMODE WIFI_AUTH_WEP
|
2019-05-09 10:43:06 -04:00
|
|
|
#elif CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_WPA
|
2017-09-28 04:17:06 -04:00
|
|
|
#define DEFAULT_AUTHMODE WIFI_AUTH_WPA_PSK
|
2019-05-09 10:43:06 -04:00
|
|
|
#elif CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_WPA2
|
2017-09-28 04:17:06 -04:00
|
|
|
#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
|
2019-05-09 10:43:06 -04:00
|
|
|
#endif /*CONFIG_EXAMPLE_FAST_SCAN_THRESHOLD*/
|
2017-09-28 04:17:06 -04:00
|
|
|
|
|
|
|
static const char *TAG = "scan";
|
|
|
|
|
2018-11-20 05:26:53 -05:00
|
|
|
static void event_handler(void* arg, esp_event_base_t event_base,
|
|
|
|
int32_t event_id, void* event_data)
|
2017-09-28 04:17:06 -04:00
|
|
|
{
|
2018-11-20 05:26:53 -05:00
|
|
|
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
|
|
|
esp_wifi_connect();
|
|
|
|
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
|
|
|
esp_wifi_connect();
|
|
|
|
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
|
|
|
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
|
|
|
|
ESP_LOGI(TAG, "got ip: %s", ip4addr_ntoa(&event->ip_info.ip));
|
2017-09-28 04:17:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 05:26:53 -05:00
|
|
|
|
2017-11-15 04:29:13 -05:00
|
|
|
/* Initialize Wi-Fi as sta and set scan method */
|
2017-09-28 04:17:06 -04:00
|
|
|
static void wifi_scan(void)
|
|
|
|
{
|
|
|
|
tcpip_adapter_init();
|
2018-11-20 05:26:53 -05:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
2017-11-15 02:34:21 -05:00
|
|
|
|
2017-09-28 04:17:06 -04:00
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
2018-11-20 05:26:53 -05:00
|
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
|
|
|
|
|
2017-09-28 04:17:06 -04:00
|
|
|
wifi_config_t wifi_config = {
|
2017-11-15 02:34:21 -05:00
|
|
|
.sta = {
|
|
|
|
.ssid = DEFAULT_SSID,
|
|
|
|
.password = DEFAULT_PWD,
|
2017-09-28 04:17:06 -04:00
|
|
|
.scan_method = DEFAULT_SCAN_METHOD,
|
|
|
|
.sort_method = DEFAULT_SORT_METHOD,
|
|
|
|
.threshold.rssi = DEFAULT_RSSI,
|
|
|
|
.threshold.authmode = DEFAULT_AUTHMODE,
|
2017-11-15 02:34:21 -05:00
|
|
|
},
|
2017-09-28 04:17:06 -04:00
|
|
|
};
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2017-09-28 04:17:06 -04:00
|
|
|
{
|
|
|
|
// Initialize NVS
|
|
|
|
esp_err_t ret = nvs_flash_init();
|
2018-07-25 11:11:09 -04:00
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
2017-09-28 04:17:06 -04:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK( ret );
|
|
|
|
|
|
|
|
wifi_scan();
|
|
|
|
}
|