scan.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #include "sys/socket.h"
  26. #include "string.h"
  27. /*Set the SSID and Password via "make menuconfig"*/
  28. #define DEFAULT_SSID CONFIG_WIFI_SSID
  29. #define DEFAULT_PWD CONFIG_WIFI_PASSWORD
  30. #if CONFIG_WIFI_ALL_CHANNEL_SCAN
  31. #define DEFAULT_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
  32. #elif CONFIG_WIFI_FAST_SCAN
  33. #define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
  34. #else
  35. #define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
  36. #endif /*CONFIG_SCAN_METHOD*/
  37. #if CONFIG_WIFI_CONNECT_AP_BY_SIGNAL
  38. #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
  39. #elif CONFIG_WIFI_CONNECT_AP_BY_SECURITY
  40. #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
  41. #else
  42. #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
  43. #endif /*CONFIG_SORT_METHOD*/
  44. #if CONFIG_FAST_SCAN_THRESHOLD
  45. #define DEFAULT_RSSI CONFIG_FAST_SCAN_MINIMUM_SIGNAL
  46. #if CONFIG_EXAMPLE_OPEN
  47. #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
  48. #elif CONFIG_EXAMPLE_WEP
  49. #define DEFAULT_AUTHMODE WIFI_AUTH_WEP
  50. #elif CONFIG_EXAMPLE_WPA
  51. #define DEFAULT_AUTHMODE WIFI_AUTH_WPA_PSK
  52. #elif CONFIG_EXAMPLE_WPA2
  53. #define DEFAULT_AUTHMODE WIFI_AUTH_WPA2_PSK
  54. #else
  55. #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
  56. #endif
  57. #else
  58. #define DEFAULT_RSSI -127
  59. #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
  60. #endif /*CONFIG_FAST_SCAN_THRESHOLD*/
  61. static const char *TAG = "scan";
  62. static void event_handler(void* arg, esp_event_base_t event_base,
  63. int32_t event_id, void* event_data)
  64. {
  65. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  66. esp_wifi_connect();
  67. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  68. esp_wifi_connect();
  69. } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  70. ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
  71. ESP_LOGI(TAG, "got ip: %s", ip4addr_ntoa(&event->ip_info.ip));
  72. }
  73. }
  74. /* Initialize Wi-Fi as sta and set scan method */
  75. static void wifi_scan(void)
  76. {
  77. tcpip_adapter_init();
  78. ESP_ERROR_CHECK(esp_event_loop_create_default());
  79. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  80. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  81. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
  82. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
  83. wifi_config_t wifi_config = {
  84. .sta = {
  85. .ssid = DEFAULT_SSID,
  86. .password = DEFAULT_PWD,
  87. .scan_method = DEFAULT_SCAN_METHOD,
  88. .sort_method = DEFAULT_SORT_METHOD,
  89. .threshold.rssi = DEFAULT_RSSI,
  90. .threshold.authmode = DEFAULT_AUTHMODE,
  91. },
  92. };
  93. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  94. ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  95. ESP_ERROR_CHECK(esp_wifi_start());
  96. }
  97. int main(int argc, char**argv);
  98. void app_main()
  99. {
  100. int i;
  101. char **argv, *_argv[] = {
  102. "squeezelite-esp32",
  103. "-n",
  104. "ESP32",
  105. "-d",
  106. "all=info",
  107. "-d",
  108. "slimproto=debug",
  109. "-b",
  110. "128:2000",
  111. };
  112. // can't do strtok on FLASH strings
  113. argv = malloc(sizeof(_argv));
  114. for (i = 0; i < sizeof(_argv)/sizeof(char*); i++) {
  115. argv[i] = strdup(_argv[i]);
  116. }
  117. // Initialize NVS
  118. esp_err_t ret = nvs_flash_init();
  119. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  120. ESP_ERROR_CHECK(nvs_flash_erase());
  121. ret = nvs_flash_init();
  122. }
  123. ESP_ERROR_CHECK( ret );
  124. wifi_scan();
  125. main(sizeof(_argv)/sizeof(char*), argv);
  126. }