diff --git a/esp32/include/wifi/wifi_idf.h b/esp32/include/wifi/wifi_idf.h index 9f7c2f4..1008d65 100644 --- a/esp32/include/wifi/wifi_idf.h +++ b/esp32/include/wifi/wifi_idf.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include diff --git a/esp32/include/www_mount.hpp b/esp32/include/www_mount.hpp index 4c99988..7b66f1f 100644 --- a/esp32/include/www_mount.hpp +++ b/esp32/include/www_mount.hpp @@ -3,3 +3,4 @@ #include "WWWData.h" void mountStaticAssets(WebServer& s); +void mountSpaFallback(WebServer& s); diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 81ee093..591d95c 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -42,7 +42,7 @@ WiFiService wifiService; APService apService; void setupServer() { - server.config(50 + WWW_ASSETS_COUNT, 32768); + server.config(50 + WWW_ASSETS_COUNT, 16384); server.listen(80); server.on("/api/system/reset", HTTP_POST, @@ -107,8 +107,10 @@ void setupServer() { STAITC_PROTO_POST_ENDPOINT(server, "/api/files/delete", file_delete_request, FileSystem::handleDelete); STAITC_PROTO_POST_ENDPOINT(server, "/api/files/edit", file_edit_request, FileSystem::handleEdit); STAITC_PROTO_POST_ENDPOINT(server, "/api/files/mkdir", file_mkdir_request, FileSystem::mkdir); + wsSocket.begin(); #if EMBED_WEBAPP mountStaticAssets(server); + mountSpaFallback(server); #endif server.on("/*", HTTP_OPTIONS, [](httpd_req_t *request) { httpd_resp_set_status(request, "200 OK"); @@ -286,8 +288,6 @@ void IRAM_ATTR serviceLoopEntry(void *) { #endif setupServer(); - - wsSocket.begin(); setupEventSocket(); ESP_LOGI("main", "Service task started"); diff --git a/esp32/src/wifi/wifi_idf.cpp b/esp32/src/wifi/wifi_idf.cpp index 1b8b01a..74d2974 100644 --- a/esp32/src/wifi/wifi_idf.cpp +++ b/esp32/src/wifi/wifi_idf.cpp @@ -184,6 +184,7 @@ bool WiFiClass::begin(const char* ssid, const char* password, int32_t channel, c wifi_mode_t currentMode = getMode(); if (currentMode == WIFI_MODE_NULL || currentMode == WIFI_MODE_AP) { mode(currentMode == WIFI_MODE_AP ? WIFI_MODE_APSTA : WIFI_MODE_STA); + vTaskDelay(500 / portTICK_PERIOD_MS); } wifi_config_t wifi_config = {}; @@ -370,6 +371,7 @@ int16_t WiFiClass::scanNetworks(bool async) { wifi_mode_t currentMode = getMode(); if (currentMode == WIFI_MODE_NULL || currentMode == WIFI_MODE_AP) { mode(currentMode == WIFI_MODE_AP ? WIFI_MODE_APSTA : WIFI_MODE_STA); + vTaskDelay(500 / portTICK_PERIOD_MS); } _scanStatus = -1; diff --git a/esp32/src/wifi_service.cpp b/esp32/src/wifi_service.cpp index 8819af2..751a0f8 100644 --- a/esp32/src/wifi_service.cpp +++ b/esp32/src/wifi_service.cpp @@ -27,12 +27,6 @@ void WiFiService::begin() { _persistence.readFromFS(); _lastConnectionAttempt = 0; - - if (state().wifi_networks_count >= 1) { - WiFi.mode(WIFI_MODE_STA); - vTaskDelay(100 / portTICK_PERIOD_MS); - configureNetwork(state().wifi_networks[0]); - } } void WiFiService::reconfigureWiFiConnection() { @@ -127,65 +121,24 @@ esp_err_t WiFiService::getNetworkStatus(httpd_req_t *request) { void WiFiService::manageSTA() { if (WiFi.isConnected() || state().wifi_networks_count == 0) return; - if ((WiFi.getMode() & WIFI_MODE_STA) == 0) { - WiFi.mode(WIFI_MODE_STA); - vTaskDelay(100 / portTICK_PERIOD_MS); + wifi_mode_t mode = WiFi.getMode(); + if (mode == WIFI_MODE_NULL || mode == WIFI_MODE_AP) return; + + static uint32_t startTime = 0; + static bool attempted = false; + + if (startTime == 0) { + startTime = esp_timer_get_time() / 1000; + return; } - connectToWiFi(); -} -void WiFiService::connectToWiFi() { - int scanResult = WiFi.scanNetworks(); - if (scanResult < 0) { - ESP_LOGE(TAG, "WiFi scan failed."); - } else if (scanResult == 0) { - ESP_LOGI(TAG, "No networks found."); - } else { - ESP_LOGI(TAG, "%d networks found.", scanResult); + uint32_t now = esp_timer_get_time() / 1000; + if (now - startTime < 3000) return; - WiFiNetwork *bestNetwork = nullptr; - int32_t bestNetworkDb = FACTORY_WIFI_RSSI_THRESHOLD; - - for (int i = 0; i < scanResult; ++i) { - std::string ssid_scan; - int32_t rssi_scan; - uint8_t sec_scan; - uint8_t *BSSID_scan; - int32_t chan_scan; - - WiFi.getNetworkInfo(i, ssid_scan, sec_scan, rssi_scan, BSSID_scan, chan_scan); - - for (pb_size_t j = 0; j < state().wifi_networks_count; j++) { - WiFiNetwork &network = state().wifi_networks[j]; - if (ssid_scan == network.ssid) { - if (rssi_scan > bestNetworkDb) { - bestNetworkDb = rssi_scan; - bestNetwork = &network; - } - } - } - } - - if (!state().priority_rssi) { - for (pb_size_t j = 0; j < state().wifi_networks_count; j++) { - WiFiNetwork &network = state().wifi_networks[j]; - for (int i = 0; i < scanResult; ++i) { - if (WiFi.SSID(i) == network.ssid) { - ESP_LOGI(TAG, "Connecting to first available network: %s", network.ssid); - configureNetwork(network); - WiFi.scanDelete(); - return; - } - } - } - } else if (bestNetwork) { - ESP_LOGI(TAG, "Connecting to strongest network: %s", bestNetwork->ssid); - configureNetwork(*bestNetwork); - } else { - ESP_LOGI(TAG, "No known networks found."); - } - - WiFi.scanDelete(); + if (!attempted && state().wifi_networks_count > 0) { + attempted = true; + ESP_LOGI(TAG, "Connecting to: %s", state().wifi_networks[0].ssid); + configureNetwork(state().wifi_networks[0]); } } diff --git a/esp32/src/www_mount.cpp b/esp32/src/www_mount.cpp index 71ef876..3a77273 100644 --- a/esp32/src/www_mount.cpp +++ b/esp32/src/www_mount.cpp @@ -1,4 +1,14 @@ #include "www_mount.hpp" +#include + +static const WebAsset* findAsset(const char* uri) { + for (size_t i = 0; i < WWW_ASSETS_COUNT; i++) { + if (strcmp(WWW_ASSETS[i].uri, uri) == 0) { + return &WWW_ASSETS[i]; + } + } + return nullptr; +} static esp_err_t web_send(httpd_req_t* req, const WebAsset& asset) { httpd_resp_set_status(req, "200 OK"); @@ -23,3 +33,16 @@ void mountStaticAssets(WebServer& server) { server.on(a->uri, HTTP_GET, [a](httpd_req_t* req) { return web_send(req, *a); }); } } + +void mountSpaFallback(WebServer& server) { + const WebAsset* indexAsset = findAsset(WWW_OPT.default_uri); + if (indexAsset) { + server.on("/*", HTTP_GET, [indexAsset](httpd_req_t* req) { + if (strncmp(req->uri, "/api/", 5) == 0) { + httpd_resp_send_err(req, HTTPD_404_NOT_FOUND, "Not found"); + return ESP_FAIL; + } + return web_send(req, *indexAsset); + }); + } +}