diff --git a/.gitmodules b/.gitmodules index 8cfcc27..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "device/components/arduino-esp32"] - path = device/components/arduino-esp32 - url = https://github.com/espressif/arduino-esp32.git \ No newline at end of file diff --git a/device/components/arduino-esp32 b/device/components/arduino-esp32 deleted file mode 160000 index 16a9cf7..0000000 --- a/device/components/arduino-esp32 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 16a9cf781fafffedd70b794beed24853965d78ce diff --git a/device/components/esp-idf-rc522/rc522.h b/device/components/esp-idf-rc522/rc522.h index 6df5d80..678ea26 100644 --- a/device/components/esp-idf-rc522/rc522.h +++ b/device/components/esp-idf-rc522/rc522.h @@ -10,7 +10,12 @@ extern "C" { #define RC522_DEFAULT_MOSI (23) #define RC522_DEFAULT_SCK (19) #define RC522_DEFAULT_SDA (22) -#define RC522_DEFAULT_SPI_HOST (VSPI_HOST) +/* VSPI_HOST was renamed to SPI3_HOST in ESP-IDF 5.0 */ +#ifdef VSPI_HOST +# define RC522_DEFAULT_SPI_HOST (VSPI_HOST) +#else +# define RC522_DEFAULT_SPI_HOST (SPI3_HOST) +#endif #define RC522_DEFAULT_SCAN_INTERVAL_MS (125) #define RC522_DEFAULT_TACK_STACK_SIZE (4 * 1024) #define RC522_DEFAULT_TACK_STACK_PRIORITY (4) diff --git a/device/main/CMakeLists.txt b/device/main/CMakeLists.txt index 30122c3..57cd7f5 100644 --- a/device/main/CMakeLists.txt +++ b/device/main/CMakeLists.txt @@ -9,4 +9,4 @@ idf_component_register(SRCS "BluetoothInterface.cpp" "audio_setup.c" INCLUDE_DIRS "." - REQUIRES esp_http_client esp_netif) + REQUIRES esp_http_client esp_netif esp_wifi nvs_flash) diff --git a/device/main/Networking.cpp b/device/main/Networking.cpp index 0e04bd4..6c524fd 100644 --- a/device/main/Networking.cpp +++ b/device/main/Networking.cpp @@ -1,26 +1,89 @@ #include "Networking.hpp" -#include "WiFi.h" #include +#include +#include +#include +#include +#include +#include +#include static const char* TAG = "Networking"; -static constexpr int WIFI_TIMEOUT_MS = 30000; -static constexpr int WIFI_POLL_MS = 500; +static EventGroupHandle_t s_wifi_event_group; +static const int CONNECTED_BIT = BIT0; +static int s_retries = 0; -void Networking::connectWifi() { - WiFi.begin(CONFIG_ESP_WIFI_SSID, CONFIG_ESP_WIFI_PASSWORD); - - int elapsed = 0; - while(WiFi.status() != WL_CONNECTED && elapsed < WIFI_TIMEOUT_MS) { - vTaskDelay(WIFI_POLL_MS / portTICK_PERIOD_MS); - elapsed += WIFI_POLL_MS; - ESP_LOGI(TAG, "Waiting for WiFi... (%d ms)", elapsed); - } - - if(WiFi.status() == WL_CONNECTED) { - ESP_LOGI(TAG, "Connected. IP: %s", WiFi.localIP().toString().c_str()); - } else { - ESP_LOGW(TAG, "WiFi connection timed out — running without network"); +static void wifi_event_handler(void*, esp_event_base_t base, int32_t id, void* data) { + if(base == WIFI_EVENT && id == WIFI_EVENT_STA_START) { + esp_wifi_connect(); + } else if(base == WIFI_EVENT && id == WIFI_EVENT_STA_DISCONNECTED) { + if(s_retries < CONFIG_ESP_MAXIMUM_RETRY) { + esp_wifi_connect(); + s_retries++; + ESP_LOGI(TAG, "Reconnect attempt %d/%d", s_retries, CONFIG_ESP_MAXIMUM_RETRY); + } else { + ESP_LOGW(TAG, "Could not connect after %d attempts", CONFIG_ESP_MAXIMUM_RETRY); + } + } else if(base == IP_EVENT && id == IP_EVENT_STA_GOT_IP) { + ip_event_got_ip_t* ev = (ip_event_got_ip_t*)data; + ESP_LOGI(TAG, "Connected. IP: " IPSTR, IP2STR(&ev->ip_info.ip)); + s_retries = 0; + xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT); } } + +void Networking::connectWifi() { + /* NVS is required by the WiFi driver */ + esp_err_t err = nvs_flash_init(); + if(err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) { + nvs_flash_erase(); + nvs_flash_init(); + } + + err = esp_netif_init(); + if(err != ESP_OK && err != ESP_ERR_INVALID_STATE) { + ESP_LOGE(TAG, "esp_netif_init: %s", esp_err_to_name(err)); + } + + err = esp_event_loop_create_default(); + if(err != ESP_OK && err != ESP_ERR_INVALID_STATE) { + ESP_LOGE(TAG, "esp_event_loop_create_default: %s", esp_err_to_name(err)); + } + + esp_netif_create_default_wifi_sta(); + + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); + esp_wifi_init(&cfg); + + s_wifi_event_group = xEventGroupCreate(); + s_retries = 0; + + esp_event_handler_instance_t inst_any, inst_ip; + esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, NULL, &inst_any); + esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, NULL, &inst_ip); + + wifi_config_t wifi_cfg = {}; + strncpy((char*)wifi_cfg.sta.ssid, CONFIG_ESP_WIFI_SSID, sizeof(wifi_cfg.sta.ssid) - 1); + strncpy((char*)wifi_cfg.sta.password, CONFIG_ESP_WIFI_PASSWORD, sizeof(wifi_cfg.sta.password) - 1); + wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK; + + esp_wifi_set_mode(WIFI_MODE_STA); + esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg); + esp_wifi_start(); + + EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, CONNECTED_BIT, + pdFALSE, pdFALSE, pdMS_TO_TICKS(30000)); + if(!(bits & CONNECTED_BIT)) { + ESP_LOGW(TAG, "WiFi not connected after 30 s — continuing without network"); + } + + vEventGroupDelete(s_wifi_event_group); + s_wifi_event_group = NULL; +} + +bool Networking::isConnected() const { + wifi_ap_record_t ap; + return esp_wifi_sta_get_ap_info(&ap) == ESP_OK; +} diff --git a/device/main/Networking.hpp b/device/main/Networking.hpp index a566246..e3387df 100644 --- a/device/main/Networking.hpp +++ b/device/main/Networking.hpp @@ -1,9 +1,10 @@ -#ifndef _NETWORKING_HPP -#define _NETWORKING_HPP +#pragma once class Networking { - public: - void connectWifi(); -}; +public: + /* Connects to the WiFi configured in Kconfig (CONFIG_ESP_WIFI_SSID / _PASSWORD). + * Blocks up to 30 s, then returns regardless of connection state. */ + void connectWifi(); -#endif \ No newline at end of file + bool isConnected() const; +}; diff --git a/device/main/WebInterface.cpp b/device/main/WebInterface.cpp index 7f361c9..34d3b74 100644 --- a/device/main/WebInterface.cpp +++ b/device/main/WebInterface.cpp @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/device/main/main.cpp b/device/main/main.cpp index 95bd4bd..c8f3d3c 100644 --- a/device/main/main.cpp +++ b/device/main/main.cpp @@ -1,5 +1,3 @@ -#include "Arduino.h" - #include "Networking.hpp" #include "WebInterface.hpp" #include "RFIDInterface.hpp" @@ -25,8 +23,6 @@ std::shared_ptr cacheManager; std::shared_ptr tagPlayerControl; void app_main(void) { - initArduino(); - /* SD card must be mounted first — other components depend on it */ webInterface.initSD();