From e00f3a9ede1b8e46b480bd68daf48da0bb08acae Mon Sep 17 00:00:00 2001 From: Filip Wandzio Date: Thu, 4 Sep 2025 01:11:11 +0200 Subject: Initial --- src/wifi.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/wifi.c (limited to 'src/wifi.c') diff --git a/src/wifi.c b/src/wifi.c new file mode 100644 index 0000000..8d6b29d --- /dev/null +++ b/src/wifi.c @@ -0,0 +1,49 @@ +#include "wifi.h" +#include "config.h" +#include "esp_event.h" +#include "esp_netif.h" +#include "esp_wifi.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include + +#define WIFI_SCAN_DELAY_MS 3000 + +/** + * @brief Initialize WiFi in station mode. + * + * This function performs the following steps: + * - Creates the default WiFi station network interface. + * - Initializes the WiFi driver with default configuration. + * - Sets WiFi mode to station. + * - Starts WiFi. + * - Starts scanning for available WiFi networks. + * - Waits for a predefined delay to allow scanning to complete. + * - Configures the WiFi station with SSID and password. + * - Connects to the configured WiFi network. + * + * The SSID and password are defined in the configuration headers. + */ +void wifi_init_sta(void) { + esp_netif_create_default_wifi_sta(); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + if (esp_wifi_init(&cfg) != ESP_OK) { + return; + } + + esp_wifi_set_mode(WIFI_MODE_STA); + esp_wifi_start(); + + wifi_scan_start(); + vTaskDelay(pdMS_TO_TICKS(WIFI_SCAN_DELAY_MS)); + + wifi_config_t wifi_config = {0}; + strncpy((char *)wifi_config.sta.ssid, WIFI_SSID, + sizeof(wifi_config.sta.ssid) - 1); + strncpy((char *)wifi_config.sta.password, WIFI_PASS, + sizeof(wifi_config.sta.password) - 1); + + esp_wifi_set_config(WIFI_IF_STA, &wifi_config); + esp_wifi_connect(); +} -- cgit v1.2.3