fast_scan.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Scan Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. /*
  8. This example shows how to use the All Channel Scan or Fast Scan to connect
  9. to a Wi-Fi network.
  10. In the Fast Scan mode, the scan will stop as soon as the first network matching
  11. the SSID is found. In this mode, an application can set threshold for the
  12. authentication mode and the Signal strength. Networks that do not meet the
  13. threshold requirements will be ignored.
  14. In the All Channel Scan mode, the scan will end only after all the channels
  15. are scanned, and connection will start with the best network. The networks
  16. can be sorted based on Authentication Mode or Signal Strength. The priority
  17. for the Authentication mode is: WPA2 > WPA > WEP > Open
  18. */
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/event_groups.h"
  21. #include "esp_wifi.h"
  22. #include "esp_log.h"
  23. #include "esp_event.h"
  24. #include "nvs_flash.h"
  25. /* Set the SSID and Password via project configuration, or can set directly here */
  26. #define DEFAULT_SSID "xxy"
  27. #define DEFAULT_PWD "123456x-"
  28. #if CONFIG_EXAMPLE_WIFI_ALL_CHANNEL_SCAN
  29. #define DEFAULT_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
  30. #elif CONFIG_EXAMPLE_WIFI_FAST_SCAN
  31. #define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
  32. #else
  33. #define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
  34. #endif /*CONFIG_EXAMPLE_SCAN_METHOD*/
  35. #if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL
  36. #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
  37. #elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY
  38. #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
  39. #else
  40. #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
  41. #endif /*CONFIG_EXAMPLE_SORT_METHOD*/
  42. #if CONFIG_EXAMPLE_FAST_SCAN_THRESHOLD
  43. #define DEFAULT_RSSI CONFIG_EXAMPLE_FAST_SCAN_MINIMUM_SIGNAL
  44. #if CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_OPEN
  45. #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
  46. #elif CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_WEP
  47. #define DEFAULT_AUTHMODE WIFI_AUTH_WEP
  48. #elif CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_WPA
  49. #define DEFAULT_AUTHMODE WIFI_AUTH_WPA_PSK
  50. #elif CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_WPA2
  51. #define DEFAULT_AUTHMODE WIFI_AUTH_WPA2_PSK
  52. #else
  53. #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
  54. #endif
  55. #else
  56. #define DEFAULT_RSSI -127
  57. #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
  58. #endif /*CONFIG_EXAMPLE_FAST_SCAN_THRESHOLD*/
  59. static const char *TAG = "scan";
  60. static void event_handler(void* arg, esp_event_base_t event_base,
  61. int32_t event_id, void* event_data)
  62. {
  63. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  64. esp_wifi_connect();
  65. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  66. esp_wifi_connect();
  67. } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  68. ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
  69. ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
  70. }
  71. }
  72. /* Initialize Wi-Fi as sta and set scan method */
  73. static void fast_scan(void)
  74. {
  75. ESP_ERROR_CHECK(esp_netif_init());
  76. ESP_ERROR_CHECK(esp_event_loop_create_default());
  77. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  78. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  79. ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, NULL));
  80. ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, NULL));
  81. // Initialize default station as network interface instance (esp-netif)
  82. esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
  83. assert(sta_netif);
  84. // Initialize and start WiFi
  85. wifi_config_t wifi_config = {
  86. .sta = {
  87. .ssid = DEFAULT_SSID,
  88. .password = DEFAULT_PWD,
  89. .scan_method = DEFAULT_SCAN_METHOD,
  90. .sort_method = DEFAULT_SORT_METHOD,
  91. .threshold.rssi = DEFAULT_RSSI,
  92. .threshold.authmode = DEFAULT_AUTHMODE,
  93. },
  94. };
  95. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  96. ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
  97. ESP_ERROR_CHECK(esp_wifi_start());
  98. }
  99. void app_main(void)
  100. {
  101. // Initialize NVS
  102. esp_err_t ret = nvs_flash_init();
  103. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  104. ESP_ERROR_CHECK(nvs_flash_erase());
  105. ret = nvs_flash_init();
  106. }
  107. ESP_ERROR_CHECK( ret );
  108. fast_scan();
  109. int i = 0;
  110. while (1) {
  111. ESP_LOGI(TAG, "Scan done");
  112. ESP_LOGI(TAG, "AP number:%d", i++);
  113. vTaskDelay(1000 / portTICK_PERIOD_MS);
  114. }
  115. }