Skip to content

Commit 39760d7

Browse files
committed
Implement factory reset - resolves #10
Disable config ports in autoConnect - resolves #11 Requires: tzapu/WiFiManager#575
1 parent f75807e commit 39760d7

File tree

2 files changed

+66
-23
lines changed

2 files changed

+66
-23
lines changed

SynchroClock/SynchroClock.cpp

+63-22
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,7 @@ void handleSave()
538538

539539
void handleErase()
540540
{
541-
for (unsigned int i = 0; i < sizeof(EEConfig); ++i)
542-
{
543-
EEPROM.write(i, 0);
544-
}
545-
EEPROM.commit();
541+
eraseConfig();
546542
HTTP.send(200, "text/plain", "Erased!\n");
547543
}
548544

@@ -739,35 +735,35 @@ void initWiFi()
739735
// setup wifi, blink let slow while connecting and fast if portal activated.
740736
feedback.blink(FEEDBACK_LED_SLOW);
741737

742-
WiFiManager wifi;
743-
wifi.setDebugOutput(false);
744-
wifi.setConnectTimeout(CONNECTION_TIMEOUT);
745-
746-
wifi.setSaveConfigCallback([]()
738+
WiFiManager wm;
739+
wm.setEnableConfigPortal(false); // don't automatically use the captive portal
740+
wm.setDebugOutput(false);
741+
wm.setConnectTimeout(CONNECTION_TIMEOUT);
742+
wm.setSaveConfigCallback([]()
747743
{
748744
save_config = true;
749745
});
750746

751-
wifi.setAPCallback([](WiFiManager *)
747+
wm.setAPCallback([](WiFiManager *)
752748
{
749+
dlog.info(FPSTR(TAG), F("config portal up!"));
753750
feedback.blink(FEEDBACK_LED_FAST);
754751
});
755752

756-
757753
std::vector<ConfigParamPtr> params;
758-
createWiFiParams(wifi, params);
754+
createWiFiParams(wm, params);
759755

760756
dlog.info(FPSTR(TAG), "params has %d items", params.size());
761757

762758
String ssid = "SynchroClock" + String(ESP.getChipId());
763759

764760
if (force_config)
765761
{
766-
wifi.startConfigPortal(ssid.c_str(), NULL);
762+
wm.startConfigPortal(ssid.c_str(), NULL);
767763
}
768764
else
769765
{
770-
wifi.autoConnect(ssid.c_str(), NULL);
766+
wm.autoConnect(ssid.c_str(), NULL);
771767
}
772768

773769
feedback.off();
@@ -777,9 +773,7 @@ void initWiFi()
777773
if (!WiFi.isConnected())
778774
{
779775
dlog.error(FPSTR(TAG), F("failed to connect to wifi!"));
780-
dlog.info(FPSTR(TAG), F("Deep Sleep Time: %d"), MAX_SLEEP_DURATION);
781-
dlog.end();
782-
ESP.deepSleep(MAX_SLEEP_DURATION, RF_DEFAULT);
776+
sleepFor(MAX_SLEEP_DURATION);
783777
}
784778

785779
IPAddress ip = WiFi.localIP();
@@ -984,6 +978,31 @@ void setup()
984978
// If we force config because of the config button then we stop the clock.
985979
//
986980
clk.setEnable(false);
981+
982+
time_t start = millis();
983+
while (digitalRead(CONFIG_PIN) == 0)
984+
{
985+
time_t now = millis();
986+
if ((now-start) > FACTORY_RESET_DELAY)
987+
{
988+
dlog.info(FPSTR(TAG), F("factory reset activated!"));
989+
feedback.off();
990+
991+
dlog.debug(FPSTR(TAG), F("invalidating config..."));
992+
EEPROM.begin(sizeof(EEConfig));
993+
eraseConfig();
994+
995+
dlog.debug(FPSTR(TAG), F("erase WiFi config..."));
996+
ESP.eraseConfig();
997+
998+
dlog.debug(FPSTR(TAG), F("rebooting!"));
999+
dlog.end();
1000+
delay(100);
1001+
ESP.restart();
1002+
delay(1000);
1003+
}
1004+
delay(100);
1005+
}
9871006
}
9881007

9891008

@@ -1012,9 +1031,6 @@ void setup()
10121031

10131032
dlog.debug(FPSTR(TAG), F("EEConfig size: %u"), sizeof(EEConfig));
10141033

1015-
EEPROM.begin(sizeof(EEConfig));
1016-
delay(100);
1017-
10181034
//
10191035
// if the saved config was not good and the clock is not running
10201036
// then force config. If the clock is running then we trust that
@@ -1413,11 +1429,20 @@ uint32_t calculateCRC32(const uint8_t *data, size_t length)
14131429
return crc;
14141430
}
14151431

