feat: Implement Ethernet and Wi-Fi network connectivity with LED status indication and configuration.

This commit is contained in:
2026-03-02 16:18:50 -05:00
parent 056c86644f
commit bad32f33ce
5 changed files with 237 additions and 17 deletions

View File

@@ -6,6 +6,7 @@
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs_flash.h"
#include "sdkconfig.h"
#include "soc/gpio_num.h"
@@ -15,16 +16,19 @@
// TODO : Make it configurable
internal constexpr bool blockUntilEthernetEstablished = false;
internal constexpr uint8_t maxEthernetRetries = 5;
internal bool ethernetInitialized = false;
internal bool wifiInitialized = false;
extern "C" void app_main() {
printf("Hello, Worldi!\n");
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
setup_led();
set_led_status(led_status::Connecting);
set_led_status(led_status::ConnectingEthernet);
ethernetInitialized = true;
esp_err_t result = connect_ethernet(blockUntilEthernetEstablished);
if (result != ESP_OK) {
set_led_status(led_status::Failed);
@@ -36,7 +40,7 @@ extern "C" void app_main() {
if (!blockUntilEthernetEstablished) {
uint8 retries = 1;
do {
set_led_status(led_status::Connecting);
set_led_status(led_status::ConnectingEthernet);
result = check_ethernet_connection(retries);
if (result != ESP_OK) {
@@ -45,25 +49,73 @@ extern "C" void app_main() {
}
retries++;
} while (result == ESP_ERR_TIMEOUT && retries <= maxEthernetRetries);
} while (result == ESP_ERR_TIMEOUT &&
retries <= CONFIG_CALENDINK_ETH_RETRIES);
}
if (result != ESP_OK) {
// TODO : Wifi connection
// Needs to disconnect ethernet at that point if we go wifi
// If wifi failes -> total shutdown
goto shutdown;
printf("Ethernet failed, trying wifi\n");
disconnect_ethernet();
ethernetInitialized = false;
set_led_status(led_status::ConnectingWifi);
wifiInitialized = true;
result =
connect_wifi(CONFIG_CALENDINK_WIFI_SSID, CONFIG_CALENDINK_WIFI_PASSWORD,
blockUntilEthernetEstablished);
if (result != ESP_OK) {
set_led_status(led_status::Failed);
vTaskDelay(pdMS_TO_TICKS(1000));
goto shutdown;
}
if (!blockUntilEthernetEstablished) {
uint8 retries = 1;
do {
set_led_status(led_status::ConnectingWifi);
result = check_wifi_connection(retries);
if (result != ESP_OK) {
set_led_status(led_status::Failed);
vTaskDelay(pdMS_TO_TICKS(1000));
}
retries++;
} while (result == ESP_ERR_TIMEOUT &&
retries <= CONFIG_CALENDINK_WIFI_RETRIES);
}
if (result != ESP_OK) {
printf("Wifi failed.\n");
goto shutdown;
}
set_led_status(led_status::ReadyWifi);
printf("Will use Wifi!\n");
} else {
set_led_status(led_status::ReadyEthernet);
printf("Will use Ethernet!\n");
}
set_led_status(led_status::Ready);
vTaskDelay(pdMS_TO_TICKS(1000));
printf("Connected!\n");
vTaskDelay(pdMS_TO_TICKS(5000));
// TODO Main loop
shutdown:
disconnect_ethernet();
printf("Shutting down.\n");
if (ethernetInitialized) {
disconnect_ethernet();
ethernetInitialized = false;
}
if (wifiInitialized) {
disconnect_wifi();
wifiInitialized = false;
}
destroy_led();
ESP_ERROR_CHECK(esp_event_loop_delete_default());
ESP_ERROR_CHECK(nvs_flash_deinit());
}