I’m really confused with Time.now(). It keeps on giving me a time in 1999. I’m working on a Particle Electron with a third-party SIM. Any ideas?
Has it connected to the cloud already, since that’s where it gets the time from?
I think so.
Particle.connect();
if (Particle.connected) {
digitalWrite(D7, HIGH);
delay(2000);
Serial.printlnf("Connected to Particle Cloud.");
Serial.printlnf("Start synchronization.");
Particle.syncTime();
waitUntil(Particle.syncTimeDone);
if (Time.isValid()) {
Serial.printlnf("Time is valid.");
}
else {
Serial.printlnf("Time is still invalid.");
}
// Print current time
Serial.println(Time.timeStr());
}
Particle.connected()
is a class function/method not a class field.
Hence you are missing the parenthesis in if (Particle.connected)
. This way you just “check” whether the function exists, but don’t acutally call it.
You need to write it as if (Particle.connected())
But I’d rather go with this
Particle.connect();
if (waitFor(Particle.connected, 60000)) {
digitalWrite(D7, HIGH);
delay(2000);
Serial.printlnf("Connected to Particle Cloud.");
Serial.printlnf("Start synchronization.");
Particle.syncTime();
waitUntil(Particle.syncTimeDone);
if (Time.isValid()) {
Serial.printlnf("Time is valid.");
}
else {
Serial.printlnf("Time is still invalid.");
}
// Print current time
Serial.println(Time.timeStr());
}
else {
Serial.println("Not yet connected");
delay(1000);
}
We can’t see the context of your code snippet, but if you’d call Paticle.connect()
over and over while a connection attempt is still ongoing, you may be interfering with the actual connecting.
Meanwhile I understand why I was having problems with this. I’m using a third-party SIM provided by our IT department. Their firewall is configured in such a way that I can only connect to AWS at the moment and not to Particle Cloud.
As I am still developing my sensor, I am disconnecting the battery at the end of the day. Did I understand it correctly that the latter will undo Particle.syncTime()
?
To be exact, depowering the device completely causes the RTC on the device to have amnesia too.
However, you can keep the RTC powered while the controller core is not by removing the 0Ohm resistor between 3v3 and VBAT and connect a coin cell between VBAT and GND.