wifi.c 4.9 KB

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