esp32-lilygo-t5/main/server.c
2025-10-05 10:37:12 +02:00

49 lines
1.4 KiB
C

#include "server.h"
#include "settings.h"
httpd_handle_t get_server(void);
static void wifi_init_sta(void) {
// Initialize the ESP-NETIF
esp_netif_init();
esp_event_loop_create_default();
// Create default event loop
esp_netif_create_default_wifi_sta();
// Initialize the Wi-Fi driver
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
// Set Wi-Fi mode to station
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
// Configure Wi-Fi connection
wifi_config_t wifi_config = {
// TODO: Allow setting SSID at boot or build time
// For some reason I could not get https://cmake.org/cmake/help/latest/command/add_compile_definitions.html#command:add_compile_definitions to work
.sta = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
},
};
// Set the Wi-Fi configuration
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_ERROR_CHECK(esp_wifi_connect());
}
httpd_handle_t start_webserver(void) {
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.lru_purge_enable = true;
httpd_handle_t server = NULL;
ESP_ERROR_CHECK(httpd_start(&server, &config));
return server;
}
httpd_handle_t get_server(void) {
wifi_init_sta();
return start_webserver();
}