esp_app_main.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "platform_esp32.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include "esp_bt.h"
  25. #include "esp_bt_device.h"
  26. #include "esp_bt_main.h"
  27. #include "esp_gap_bt_api.h"
  28. #include "esp_a2dp_api.h"
  29. #include "esp_avrc_api.h"
  30. #include "esp_log.h"
  31. #include "esp_pthread.h"
  32. #include "esp_system.h"
  33. #include "esp_wifi.h"
  34. #include "freertos/FreeRTOS.h"
  35. #include "freertos/event_groups.h"
  36. #include "freertos/task.h"
  37. #include "freertos/timers.h"
  38. #include "nvs.h"
  39. #include "nvs_flash.h"
  40. #include "nvs_utilities.h"
  41. #include "pthread.h"
  42. #include "string.h"
  43. #include "sys/socket.h"
  44. #include <signal.h>
  45. #include "esp_system.h"
  46. #include <signal.h>
  47. /*Set the SSID and Password via "make menuconfig"*/
  48. #define DEFAULT_SSID CONFIG_WIFI_SSID
  49. #define DEFAULT_PWD CONFIG_WIFI_PASSWORD
  50. #if CONFIG_WIFI_ALL_CHANNEL_SCAN
  51. #define DEFAULT_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
  52. #elif CONFIG_WIFI_FAST_SCAN
  53. #define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
  54. #else
  55. #define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
  56. #endif /*CONFIG_SCAN_METHOD*/
  57. #if CONFIG_WIFI_CONNECT_AP_BY_SIGNAL
  58. #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
  59. #elif CONFIG_WIFI_CONNECT_AP_BY_SECURITY
  60. #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
  61. #else
  62. #define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
  63. #endif /*CONFIG_SORT_METHOD*/
  64. #if CONFIG_FAST_SCAN_THRESHOLD
  65. #define DEFAULT_RSSI CONFIG_FAST_SCAN_MINIMUM_SIGNAL
  66. #if CONFIG_EXAMPLE_OPEN
  67. #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
  68. #elif CONFIG_EXAMPLE_WEP
  69. #define DEFAULT_AUTHMODE WIFI_AUTH_WEP
  70. #elif CONFIG_EXAMPLE_WPA
  71. #define DEFAULT_AUTHMODE WIFI_AUTH_WPA_PSK
  72. #elif CONFIG_EXAMPLE_WPA2
  73. #define DEFAULT_AUTHMODE WIFI_AUTH_WPA2_PSK
  74. #else
  75. #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
  76. #endif
  77. #else
  78. #define DEFAULT_RSSI -127
  79. #define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
  80. #endif /*CONFIG_FAST_SCAN_THRESHOLD*/
  81. extern char current_namespace[];
  82. static const char * TAG = "platform_esp32";
  83. //static void event_handler(void* arg, esp_event_base_t event_base,
  84. // int32_t event_id, void* event_data)
  85. //{
  86. // if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  87. // esp_wifi_connect();
  88. // } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  89. // esp_wifi_connect();
  90. // } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  91. // ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
  92. // ESP_LOGI(TAG, "got ip: %s.", ip4addr_ntoa(&event->ip_info.ip));
  93. // ESP_LOGD(TAG,"Signaling wifi connected. Locking.\n");
  94. // pthread_mutex_lock(&wifi_connect_suspend_mutex);
  95. // ESP_LOGD(TAG,"Signaling wifi connected. Broadcasting.\n");
  96. // pthread_cond_broadcast(&wifi_connect_suspend_cond);
  97. // ESP_LOGD(TAG,"Signaling wifi connected. Unlocking.\n");
  98. // pthread_mutex_unlock(&wifi_connect_suspend_mutex);
  99. // }
  100. //}
  101. //
  102. ///* Initialize Wi-Fi as sta and set scan method */
  103. //static void wifi_scan(void)
  104. //{
  105. //
  106. // tcpip_adapter_init();
  107. // ESP_ERROR_CHECK(esp_event_loop_create_default());
  108. //
  109. // wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  110. // ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  111. //
  112. // ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
  113. // ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
  114. //
  115. // wifi_config_t wifi_config = {
  116. // .sta = {
  117. // .ssid = DEFAULT_SSID,
  118. // .password = DEFAULT_PWD,
  119. // .scan_method = DEFAULT_SCAN_METHOD,
  120. // .sort_method = DEFAULT_SORT_METHOD,
  121. // .threshold.rssi = DEFAULT_RSSI,
  122. // .threshold.authmode = DEFAULT_AUTHMODE,
  123. // },
  124. // };
  125. // ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  126. // ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  127. // ESP_ERROR_CHECK(esp_wifi_start());
  128. //}
  129. void app_main()
  130. {
  131. console_start();
  132. }