wifi.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* WiFi station 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. #include <string.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/event_groups.h"
  11. #if ESP_IDF_VERSION_MAJOR >= 5
  12. #include "esp_chip_info.h"
  13. #else
  14. #include "esp_system.h"
  15. #endif
  16. #include "esp_log.h"
  17. #include "driver/gpio.h"
  18. #include "wifi.h"
  19. #ifdef WIFI_ENABLED
  20. #include "nvs_flash.h"
  21. #include "esp_wifi.h"
  22. #include "esp_event.h"
  23. #include "lwip/err.h"
  24. #include "lwip/sys.h"
  25. // These are configured in idf.py menuconfig
  26. #define DEFAULT_SSID CONFIG_ESP_WIFI_SSID
  27. #define DEFAULT_PWD CONFIG_ESP_WIFI_PASSWORD
  28. static const char *TAG = "wifi";
  29. int latestRSSIValue = 0;
  30. int latestWiFiChannel = -1;
  31. /*******
  32. * 0: Not initialized
  33. * 5: Waiting for a connection, or other event
  34. * 10: WIFI_EVENT_STA_START
  35. * 15: Waiting for a connection
  36. * 20: WIFI_EVENT_STA_DISCONNECTED
  37. * 30: IP_EVENT_STA_GOT_IP
  38. * 35: All is up and running
  39. */
  40. static int wifiState = 0; // Local wifi-state
  41. int commIsUpAndRunning = 0; // Global info
  42. static void event_handler(void* arg, esp_event_base_t event_base,int32_t event_id, void* event_data)
  43. {
  44. ESP_LOGI(TAG, "Got event: %s ID:%d",event_base, event_id);
  45. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  46. wifiState = 10;
  47. }
  48. else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  49. wifiState = 20;
  50. }
  51. else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
  52. wifiState = 30;
  53. }
  54. }
  55. static void setupAndConfigureWiFi(void)
  56. {
  57. ESP_ERROR_CHECK(esp_netif_init());
  58. ESP_ERROR_CHECK(esp_event_loop_create_default());
  59. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  60. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  61. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
  62. ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
  63. // Initialize default station as network interface instance (esp-netif)
  64. esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
  65. assert(sta_netif);
  66. // Initialize and start WiFi
  67. wifi_config_t wifi_config = {
  68. .sta = {
  69. .ssid = DEFAULT_SSID,
  70. .password = DEFAULT_PWD,
  71. .scan_method = WIFI_ALL_CHANNEL_SCAN,
  72. .sort_method = WIFI_CONNECT_AP_BY_SIGNAL,
  73. .threshold.rssi = -127,
  74. .threshold.authmode = WIFI_AUTH_OPEN,
  75. },
  76. };
  77. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  78. ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  79. ESP_ERROR_CHECK(esp_wifi_start());
  80. }
  81. void wifiTask(void *pvParameters)
  82. {
  83. ESP_LOGI("WIFI", "wifiTask starting. Core:%d\n",xPortGetCoreID());
  84. //Initialize NVS
  85. esp_err_t ret = nvs_flash_init();
  86. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  87. ESP_ERROR_CHECK(nvs_flash_erase());
  88. ret = nvs_flash_init();
  89. }
  90. ESP_ERROR_CHECK(ret);
  91. static TickType_t lastLogTime = 0;
  92. while(1) {
  93. switch (wifiState)
  94. {
  95. case 0:
  96. // Time to setup everything
  97. ESP_LOGI(TAG, "wifiTask state 0. Init-phase");
  98. wifiState = 5;
  99. setupAndConfigureWiFi();
  100. break;
  101. case 10:
  102. // Try to connect
  103. ESP_LOGI(TAG, "wifiTask state 10: Try to scan and connect");
  104. wifiState = 15;
  105. esp_wifi_connect();
  106. break;
  107. case 20:
  108. // We got disconnected ??
  109. commIsUpAndRunning = 0;
  110. ESP_LOGI(TAG, "wifiTask state 20: Disconnected");
  111. wifiState = 15;
  112. esp_wifi_connect();
  113. break;
  114. case 30:
  115. // We got IP
  116. commIsUpAndRunning = 1;
  117. wifiState = 35;
  118. ESP_LOGI(TAG, "wifiTask state 30. We got IP");
  119. break;
  120. case 35:
  121. // Everything is fine !
  122. if( (xTaskGetTickCount()*portTICK_PERIOD_MS) > (lastLogTime + 500) ) {
  123. static int logCounter = 5;
  124. lastLogTime = xTaskGetTickCount() * portTICK_PERIOD_MS;
  125. wifi_ap_record_t wifidata;
  126. if (esp_wifi_sta_get_ap_info(&wifidata)==0){
  127. if( --logCounter == 0 ) {
  128. //ESP_LOGI(TAG, "WiFi RSSI:%d Ch:%d",wifidata.rssi, wifidata.primary);
  129. logCounter=60;
  130. }
  131. latestRSSIValue = wifidata.rssi;
  132. latestWiFiChannel = wifidata.primary;
  133. }
  134. }
  135. break;
  136. case 5:
  137. case 15:
  138. default:
  139. break;
  140. }
  141. vTaskDelay(250 / portTICK_PERIOD_MS);
  142. }
  143. vTaskDelete(NULL);
  144. }
  145. void initWifi(void)
  146. {
  147. xTaskCreatePinnedToCore(wifiTask, "WiFi-Task", 1024*10, NULL, 2, NULL,0);
  148. }
  149. #else
  150. void initWifi(void) {};
  151. #endif