1432+
void initConfig()
1433+
{
1434+
if (EEPROM.length() != sizeof(EEConfig))
1435+
{
1436+
dlog.info(F("initConfig"), F("initializing EEPROM"));
1437+
EEPROM.begin(sizeof(EEConfig));
1438+
}
1439+
}
14161440

14171441
boolean loadConfig()
14181442
{
14191443
static PROGMEM const char TAG[] = "loadConfig";
14201444
EEConfig cfg;
1445+
initConfig();
14211446
// Read struct from EEPROM
14221447
dlog.debug(FPSTR(TAG), F("loading from EEPROM"));
14231448
unsigned int i;
@@ -1445,6 +1470,7 @@ void saveConfig()
14451470
{
14461471
static PROGMEM const char TAG[] = "saveConfig";
14471472
EEConfig cfg;
1473+
initConfig();
14481474
memcpy(&cfg.data, &config, sizeof(cfg.data));
14491475
cfg.crc = calculateCRC32(((uint8_t*) &cfg.data), sizeof(cfg.data));
14501476
dlog.debug(FPSTR(TAG), F("caculated CRC: %08x"), cfg.crc);
@@ -1456,7 +1482,22 @@ void saveConfig()
14561482
{
14571483
EEPROM.write(i, p[i]);
14581484
}
1459-
EEPROM.commit();
1485+
bool result = EEPROM.commit();
1486+
dlog.info(FPSTR(TAG), F("result: %s"), result ? "success" : "FAILURE");
1487+
}
1488+
1489+
void eraseConfig()
1490+
{
1491+
static PROGMEM const char TAG[] = "eraseConfig";
1492+
initConfig();
1493+
dlog.info(FPSTR(TAG), F("erasing...."));
1494+
for (unsigned int i = 0; i < sizeof(EEConfig); ++i)
1495+
{
1496+
EEPROM.write(i, 0xff);
1497+
}
1498+
dlog.info(FPSTR(TAG), F("committing...."));
1499+
bool result = EEPROM.commit();
1500+
dlog.info(FPSTR(TAG), F("result: %s"), result ? "success" : "FAILURE");
14601501
}
14611502

14621503
boolean readDeepSleepData()

SynchroClock/SynchroClock.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@
6969

7070
#define CLOCK_STRETCH_LIMIT 1500 // i2c clock stretch timeout in microseconds
7171
#define MAX_SLEEP_DURATION 3600 // we do multiple sleep of this to handle bigger sleeps
72-
#define CONNECTION_TIMEOUT 300 // wifi portal timeout - we will deep sleep and try again later
72+
#define CONNECTION_TIMEOUT 30 // wifi connection timeout - we will deep sleep and try again later
73+
#define FACTORY_RESET_DELAY 10000 // how long to hold factory reset after LED is ON - 10 seconds (10,000 milliseconds)
7374

7475
#define offset2longDouble(x) ((long double)x / 4294967296L)
7576

@@ -221,6 +222,7 @@ int setRTCfromNTP(const char* server, bool sync, double* result_offset, IPAddres
221222
int setCLKfromRTC();
222223
void saveConfig();
223224
boolean loadConfig();
225+
void eraseConfig();
224226
boolean readDeepSleepData();
225227
boolean writeDeepSleepData();
226228

0 commit comments

Comments
 (0)