mirror of
https://github.com/sweidac/kidsbox.git
synced 2026-10-28 06:52:57 +01:00
- 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
90 lines
3.1 KiB
C++
90 lines
3.1 KiB
C++
#include "Networking.hpp"
|
|
|
|
#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 EventGroupHandle_t s_wifi_event_group;
|
|
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() {
|
|
/* 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;
|
|
}
|