@@ -538,11 +538,7 @@ void handleSave()
538
538
539
539
void handleErase ()
540
540
{
541
- for (unsigned int i = 0 ; i < sizeof (EEConfig); ++i)
542
- {
543
- EEPROM.write (i, 0 );
544
- }
545
- EEPROM.commit ();
541
+ eraseConfig ();
546
542
HTTP.send (200 , " text/plain" , " Erased!\n " );
547
543
}
548
544
@@ -739,35 +735,35 @@ void initWiFi()
739
735
// setup wifi, blink let slow while connecting and fast if portal activated.
740
736
feedback.blink (FEEDBACK_LED_SLOW);
741
737
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 ([]()
747
743
{
748
744
save_config = true ;
749
745
});
750
746
751
- wifi .setAPCallback ([](WiFiManager *)
747
+ wm .setAPCallback ([](WiFiManager *)
752
748
{
749
+ dlog.info (FPSTR (TAG), F (" config portal up!" ));
753
750
feedback.blink (FEEDBACK_LED_FAST);
754
751
});
755
752
756
-
757
753
std::vector<ConfigParamPtr> params;
758
- createWiFiParams (wifi , params);
754
+ createWiFiParams (wm , params);
759
755
760
756
dlog.info (FPSTR (TAG), " params has %d items" , params.size ());
761
757
762
758
String ssid = " SynchroClock" + String (ESP.getChipId ());
763
759
764
760
if (force_config)
765
761
{
766
- wifi .startConfigPortal (ssid.c_str (), NULL );
762
+ wm .startConfigPortal (ssid.c_str (), NULL );
767
763
}
768
764
else
769
765
{
770
- wifi .autoConnect (ssid.c_str (), NULL );
766
+ wm .autoConnect (ssid.c_str (), NULL );
771
767
}
772
768
773
769
feedback.off ();
@@ -777,9 +773,7 @@ void initWiFi()
777
773
if (!WiFi.isConnected ())
778
774
{
779
775
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);
783
777
}
784
778
785
779
IPAddress ip = WiFi.localIP ();
@@ -984,6 +978,31 @@ void setup()
984
978
// If we force config because of the config button then we stop the clock.
985
979
//
986
980
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
+ }
987
1006
}
988
1007
989
1008
@@ -1012,9 +1031,6 @@ void setup()
1012
1031
1013
1032
dlog.debug (FPSTR (TAG), F (" EEConfig size: %u" ), sizeof (EEConfig));
1014
1033
1015
- EEPROM.begin (sizeof (EEConfig));
1016
- delay (100 );
1017
-
1018
1034
//
1019
1035
// if the saved config was not good and the clock is not running
1020
1036
// 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)
1413
1429
return crc;
1414
1430
}
1415
1431
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
+ }
1416
1440
1417
1441
boolean loadConfig ()
1418
1442
{
1419
1443
static PROGMEM const char TAG[] = " loadConfig" ;
1420
1444
EEConfig cfg;
1445
+ initConfig ();
1421
1446
// Read struct from EEPROM
1422
1447
dlog.debug (FPSTR (TAG), F (" loading from EEPROM" ));
1423
1448
unsigned int i;
@@ -1445,6 +1470,7 @@ void saveConfig()
1445
1470
{
1446
1471
static PROGMEM const char TAG[] = " saveConfig" ;
1447
1472
EEConfig cfg;
1473
+ initConfig ();
1448
1474
memcpy (&cfg.data , &config, sizeof (cfg.data ));
1449
1475
cfg.crc = calculateCRC32 (((uint8_t *) &cfg.data ), sizeof (cfg.data ));
1450
1476
dlog.debug (FPSTR (TAG), F (" caculated CRC: %08x" ), cfg.crc );
@@ -1456,7 +1482,22 @@ void saveConfig()
1456
1482
{
1457
1483
EEPROM.write (i, p[i]);
1458
1484
}
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" );
1460
1501
}
1461
1502
1462
1503
boolean readDeepSleepData ()
0 commit comments