/* WiFi station Example This example code is in the Public Domain (or CC0 licensed, at your option.) Unless required by applicable law or agreed to in writing, this software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_log.h" #include "driver/gpio.h" #include "wifi.h" #ifdef WIFI_ENABLED #include "nvs_flash.h" #include "esp_wifi.h" #include "esp_event.h" #include "lwip/err.h" #include "lwip/sys.h" // These are configured in idf.py menuconfig #define DEFAULT_SSID CONFIG_ESP_WIFI_SSID #define DEFAULT_PWD CONFIG_ESP_WIFI_PASSWORD static const char *TAG = "wifi"; int latestRSSIValue = 0; int latestWiFiChannel = -1; /******* * 0: Not initialized * 5: Waiting for a connection, or other event * 10: WIFI_EVENT_STA_START * 15: Waiting for a connection * 20: WIFI_EVENT_STA_DISCONNECTED * 30: IP_EVENT_STA_GOT_IP * 35: All is up and running */ static int wifiState = 0; // Local wifi-state int commIsUpAndRunning = 0; // Global info static void event_handler(void* arg, esp_event_base_t event_base,int32_t event_id, void* event_data) { ESP_LOGI(TAG, "Got event: %s ID:%d",event_base, event_id); if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { wifiState = 10; } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { wifiState = 20; } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { wifiState = 30; } } static void setupAndConfigureWiFi(void) { ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL)); // Initialize default station as network interface instance (esp-netif) esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); assert(sta_netif); // Initialize and start WiFi wifi_config_t wifi_config = { .sta = { .ssid = DEFAULT_SSID, .password = DEFAULT_PWD, .scan_method = WIFI_ALL_CHANNEL_SCAN, .sort_method = WIFI_CONNECT_AP_BY_SIGNAL, .threshold.rssi = -127, .threshold.authmode = WIFI_AUTH_OPEN, }, }; ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); } void wifiTask(void *pvParameters) { ESP_LOGI("WIFI", "wifiTask starting. Core:%d\n",xPortGetCoreID()); //Initialize NVS esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } ESP_ERROR_CHECK(ret); static TickType_t lastLogTime = 0; while(1) { switch (wifiState) { case 0: // Time to setup everything ESP_LOGI(TAG, "wifiTask state 0. Init-phase"); wifiState = 5; setupAndConfigureWiFi(); break; case 10: // Try to connect ESP_LOGI(TAG, "wifiTask state 10: Try to scan and connect"); wifiState = 15; esp_wifi_connect(); break; case 20: // We got disconnected ?? commIsUpAndRunning = 0; ESP_LOGI(TAG, "wifiTask state 20: Disconnected"); wifiState = 15; esp_wifi_connect(); break; case 30: // We got IP commIsUpAndRunning = 1; wifiState = 35; ESP_LOGI(TAG, "wifiTask state 30. We got IP"); break; case 35: // Everything is fine ! if( (xTaskGetTickCount()*portTICK_PERIOD_MS) > (lastLogTime + 500) ) { static int logCounter = 5; lastLogTime = xTaskGetTickCount() * portTICK_PERIOD_MS; wifi_ap_record_t wifidata; if (esp_wifi_sta_get_ap_info(&wifidata)==0){ if( --logCounter == 0 ) { //ESP_LOGI(TAG, "WiFi RSSI:%d Ch:%d",wifidata.rssi, wifidata.primary); logCounter=60; } latestRSSIValue = wifidata.rssi; latestWiFiChannel = wifidata.primary; } } break; case 5: case 15: default: break; } vTaskDelay(250 / portTICK_PERIOD_MS); } vTaskDelete(NULL); } void initWifi(void) { xTaskCreatePinnedToCore(wifiTask, "WiFi-Task", 1024*10, NULL, 2, NULL,0); } #endif