mirror of
https://github.com/sweidac/kidsbox.git
synced 2026-10-28 06:52:57 +01:00
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:
8 files changed
+89
-28
No files matched your search
@@ -1,3 +0,0 @@
|
|||||||
[submodule "device/components/arduino-esp32"]
|
|
||||||
path = device/components/arduino-esp32
|
|
||||||
url = https://github.com/espressif/arduino-esp32.git
|
|
||||||
Submodule device/components/arduino-esp32 deleted from 16a9cf781f.
@@ -10,7 +10,12 @@ extern "C" {
|
|||||||
#define RC522_DEFAULT_MOSI (23)
|
#define RC522_DEFAULT_MOSI (23)
|
||||||
#define RC522_DEFAULT_SCK (19)
|
#define RC522_DEFAULT_SCK (19)
|
||||||
#define RC522_DEFAULT_SDA (22)
|
#define RC522_DEFAULT_SDA (22)
|
||||||
|
/* VSPI_HOST was renamed to SPI3_HOST in ESP-IDF 5.0 */
|
||||||
|
#ifdef VSPI_HOST
|
||||||
# define RC522_DEFAULT_SPI_HOST (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_SCAN_INTERVAL_MS (125)
|
||||||
#define RC522_DEFAULT_TACK_STACK_SIZE (4 * 1024)
|
#define RC522_DEFAULT_TACK_STACK_SIZE (4 * 1024)
|
||||||
#define RC522_DEFAULT_TACK_STACK_PRIORITY (4)
|
#define RC522_DEFAULT_TACK_STACK_PRIORITY (4)
|
||||||
|
|||||||
@@ -9,4 +9,4 @@ idf_component_register(SRCS
|
|||||||
"BluetoothInterface.cpp"
|
"BluetoothInterface.cpp"
|
||||||
"audio_setup.c"
|
"audio_setup.c"
|
||||||
INCLUDE_DIRS "."
|
INCLUDE_DIRS "."
|
||||||
REQUIRES esp_http_client esp_netif)
|
REQUIRES esp_http_client esp_netif esp_wifi nvs_flash)
|
||||||
+77
-14
@@ -1,26 +1,89 @@
|
|||||||
#include "Networking.hpp"
|
#include "Networking.hpp"
|
||||||
|
|
||||||
#include "WiFi.h"
|
|
||||||
#include <esp_log.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 const char* TAG = "Networking";
|
||||||
|
|
||||||
static constexpr int WIFI_TIMEOUT_MS = 30000;
|
static EventGroupHandle_t s_wifi_event_group;
|
||||||
static constexpr int WIFI_POLL_MS = 500;
|
static const int CONNECTED_BIT = BIT0;
|
||||||
|
static int s_retries = 0;
|
||||||
|
|
||||||
|
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() {
|
void Networking::connectWifi() {
|
||||||
WiFi.begin(CONFIG_ESP_WIFI_SSID, CONFIG_ESP_WIFI_PASSWORD);
|
/* NVS is required by the WiFi driver */
|
||||||
|
esp_err_t err = nvs_flash_init();
|
||||||
int elapsed = 0;
|
if(err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||||
while(WiFi.status() != WL_CONNECTED && elapsed < WIFI_TIMEOUT_MS) {
|
nvs_flash_erase();
|
||||||
vTaskDelay(WIFI_POLL_MS / portTICK_PERIOD_MS);
|
nvs_flash_init();
|
||||||
elapsed += WIFI_POLL_MS;
|
|
||||||
ESP_LOGI(TAG, "Waiting for WiFi... (%d ms)", elapsed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(WiFi.status() == WL_CONNECTED) {
|
err = esp_netif_init();
|
||||||
ESP_LOGI(TAG, "Connected. IP: %s", WiFi.localIP().toString().c_str());
|
if(err != ESP_OK && err != ESP_ERR_INVALID_STATE) {
|
||||||
} else {
|
ESP_LOGE(TAG, "esp_netif_init: %s", esp_err_to_name(err));
|
||||||
ESP_LOGW(TAG, "WiFi connection timed out — running without network");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
#ifndef _NETWORKING_HPP
|
#pragma once
|
||||||
#define _NETWORKING_HPP
|
|
||||||
|
|
||||||
class Networking {
|
class Networking {
|
||||||
public:
|
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();
|
void connectWifi();
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
bool isConnected() const;
|
||||||
|
};
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
#include <board.h>
|
#include <board.h>
|
||||||
#include <periph_sdcard.h>
|
#include <periph_sdcard.h>
|
||||||
#include <esp_system.h>
|
#include <esp_system.h>
|
||||||
#include <esp_wifi.h>
|
#include <esp_netif.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
#include "Arduino.h"
|
|
||||||
|
|
||||||
#include "Networking.hpp"
|
#include "Networking.hpp"
|
||||||
#include "WebInterface.hpp"
|
#include "WebInterface.hpp"
|
||||||
#include "RFIDInterface.hpp"
|
#include "RFIDInterface.hpp"
|
||||||
@@ -25,8 +23,6 @@ std::shared_ptr<CacheManager> cacheManager;
|
|||||||
std::shared_ptr<TagPlayerControl> tagPlayerControl;
|
std::shared_ptr<TagPlayerControl> tagPlayerControl;
|
||||||
|
|
||||||
void app_main(void) {
|
void app_main(void) {
|
||||||
initArduino();
|
|
||||||
|
|
||||||
/* SD card must be mounted first — other components depend on it */
|
/* SD card must be mounted first — other components depend on it */
|
||||||
webInterface.initSD();
|
webInterface.initSD();
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user