Skip to content

Commit

Permalink
Implement factory reset - resolves #10
Browse files Browse the repository at this point in the history
Disable config ports in autoConnect - resolves #11

Requires: tzapu/WiFiManager#575
  • Loading branch information
liebman committed Mar 25, 2018
1 parent f75807e commit 39760d7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 23 deletions.
85 changes: 63 additions & 22 deletions SynchroClock/SynchroClock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,7 @@ void handleSave()

void handleErase()
{
for (unsigned int i = 0; i < sizeof(EEConfig); ++i)
{
EEPROM.write(i, 0);
}
EEPROM.commit();
eraseConfig();
HTTP.send(200, "text/plain", "Erased!\n");
}

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

WiFiManager wifi;
wifi.setDebugOutput(false);
wifi.setConnectTimeout(CONNECTION_TIMEOUT);

wifi.setSaveConfigCallback([]()
WiFiManager wm;
wm.setEnableConfigPortal(false); // don't automatically use the captive portal
wm.setDebugOutput(false);
wm.setConnectTimeout(CONNECTION_TIMEOUT);
wm.setSaveConfigCallback([]()
{
save_config = true;
});

wifi.setAPCallback([](WiFiManager *)
wm.setAPCallback([](WiFiManager *)
{
dlog.info(FPSTR(TAG), F("config portal up!"));
feedback.blink(FEEDBACK_LED_FAST);
});


std::vector<ConfigParamPtr> params;
createWiFiParams(wifi, params);
createWiFiParams(wm, params);

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

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

if (force_config)
{
wifi.startConfigPortal(ssid.c_str(), NULL);
wm.startConfigPortal(ssid.c_str(), NULL);
}
else
{
wifi.autoConnect(ssid.c_str(), NULL);
wm.autoConnect(ssid.c_str(), NULL);
}

feedback.off();
Expand All @@ -777,9 +773,7 @@ void initWiFi()
if (!WiFi.isConnected())
{
dlog.error(FPSTR(TAG), F("failed to connect to wifi!"));
dlog.info(FPSTR(TAG), F("Deep Sleep Time: %d"), MAX_SLEEP_DURATION);
dlog.end();
ESP.deepSleep(MAX_SLEEP_DURATION, RF_DEFAULT);
sleepFor(MAX_SLEEP_DURATION);
}

IPAddress ip = WiFi.localIP();
Expand Down Expand Up @@ -984,6 +978,31 @@ void setup()
// If we force config because of the config button then we stop the clock.
//
clk.setEnable(false);

time_t start = millis();
while (digitalRead(CONFIG_PIN) == 0)
{
time_t now = millis();
if ((now-start) > FACTORY_RESET_DELAY)
{
dlog.info(FPSTR(TAG), F("factory reset activated!"));
feedback.off();

dlog.debug(FPSTR(TAG), F("invalidating config..."));
EEPROM.begin(sizeof(EEConfig));
eraseConfig();

dlog.debug(FPSTR(TAG), F("erase WiFi config..."));
ESP.eraseConfig();

dlog.debug(FPSTR(TAG), F("rebooting!"));
dlog.end();
delay(100);
ESP.restart();
delay(1000);
}
delay(100);
}
}


Expand Down Expand Up @@ -1012,9 +1031,6 @@ void setup()

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

EEPROM.begin(sizeof(EEConfig));
delay(100);

//
// if the saved config was not good and the clock is not running
// then force config. If the clock is running then we trust that
Expand Down Expand Up @@ -1413,11 +1429,20 @@ uint32_t calculateCRC32(const uint8_t *data, size_t length)
return crc;
}

void initConfig()
{
if (EEPROM.length() != sizeof(EEConfig))
{
dlog.info(F("initConfig"), F("initializing EEPROM"));
EEPROM.begin(sizeof(EEConfig));
}
}

boolean loadConfig()
{
static PROGMEM const char TAG[] = "loadConfig";
EEConfig cfg;
initConfig();
// Read struct from EEPROM
dlog.debug(FPSTR(TAG), F("loading from EEPROM"));
unsigned int i;
Expand Down Expand Up @@ -1445,6 +1470,7 @@ void saveConfig()
{
static PROGMEM const char TAG[] = "saveConfig";
EEConfig cfg;
initConfig();
memcpy(&cfg.data, &config, sizeof(cfg.data));
cfg.crc = calculateCRC32(((uint8_t*) &cfg.data), sizeof(cfg.data));
dlog.debug(FPSTR(TAG), F("caculated CRC: %08x"), cfg.crc);
Expand All @@ -1456,7 +1482,22 @@ void saveConfig()
{
EEPROM.write(i, p[i]);
}
EEPROM.commit();
bool result = EEPROM.commit();
dlog.info(FPSTR(TAG), F("result: %s"), result ? "success" : "FAILURE");
}

void eraseConfig()
{
static PROGMEM const char TAG[] = "eraseConfig";
initConfig();
dlog.info(FPSTR(TAG), F("erasing...."));
for (unsigned int i = 0; i < sizeof(EEConfig); ++i)
{
EEPROM.write(i, 0xff);
}
dlog.info(FPSTR(TAG), F("committing...."));
bool result = EEPROM.commit();
dlog.info(FPSTR(TAG), F("result: %s"), result ? "success" : "FAILURE");
}

boolean readDeepSleepData()
Expand Down
4 changes: 3 additions & 1 deletion SynchroClock/SynchroClock.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@

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

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

Expand Down Expand Up @@ -221,6 +222,7 @@ int setRTCfromNTP(const char* server, bool sync, double* result_offset, IPAddres
int setCLKfromRTC();
void saveConfig();
boolean loadConfig();
void eraseConfig();
boolean readDeepSleepData();
boolean writeDeepSleepData();

Expand Down

0 comments on commit 39760d7

Please sign in to comment.