esp_app_main.c 4.8 KB

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