feat: two-tier factory reset, WiFi fallback chain, SettingsWebServer

Factory reset (F-015):
- InputManager gains MEDIUM_PRESSED state with mediumPressMs=5000
- 5s hold: wipe WiFi credentials only, restart on release
- 10s hold: full factory reset (WiFi wipe already done at 5s mark)
- New callbacks: setFactoryResetMediumPressCallback,
  setFactoryResetMediumPressReleaseCallback

WiFi fallback chain (F-016):
- Connection now cycles: saved creds → hardcoded failsafe → portal
- MAX_WIFI_FAILURES reduced 3→2 per stage
- Portal now reboots after saving credentials (fixes port 80 conflict
  between WiFiManager and AsyncWebServer)
- AP SSID sourced from netConfig.apSsid instead of hardcoded pattern
- clearWiFiCredentials() added to Networking for 5s reset gesture

SettingsWebServer (F-017):
- Instantiated in main.cpp alongside CommunicationRouter
- begin() called when network becomes ready (both WiFi paths)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-30 09:52:17 +03:00
co-authored by Claude Sonnet 4.6
parent ee73d4d2c4
commit df8426fce6
5 changed files with 183 additions and 124 deletions
+62 -67
View File
@@ -17,6 +17,7 @@ Networking::Networking(ConfigManager& configManager)
, _bootSequenceComplete(false)
, _ethernetCableConnected(false)
, _wifiConnectionFailures(0)
, _triedSavedCredentials(false)
, _wifiManager(nullptr)
, _reconnectionTimer(nullptr) {
@@ -110,60 +111,35 @@ void Networking::startEthernetConnection() {
}
void Networking::startWiFiConnection() {
// If bootOtaCheck (or any pre-init code) already connected WiFi, adopt it
// instead of tearing it down with WiFi.mode(WIFI_STA).
if (WiFi.status() == WL_CONNECTED) {
LOG_INFO(TAG, "WiFi already connected - adopting existing connection");
onWiFiConnected();
return;
}
LOG_INFO(TAG, "Attempting WiFi connection...");
setState(NetworkState::CONNECTING_WIFI);
// ALWAYS try default credentials first (for bundled router deployment)
applyNetworkConfig(false);
WiFi.mode(WIFI_STA);
auto& netConfig = _configManager.getNetworkConfig();
LOG_INFO(TAG, "Using DEFAULT WiFi credentials - SSID: %s", netConfig.defaultWifiSsid.c_str());
applyNetworkConfig(false); // false = WiFi config
WiFi.mode(WIFI_STA);
WiFi.begin(netConfig.defaultWifiSsid.c_str(), netConfig.defaultWifiPsk.c_str());
if (!_triedSavedCredentials && hasValidWiFiCredentials()) {
// Stage 1: try saved credentials (WiFiManager's stored SSID+password)
String savedSSID = _wifiManager->getWiFiSSID(true);
LOG_INFO(TAG, "Using saved WiFi credentials - SSID: %s", savedSSID.c_str());
WiFi.begin(); // WiFiManager's saved SSID+password
} else {
// Stage 2: saved creds exhausted (or none saved) — try hardcoded failsafe
LOG_INFO(TAG, "Trying hardcoded failsafe WiFi - SSID: %s", netConfig.defaultWifiSsid.c_str());
WiFi.begin(netConfig.defaultWifiSsid.c_str(), netConfig.defaultWifiPsk.c_str());
}
_lastConnectionAttempt = millis();
xTimerStart(_reconnectionTimer, 0);
// Original WiFiManager fallback code (DISABLED for fixed deployment):
// // First, try default credentials if this is the first boot attempt
// if (!_bootSequenceComplete && !hasValidWiFiCredentials()) {
// LOG_INFO(TAG, "No saved credentials - trying default WiFi credentials");
// auto& netConfig = _configManager.getNetworkConfig();
//
// applyNetworkConfig(false); // false = WiFi config
// WiFi.mode(WIFI_STA);
// WiFi.begin(netConfig.defaultWifiSsid.c_str(), netConfig.defaultWifiPsk.c_str());
//
// _lastConnectionAttempt = millis();
// xTimerStart(_reconnectionTimer, 0);
// return;
// }
//
// // Check if we have valid saved credentials
// if (!hasValidWiFiCredentials()) {
// LOG_WARNING(TAG, "No valid WiFi credentials found");
// if (!_bootSequenceComplete) {
// // No credentials during boot - start portal
// startWiFiPortal();
// }
// return;
// }
//
// // Get and log saved credentials (for debugging)
// String savedSSID = _wifiManager->getWiFiSSID(true);
// LOG_INFO(TAG, "Using WiFiManager saved credentials - SSID: %s", savedSSID.c_str());
//
// applyNetworkConfig(false); // false = WiFi config
// WiFi.mode(WIFI_STA);
//
// // Let WiFiManager handle credentials (uses saved SSID/password)
// WiFi.begin();
//
// _lastConnectionAttempt = millis();
//
// // Start reconnection timer to handle timeout
// xTimerStart(_reconnectionTimer, 0);
}
void Networking::startWiFiPortal() {
@@ -173,7 +149,7 @@ void Networking::startWiFiPortal() {
WiFi.mode(WIFI_AP_STA);
auto& netConfig = _configManager.getNetworkConfig();
String apName = "Vesper-" + _configManager.getDeviceUID();
String apName = netConfig.apSsid;
LOG_INFO(TAG, "WiFi Portal: SSID='%s', Password='%s'", apName.c_str(), netConfig.apPass.c_str());
@@ -190,8 +166,12 @@ void Networking::startWiFiPortal() {
_wifiManager->setCustomHeadElement(customHTML.c_str());
if (_wifiManager->autoConnect(apName.c_str(), netConfig.apPass.c_str())) {
LOG_INFO(TAG, "WiFi configured successfully via portal");
onWiFiConnected();
LOG_INFO(TAG, "WiFi credentials saved via portal - rebooting to apply");
// Reboot so the web server and all services start cleanly on the new network.
// WiFiManager holds port 80 during the portal; a reboot is the only reliable
// way to release it and let AsyncWebServer bind correctly.
delay(1000);
ESP.restart();
} else {
LOG_ERROR(TAG, "WiFi portal configuration failed");
setState(NetworkState::DISCONNECTED);
@@ -226,29 +206,36 @@ void Networking::handleReconnection() {
_wifiConnectionFailures++;
LOG_WARNING(TAG, "WiFi connection timeout (failure #%d)", _wifiConnectionFailures);
// After 3 failed attempts during boot, start portal
if (_wifiConnectionFailures >= MAX_WIFI_FAILURES) {
LOG_ERROR(TAG, "Multiple WiFi connection failures - credentials may be invalid");
// Stage transition: after MAX_WIFI_FAILURES on saved creds, switch to failsafe
if (!_triedSavedCredentials && _wifiConnectionFailures >= MAX_WIFI_FAILURES) {
LOG_WARNING(TAG, "Saved credentials failed %d times - switching to hardcoded failsafe", MAX_WIFI_FAILURES);
_triedSavedCredentials = true;
_wifiConnectionFailures = 0;
startWiFiConnection(); // will now use hardcoded creds
return;
}
// Stage transition: after MAX_WIFI_FAILURES on failsafe creds, open portal (boot only)
if (_triedSavedCredentials && _wifiConnectionFailures >= MAX_WIFI_FAILURES) {
LOG_ERROR(TAG, "All credential attempts failed");
if (!_bootSequenceComplete) {
// Boot not complete yet - open portal
LOG_INFO(TAG, "Opening WiFi portal for reconfiguration");
_wifiConnectionFailures = 0; // Reset counter
_triedSavedCredentials = false; // reset for next boot
_wifiConnectionFailures = 0;
startWiFiPortal();
} else {
// Boot already complete - just keep retrying
LOG_WARNING(TAG, "WiFi connection lost - continuing retry attempts");
// Reset counter after extended failure to prevent overflow
if (_wifiConnectionFailures > 10) {
_wifiConnectionFailures = 3;
}
_lastConnectionAttempt = now;
startWiFiConnection(); // startWiFiConnection re-arms the timer
// Post-boot: reset and retry from saved creds again
LOG_WARNING(TAG, "WiFi connection lost - restarting credential cycle");
_triedSavedCredentials = false;
_wifiConnectionFailures = 0;
startWiFiConnection();
}
} else {
// Retry WiFi connection — startWiFiConnection re-arms the timer
startWiFiConnection();
return;
}
// Still within a phase — keep retrying
startWiFiConnection();
} else {
// Still within timeout window — re-arm timer to check again later
xTimerStart(_reconnectionTimer, 0);
@@ -387,9 +374,10 @@ void Networking::onWiFiConnected() {
setState(NetworkState::CONNECTED_WIFI);
setActiveConnection(ConnectionType::WIFI);
// Reset failure counter on successful connection
// Reset failure/phase counters on successful connection
_wifiConnectionFailures = 0;
_triedSavedCredentials = false;
// Stop reconnection timer
xTimerStop(_reconnectionTimer, 0);
@@ -550,6 +538,13 @@ void Networking::reconnectionTimerCallback(TimerHandle_t xTimer) {
}
}
void Networking::clearWiFiCredentials() {
if (_wifiManager) {
_wifiManager->resetSettings();
LOG_WARNING(TAG, "Networking: WiFi credentials wiped");
}
}
void Networking::startPermanentAPMode() {
LOG_INFO(TAG, "Starting Permanent AP Mode");
setState(NetworkState::AP_MODE_PERMANENT);
+6 -2
View File
@@ -103,6 +103,9 @@ public:
// AP Mode control
void startPermanentAPMode();
bool isInAPMode() const { return _state == NetworkState::AP_MODE_PERMANENT; }
// Wipe WiFiManager stored credentials (SSID + password)
void clearWiFiCredentials();
// ═══════════════════════════════════════════════════════════════════════════════
// HEALTH CHECK METHOD
@@ -128,7 +131,8 @@ private:
unsigned long _lastConnectionAttempt;
bool _bootSequenceComplete;
bool _ethernetCableConnected;
int _wifiConnectionFailures; // Track consecutive WiFi failures
int _wifiConnectionFailures; // Track consecutive WiFi failures
bool _triedSavedCredentials; // True once saved-creds phase has been exhausted
// Callbacks
std::function<void()> _onNetworkConnected;
@@ -165,5 +169,5 @@ private:
// Constants
static const unsigned long RECONNECTION_INTERVAL = 5000; // 5 seconds
static const int MAX_WIFI_FAILURES = 3; // Portal after 3 failures
static const int MAX_WIFI_FAILURES = 2; // Move to next stage after 2 failures
};