Fix factory reset: count through settle window so 10x is reachable

This commit is contained in:
2026-06-29 00:24:01 +02:00
parent 3d2ba11ab0
commit 4d538437bc
+17 -20
View File
@@ -424,27 +424,24 @@ static void factoryReset(bool clearWifi) {
ESP.restart(); ESP.restart();
} }
static void checkFactoryReset() { static int incrementResetCount() {
const int THRESHOLD_CONFIG = 5;
const int THRESHOLD_WIFI = 10;
int count = 0; int count = 0;
File f = LittleFS.open("/reset_count", "r"); File f = LittleFS.open("/reset_count", "r");
if (f) { if (f) { count = f.parseInt(); f.close(); }
count = f.parseInt();
f.close();
}
count++; count++;
if (count >= THRESHOLD_CONFIG) { f = LittleFS.open("/reset_count", "w");
if (f) { f.print(count); f.close(); }
Serial.printf("[BOOT] reset count %d\n", count);
return count;
}
static void applyFactoryResetIfNeeded(int count) {
const int THRESHOLD_CONFIG = 5;
const int THRESHOLD_WIFI = 10;
LittleFS.remove("/reset_count"); LittleFS.remove("/reset_count");
if (count >= THRESHOLD_CONFIG) {
Serial.printf("[BOOT] reset count %d, triggering factory reset\n", count); Serial.printf("[BOOT] reset count %d, triggering factory reset\n", count);
factoryReset(count >= THRESHOLD_WIFI); factoryReset(count >= THRESHOLD_WIFI);
return;
}
f = LittleFS.open("/reset_count", "w");
if (f) {
f.print(count);
f.close();
Serial.printf("[BOOT] reset count %d/%d\n", count, THRESHOLD_CONFIG);
} }
} }
@@ -460,13 +457,13 @@ void setup() {
loadConfig(); loadConfig();
hostnameParam.setValue(hostname, strlen(hostname)); hostnameParam.setValue(hostname, strlen(hostname));
checkFactoryReset(); int resetCount = incrementResetCount();
// Reset-counter settle window: if the device stays powered past this point it // Settle window: if the device stays powered past this point it wasn't a
// wasn't a rapid power-cycle, so clear the counter before the (possibly // rapid power-cycle. After the delay, act on the accumulated count (5x =
// indefinitely blocking) WiFi portal. // config reset, 10x = config + WiFi reset) then clear the counter.
delay(3000); delay(3000);
LittleFS.remove("/reset_count"); applyFactoryResetIfNeeded(resetCount);
Serial.println("[WIFI] starting WiFiManager"); Serial.println("[WIFI] starting WiFiManager");
WiFiManager wm; WiFiManager wm;