2
0

esp_app_main.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. thread_cond_type wifi_connect_suspend_cond;
  29. mutex_type wifi_connect_suspend_mutex;
  30. char * art_wifi[]={
  31. "\n",
  32. "o `O ooOoOOo OOooOoO ooOoOOo\n",
  33. "O o O o O \n",
  34. "o O o O o \n",
  35. "O O O oOooO O \n",
  36. "o o o o O o \n",
  37. "O O O O o O \n",
  38. "`o O o O' O o O \n",
  39. " `OoO' `OoO' ooOOoOo O' ooOOoOo\n",
  40. "\n",
  41. ""
  42. };
  43. char * art_wifi_connecting[]={
  44. " .oOOOo.",
  45. ".O o o \n",
  46. "o O \n",
  47. "o oOo \n",
  48. "o .oOo. 'OoOo. 'OoOo. .oOo. .oOo o O 'OoOo. .oOoO \n",
  49. "O O o o O o O OooO' O O o o O o O \n",
  50. "`o .o o O O o O o O o o O O o O o \n",
  51. " `OoooO' `OoO' o O o O `OoO' `OoO' `oO o' o O `OoOo \n",
  52. " O \n",
  53. " OoO' \n",
  54. "\n",
  55. ""
  56. };
  57. char * art_wifi_connected[]={
  58. " .oOOOo. o oO\n",
  59. ".O o O OO\n",
  60. "o O o oO\n",
  61. "o oOo o Oo\n",
  62. "o .oOo. 'OoOo. 'OoOo. .oOo. .oOo o .oOo. .oOoO oO\n",
  63. "O O o o O o O OooO' O O OooO' o O \n",
  64. "`o .o o O O o O o O o o O O o Oo\n",
  65. " `OoooO' `OoO' o O o O `OoO' `OoO' `oO `OoO' `OoO'o oO\n",
  66. "\n",
  67. ""
  68. };
  69. /*Set the SSID and Password via "make menuconfig"*/
  70. #define DEFAULT_SSID CONFIG_WIFI_SSID
  71. #define DEFAULT_PWD CONFIG_WIFI_PASSWORD
  72. #if CONFIG_WIFI_ALL_CHANNEL_SCAN
  73. #define DEFAULT_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
  74. #elif CONFIG_WIFI_FAST_SCAN
  75. #define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
  76. #else
  77. #define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
  78. #endif /*CONFIG_SCAN_METHOD*/
  79. #if CONFIG_WIFI_CONNECT_AP_BY_SIGNAL
  80. #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
  81. #elif CONFIG_WIFI_CONNECT_AP_BY_SECURITY
  82. #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
  83. #else
  84. #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
  85. #endif /*CONFIG_SORT_METHOD*/
  86. #if CONFIG_FAST_SCAN_THRESHOLD
  87. #define DEFAULT_RSSI CONFIG_FAST_SCAN_MINIMUM_SIGNAL
  88. #if CONFIG_EXAMPLE_OPEN
  89. #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
  90. #elif CONFIG_EXAMPLE_WEP
  91. #define DEFAULT_AUTHMODE WIFI_AUTH_WEP
  92. #elif CONFIG_EXAMPLE_WPA
  93. #define DEFAULT_AUTHMODE WIFI_AUTH_WPA_PSK
  94. #elif CONFIG_EXAMPLE_WPA2
  95. #define DEFAULT_AUTHMODE WIFI_AUTH_WPA2_PSK
  96. #else
  97. #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
  98. #endif
  99. #else
  100. #define DEFAULT_RSSI -127
  101. #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
  102. #endif /*CONFIG_FAST_SCAN_THRESHOLD*/
  103. static const char *TAG = "scan";
  104. static void event_handler(void* arg, esp_event_base_t event_base,
  105. int32_t event_id, void* event_data)
  106. {
  107. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  108. esp_wifi_connect();
  109. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  110. esp_wifi_connect();
  111. } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  112. ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
  113. ESP_LOGI(TAG, "got ip: %s.", ip4addr_ntoa(&event->ip_info.ip));
  114. logprint("Signaling wifi connected. Locking.\n");
  115. mutex_lock(wifi_connect_suspend_mutex);
  116. logprint("Signaling wifi connected. Broadcasting.\n");
  117. mutex_broadcast_cond(wifi_connect_suspend_cond);
  118. logprint("Signaling wifi connected. Unlocking.\n");
  119. mutex_unlock(wifi_connect_suspend_mutex);
  120. }
  121. }
  122. /* Initialize Wi-Fi as sta and set scan method */
  123. static void wifi_scan(void)
  124. {
  125. for(uint8_t l=0;art_wifi[l][0]!='\0';l++){
  126. logprint("%s",art_wifi[l]);
  127. }
  128. for(uint8_t l=0;art_wifi_connecting[l][0]!='\0';l++){
  129. logprint("%s",art_wifi_connecting[l]);
  130. }
  131. tcpip_adapter_init();
  132. ESP_ERROR_CHECK(esp_event_loop_create_default());
  133. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  134. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  135. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
  136. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
  137. wifi_config_t wifi_config = {
  138. .sta = {
  139. .ssid = DEFAULT_SSID,
  140. .password = DEFAULT_PWD,
  141. .scan_method = DEFAULT_SCAN_METHOD,
  142. .sort_method = DEFAULT_SORT_METHOD,
  143. .threshold.rssi = DEFAULT_RSSI,
  144. .threshold.authmode = DEFAULT_AUTHMODE,
  145. },
  146. };
  147. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  148. ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  149. ESP_ERROR_CHECK(esp_wifi_start());
  150. }
  151. int main(int argc, char**argv);
  152. #define DO_EXPAND(VAL) VAL ## 1
  153. #define EXPAND(VAL) DO_EXPAND(VAL)
  154. void app_main()
  155. {
  156. int i;
  157. char **argv, *_argv[] = {
  158. "squeezelite-esp32",
  159. "-C",
  160. "1",
  161. "-o",
  162. CONFIG_OUTPUT_NAME,
  163. "-n",
  164. "ESP32",
  165. "-r",
  166. "OUTPUT_RATES",
  167. "-d",
  168. "slimproto=" CONFIG_LOGGING_SLIMPROTO,
  169. "-d",
  170. "stream=" CONFIG_LOGGING_STREAM,
  171. "-d",
  172. "decode=" CONFIG_LOGGING_DECODE,
  173. "-d",
  174. "output=" CONFIG_LOGGING_OUTPUT,
  175. "-b",
  176. "500:2000"
  177. };
  178. // can't do strtok on FLASH strings
  179. argv = malloc(sizeof(_argv));
  180. for (i = 0; i < sizeof(_argv)/sizeof(char*); i++) {
  181. argv[i] = strdup(_argv[i]);
  182. }
  183. // Initialize NVS
  184. esp_err_t ret = nvs_flash_init();
  185. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  186. ESP_ERROR_CHECK(nvs_flash_erase());
  187. ret = nvs_flash_init();
  188. }
  189. ESP_ERROR_CHECK( ret );
  190. mutex_create_p(wifi_connect_suspend_mutex);
  191. mutex_cond_init(wifi_connect_suspend_cond);
  192. logprint("Starting WiFi.\n");
  193. wifi_scan();
  194. logprint("Waiting for WiFi to connect. Locking Mutex.\n");
  195. mutex_lock(wifi_connect_suspend_mutex);
  196. logprint("Waiting for WiFi to connect. cond_wait.\n");
  197. pthread_cond_wait(&wifi_connect_suspend_cond,&wifi_connect_suspend_mutex);
  198. logprint("Waiting for WiFi to connect. Unlocking Mutex.\n");
  199. mutex_unlock(wifi_connect_suspend_mutex);
  200. for(uint8_t l=0;art_wifi[l][0]!='\0';l++){
  201. logprint("%s",art_wifi[l]);
  202. }
  203. for(uint8_t l=0;art_wifi_connected[l][0]!='\0';l++){
  204. logprint("%s",art_wifi_connected[l]);
  205. }
  206. logprint("%s %s:%d Calling main with parameters: " , logtime(), __FUNCTION__, __LINE__);
  207. for (i = 0; i < sizeof(_argv)/sizeof(char*); i++) {
  208. logprint("%s " , _argv[i]);
  209. }
  210. logprint("\n");
  211. main(sizeof(_argv)/sizeof(char*), argv);
  212. }