/* * wifi_service.c * * Created on: 26 May 2026 * Author: Christian Lind Vie Madsen */ #include "wifi_service.h" #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_wifi.h" #include "esp_event.h" #include "esp_log.h" #include "nvs_flash.h" #include "../board_defines/wifi_info.h" #include "lwip/err.h" #include "lwip/sys.h" #define ESP_HOSTNAME "ws_wifi_client" #define EXAMPLE_ESP_MAXIMUM_RETRY 5 /* FreeRTOS event group to signal when we are connected*/ static EventGroupHandle_t s_wifi_event_group; static int s_retry_num = 0; /* The event group allows multiple bits for each event, but we only care about two events: * - we are connected to the AP with an IP * - we failed to connect after the maximum amount of retries */ #define WIFI_CONNECTED_BIT BIT0 #define WIFI_FAIL_BIT BIT1 #define TAG "wifi_driver" static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { esp_wifi_connect(); } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) { esp_wifi_connect(); //s_retry_num++; ESP_LOGI(TAG, "retry to connect to the AP"); } else { xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); } ESP_LOGI(TAG, "connect to the AP fail"); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t *event = (ip_event_got_ip_t*) event_data; ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); s_retry_num = 0; xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); } } /** * @brief Get RSSI of currently connected WiFi AP * @return RSSI in dBm (e.g. -45), or 0 if not connected / error */ int wifi_get_rssi(void) { wifi_ap_record_t ap_info; esp_err_t err = esp_wifi_sta_get_ap_info(&ap_info); if (err != ESP_OK) { return -113; // not connected or error } return ap_info.rssi; } int wifi_IsConnected(){ EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY); if (bits & WIFI_CONNECTED_BIT) return 1; return 0; } void wifi_checkConnection(){ static uint32_t runtimer = 0; if((runtimer++ % 50) == 0){ EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY); /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually * happened. */ if (bits & WIFI_CONNECTED_BIT) { ESP_LOGI(TAG, "connected to ap "); } else if (bits & WIFI_FAIL_BIT) { ESP_LOGI(TAG, "Failed to connect to SSID"); ESP_ERROR_CHECK(esp_wifi_stop()); vTaskDelay(1000); ESP_ERROR_CHECK(esp_wifi_start()); } else { ESP_LOGE(TAG, "UNEXPECTED EVENT"); } } } int wifi_scan_to_buffer(char ssid[][33], int *rssi, int max_len) { uint16_t ap_count = 0; wifi_ap_record_t ap_info[25]; memset(ap_info, 0, sizeof(ap_info)); ESP_ERROR_CHECK(esp_wifi_scan_start(NULL, true)); ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count)); if (ap_count > max_len) { ap_count = max_len; } ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, ap_info)); for (int i = 0; i < ap_count; i++) { strncpy(ssid[i], (char *)ap_info[i].ssid, 13); ssid[i][13] = '\0'; // safety null-terminate rssi[i] = ap_info[i].rssi; } return ap_count; } void wifi_init_start(void) { 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(); } s_wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); esp_netif_t *netif = esp_netif_create_default_wifi_sta(); esp_netif_set_hostname(netif, ESP_HOSTNAME); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); esp_event_handler_instance_t instance_any_id; esp_event_handler_instance_t instance_got_ip; ESP_ERROR_CHECK( esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id)); ESP_ERROR_CHECK( esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip)); wifi_config_t wifi_config = { .sta = { .ssid = SSID, .password = PASSWORD, /* Setting a password implies station will connect to all security modes including WEP/WPA. * However these modes are deprecated and not advisable to be used. Incase your Access point * doesn't support WPA2, these mode can be enabled by commenting below line */ .threshold.authmode = WIFI_AUTH_WPA2_PSK, }, }; ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); ESP_LOGI(TAG, "wifi_init_sta finished."); }