ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.1KB

  1. static const char *TAG = "WiFi";
  2. #include "esp_log.h"
  3. #include "wifi.hpp"
  4. // Defines SSID and PASS
  5. #include "wifi_cfg.hpp"
  6. static const int CONFIG_ESP_MAXIMUM_RETRY = 3;
  7. #include "esp_wifi.h"
  8. #include "freertos/event_groups.h"
  9. static EventGroupHandle_t s_wifi_event_group;
  10. #define WIFI_CONNECTED_BIT BIT0
  11. #define WIFI_FAIL_BIT BIT1
  12. static void event_handler(void* arg, esp_event_base_t event_base,
  13. int32_t event_id, void* event_data) {
  14. static int s_retry_num = 0;
  15. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  16. esp_wifi_connect();
  17. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  18. ESP_LOGI(TAG, "Disconnected, retrying connection to AP");
  19. esp_wifi_connect();
  20. } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  21. ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
  22. ESP_LOGI(TAG, "Got IP address: " IPSTR, IP2STR(&event->ip_info.ip));
  23. s_retry_num = 0;
  24. xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
  25. }
  26. }
  27. int config_wifi() {
  28. ESP_LOGI(TAG, "Connecting to %s", SSID);
  29. s_wifi_event_group = xEventGroupCreate();
  30. ESP_ERROR_CHECK(esp_netif_init());
  31. ESP_ERROR_CHECK(esp_event_loop_create_default());
  32. esp_netif_create_default_wifi_sta();
  33. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  34. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  35. esp_event_handler_instance_t instance_any_id;
  36. esp_event_handler_instance_t instance_got_ip;
  37. ESP_ERROR_CHECK(esp_event_handler_instance_register(
  38. WIFI_EVENT,
  39. ESP_EVENT_ANY_ID,
  40. &event_handler,
  41. NULL,
  42. &instance_any_id
  43. ));
  44. ESP_ERROR_CHECK(esp_event_handler_instance_register(
  45. IP_EVENT,
  46. IP_EVENT_STA_GOT_IP,
  47. &event_handler,
  48. NULL,
  49. &instance_got_ip
  50. ));
  51. wifi_config_t wifi_config = {
  52. .sta = {
  53. .ssid = SSID,
  54. .password = PASS,
  55. },
  56. };
  57. ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  58. ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
  59. ESP_ERROR_CHECK( esp_wifi_start() );
  60. /*
  61. // Waiting until either the connection is established (WIFI_CONNECTED_BIT)
  62. // or connection failed for the maximum number of re-tries (WIFI_FAIL_BIT).
  63. // The bits are set by event_handler() (see above)
  64. EventBits_t bits = xEventGroupWaitBits(
  65. s_wifi_event_group,
  66. WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
  67. pdFALSE,
  68. pdFALSE,
  69. portMAX_DELAY
  70. );
  71. // xEventGroupWaitBits() returns the bits before the call returned, hence we
  72. // can test which event actually happened.
  73. if (bits & WIFI_CONNECTED_BIT) {
  74. ESP_LOGI(TAG, "Connected to AP: %s", SSID);
  75. return 0;
  76. } else if (bits & WIFI_FAIL_BIT) {
  77. ESP_LOGE(TAG, "Failed to connect to SSID %s", SSID);
  78. } else {
  79. ESP_LOGE(TAG, "UNEXPECTED EVENT");
  80. }
  81. */
  82. return 0;
  83. }