Using Boron 2G/3G, Device OS version 2.1.0, LiPO battery and 5V power connected.
I am having difficulty getting the RTC to store the current time in retained memory. I can successfully store my own variables in retained memory, and have them available after a software reset. According to the documentation (under Time.isValid()), RTC time should be stored under the same conditions as retained memory, but I am finding otherwise.
This is the code that I am using to test behaviour;
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);
SystemSleepConfiguration longSleep;
// Send Logging output over USB serial
SerialLogHandler logHandler;
// Persisted Variables
retained std::chrono::milliseconds longSleepDuration = 14min;
retained float timeZoneOffset = 0;
retained time_t UNIXTimestamp;
// Global variables
const std::chrono::milliseconds connectTimeout = 6min;
time_t lastSyncTimestamp;
void setup() {
System.enableFeature(FEATURE_RETAINED_MEMORY);
// Set time zone, and Local Time format
Time.zone(timeZoneOffset);
Time.setFormat(TIME_FORMAT_ISO8601_FULL);
// Set graceful cloud disconnect, 5 seconds timeout
Particle.setDisconnectOptions(CloudDisconnectOptions().graceful(true).timeout(5s));
// Long Sleep parameters
longSleep.mode(SystemSleepMode::ULTRA_LOW_POWER);
longSleep.duration(2min);
// Make sure that the modem is off
Particle.disconnect();
Cellular.off();
// Delay to allow for a serial connection
delay(10s);
// Display Local time
if (Time.isValid()) {
Log.info("Local Time : %s", Time.format().c_str());
} else {
Log.error("Local Time not Set");
// Set from stored value if valid
if (UNIXTimestamp > 0) {
Time.setTime(UNIXTimestamp);
Log.info("Time set to stored value : %s", Time.format().c_str());
}
}
// Display stored timezone offset
Log.info("Stored Timezone offset : %.2f", timeZoneOffset);
Cellular.on();
Particle.connect();
}
void loop() {
// Wait for a connection to be established
if (millis() > connectTimeout.count()) {
Log.error("No cloud connection, resetting");
delay(5s);
System.reset(RESET_NO_WAIT);
}
if (Particle.connected()) {
delay(5s);
Particle.timeSyncedLast(lastSyncTimestamp);
if (lastSyncTimestamp > 0) {
Log.info("Time received from Particle Device Cloud was: %s", Time.timeStr(lastSyncTimestamp).c_str());
} else {
Particle.syncTime();
waitUntil(Particle.syncTimeDone);
}
// Display Local time
if (Time.isValid()) {
Log.info("Local Time : %s", Time.format().c_str());
} else {
Log.error("Local Time not Set");
}
delay(1s);
Log.info("Sleeping");
Particle.disconnect();
Cellular.off();
System.sleep(longSleep);
// Store timestamp before reset
UNIXTimestamp = Time.now();
System.reset(RESET_NO_WAIT);
}
}
On the first run, this is the relevant parts of the logging output;
0000010280 [app] ERROR: Local Time not Set
0000010280 [app] INFO: Stored Timezone offset : 12.00
0000041030 [app] INFO: Time received from Particle Device Cloud was: Mon Aug 9 21:30:59 2021
0000041031 [app] INFO: Local Time : 2021-08-09T21:31:04+12:00
0000042032 [app] INFO: Sleeping
and this is what I get on second run;
0000010280 [app] ERROR: Local Time not Set
0000010281 [app] INFO: Time set to stored value : 2021-08-09T21:33:09+12:00
0000010281 [app] INFO: Stored Timezone offset : 12.00
0000054015 [app] INFO: Time received from Particle Device Cloud was: Mon Aug 9 21:33:59 2021
0000054016 [app] INFO: Local Time : 2021-08-09T21:34:04+12:00
0000055017 [app] INFO: Sleeping
So I can ‘manually’ store and set the time again, as a work-around, but either I have made an error, or the system is not behaving as described. Any ideas?? Thanks.