diff options
| author | Filip Wandzio <contact@philw.dev> | 2025-09-04 01:11:11 +0200 |
|---|---|---|
| committer | Filip Wandzio <contact@philw.dev> | 2025-09-04 01:11:11 +0200 |
| commit | e00f3a9ede1b8e46b480bd68daf48da0bb08acae (patch) | |
| tree | f666b9aa4d0a03246ab9880e11802d91a23ab1e7 /src/wifi.c | |
| download | e1-e00f3a9ede1b8e46b480bd68daf48da0bb08acae.tar.gz e1-e00f3a9ede1b8e46b480bd68daf48da0bb08acae.zip | |
Initial
Diffstat (limited to 'src/wifi.c')
| -rw-r--r-- | src/wifi.c | 49 |
1 files changed, 49 insertions, 0 deletions
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 @@ | |||
| 1 | #include "wifi.h" | ||
| 2 | #include "config.h" | ||
| 3 | #include "esp_event.h" | ||
| 4 | #include "esp_netif.h" | ||
| 5 | #include "esp_wifi.h" | ||
| 6 | #include "freertos/FreeRTOS.h" | ||
| 7 | #include "freertos/task.h" | ||
| 8 | #include <string.h> | ||
| 9 | |||
| 10 | #define WIFI_SCAN_DELAY_MS 3000 | ||
| 11 | |||
| 12 | /** | ||
| 13 | * @brief Initialize WiFi in station mode. | ||
| 14 | * | ||
| 15 | * This function performs the following steps: | ||
| 16 | * - Creates the default WiFi station network interface. | ||
| 17 | * - Initializes the WiFi driver with default configuration. | ||
| 18 | * - Sets WiFi mode to station. | ||
| 19 | * - Starts WiFi. | ||
| 20 | * - Starts scanning for available WiFi networks. | ||
| 21 | * - Waits for a predefined delay to allow scanning to complete. | ||
| 22 | * - Configures the WiFi station with SSID and password. | ||
| 23 | * - Connects to the configured WiFi network. | ||
| 24 | * | ||
| 25 | * The SSID and password are defined in the configuration headers. | ||
| 26 | */ | ||
| 27 | void wifi_init_sta(void) { | ||
| 28 | esp_netif_create_default_wifi_sta(); | ||
| 29 | |||
| 30 | wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); | ||
| 31 | if (esp_wifi_init(&cfg) != ESP_OK) { | ||
| 32 | return; | ||
| 33 | } | ||
| 34 | |||
| 35 | esp_wifi_set_mode(WIFI_MODE_STA); | ||
| 36 | esp_wifi_start(); | ||
| 37 | |||
| 38 | wifi_scan_start(); | ||
| 39 | vTaskDelay(pdMS_TO_TICKS(WIFI_SCAN_DELAY_MS)); | ||
| 40 | |||
| 41 | wifi_config_t wifi_config = {0}; | ||
| 42 | strncpy((char *)wifi_config.sta.ssid, WIFI_SSID, | ||
| 43 | sizeof(wifi_config.sta.ssid) - 1); | ||
| 44 | strncpy((char *)wifi_config.sta.password, WIFI_PASS, | ||
| 45 | sizeof(wifi_config.sta.password) - 1); | ||
| 46 | |||
| 47 | esp_wifi_set_config(WIFI_IF_STA, &wifi_config); | ||
| 48 | esp_wifi_connect(); | ||
| 49 | } | ||
