Remove arduino-esp32 dependency, modernise WiFi and SPI compat

- Replace Arduino WiFi.h in Networking.cpp with native esp_wifi/esp_netif;
  uses event-driven connect with FreeRTOS EventGroup and 30 s timeout
- Remove initArduino() / Arduino.h from main.cpp
- Remove deprecated arduino-esp32 submodule (was uninitialized and marked
  deprecated since it was first added in Dec 2021)
- rc522.h: guard VSPI_HOST → SPI3_HOST for ESP-IDF 5.x compatibility
- WebInterface.cpp: drop redundant esp_wifi.h (esp_netif.h is sufficient)
- CMakeLists.txt: add esp_wifi + nvs_flash to REQUIRES

https://claude.ai/code/session_01MhAnh68DPNnTjcKqjNYDmk
This commit is contained in:
Claude committed 2026-06-12 20:34:52 +00:00
1 parent 4f9946bd17
commit afbc0e5e23
8 files changed
+95 -34

No files matched your search

+6 -1
View File
@@ -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)
+1 -1
View File
@@ -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)
+80 -17
View File
@@ -1,26 +1,89 @@
#include "Networking.hpp"
#include "WiFi.h"
#include <esp_log.h>
#include <esp_wifi.h>
#include <esp_event.h>
#include <esp_netif.h>
#include <nvs_flash.h>
#include <freertos/FreeRTOS.h>
#include <freertos/event_groups.h>
#include <string.h>
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;
}
+7 -6
View File
@@ -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
bool isConnected() const;
};
+1 -1
View File
@@ -8,7 +8,7 @@
#include <board.h>
#include <periph_sdcard.h>
#include <esp_system.h>
#include <esp_wifi.h>
#include <esp_netif.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
-4
View File
@@ -1,5 +1,3 @@
#include "Arduino.h"
#include "Networking.hpp"
#include "WebInterface.hpp"
#include "RFIDInterface.hpp"
@@ -25,8 +23,6 @@ std::shared_ptr<CacheManager> cacheManager;
std::shared_ptr<TagPlayerControl> tagPlayerControl;
void app_main(void) {
initArduino();
/* SD card must be mounted first — other components depend on it */
webInterface.initSD();