2026-01-13 18:30:18 +00:00
|
|
|
#include "esp_event.h"
|
|
|
|
|
#include "esp_log.h"
|
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
|
#include "freertos/task.h"
|
|
|
|
|
#include "secrets.h"
|
|
|
|
|
|
2026-03-01 15:21:45 +00:00
|
|
|
static const char *TAG = "wifi"; // Logs are coming from this wifi module
|
|
|
|
|
static uint32_t WIFI_CONNECTED = 0;
|
|
|
|
|
|
2026-01-13 18:30:18 +00:00
|
|
|
static int s_retry_num = 0;
|
|
|
|
|
|
|
|
|
|
#define MAX_RETRY 5
|
|
|
|
|
|
|
|
|
|
static void wifi_event_handler(void *arg, esp_event_base_t event_base,
|
2026-03-01 15:21:45 +00:00
|
|
|
int32_t event_id, void *event_data)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
|
|
|
|
|
{
|
2026-01-13 18:30:18 +00:00
|
|
|
esp_wifi_connect();
|
2026-03-01 15:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
|
|
|
|
|
{
|
|
|
|
|
if (s_retry_num < MAX_RETRY)
|
|
|
|
|
{
|
2026-01-13 18:30:18 +00:00
|
|
|
esp_wifi_connect();
|
|
|
|
|
s_retry_num++;
|
2026-03-01 15:21:45 +00:00
|
|
|
ESP_LOGI(TAG, "Trying to reconnect to WiFi.");
|
2026-01-13 18:30:18 +00:00
|
|
|
}
|
2026-03-01 15:21:45 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ESP_LOGI(TAG, "Could not connect to WiFi. Giving up.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
|
|
|
|
|
{
|
2026-01-13 18:30:18 +00:00
|
|
|
s_retry_num = 0;
|
2026-03-01 15:21:45 +00:00
|
|
|
WIFI_CONNECTED = 1;
|
2026-01-13 18:30:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 15:21:45 +00:00
|
|
|
void wifi_connect(void)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Attempting to connect to WiFi");
|
|
|
|
|
|
|
|
|
|
// Initialise network interface layer (Internet Layer) once Link Layer
|
|
|
|
|
// established (in standard TCP/IP architecture)
|
2026-01-13 18:30:18 +00:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2026-03-01 15:21:45 +00:00
|
|
|
|
|
|
|
|
// Create default event dispatcher
|
2026-01-13 18:30:18 +00:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
2026-03-01 15:21:45 +00:00
|
|
|
|
|
|
|
|
// Having established interface layer, create an interface instance
|
2026-01-13 18:30:18 +00:00
|
|
|
esp_netif_create_default_wifi_sta();
|
|
|
|
|
|
2026-03-01 15:21:45 +00:00
|
|
|
// Initialise WiFi driver (Link Layer)
|
2026-01-13 18:30:18 +00:00
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
2026-03-01 15:21:45 +00:00
|
|
|
|
2026-01-13 18:30:18 +00:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
|
|
|
|
2026-03-01 15:21:45 +00:00
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID,
|
|
|
|
|
&wifi_event_handler, NULL));
|
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID,
|
|
|
|
|
&wifi_event_handler, NULL));
|
2026-01-13 18:30:18 +00:00
|
|
|
|
|
|
|
|
wifi_config_t wifi_config = {
|
|
|
|
|
.sta =
|
|
|
|
|
{
|
|
|
|
|
.ssid = SECRETS_WIFI_SSID,
|
|
|
|
|
.password = SECRETS_WIFI_PASS,
|
|
|
|
|
},
|
|
|
|
|
};
|
2026-03-01 15:21:45 +00:00
|
|
|
|
|
|
|
|
// Set WiFi mode to 'station' (basically a client connecting to a WiFi access
|
|
|
|
|
// point)
|
2026-01-13 18:30:18 +00:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
2026-03-01 15:21:45 +00:00
|
|
|
|
|
|
|
|
// Give connection config to WiFi driver
|
2026-01-13 18:30:18 +00:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
2026-03-01 15:21:45 +00:00
|
|
|
|
|
|
|
|
// Begin the connection which triggers WIFI_EVENT_STA_START (the event which
|
|
|
|
|
// is the basis for the actual connection defined in the event handler)
|
2026-01-13 18:30:18 +00:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
|
|
|
|
|
2026-03-01 15:21:45 +00:00
|
|
|
// Block main execution until WiFi connection est
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Waiting for WiFi connection...");
|
2026-01-13 18:30:18 +00:00
|
|
|
|
2026-03-01 15:21:45 +00:00
|
|
|
while (!WIFI_CONNECTED)
|
|
|
|
|
{
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(100));
|
2026-01-13 18:30:18 +00:00
|
|
|
}
|
|
|
|
|
}
|