Electron time sync failure

I have encountered a strange condition when attempting to sync time: one Electron does not sync correctly – I get a November 30, 1968 date from the cloud, but all other functions appear to work correctly. But when I run the same code on a different Electron, I get the correct date and time and everything functions as expected.

The code in question is:

void setup()
{
     Serial.begin(57600);		        //  Begin the Serial interface
    
     Serial.print (F("Hidrosonico online. Version "));
     Serial.println (VERSION);
    
     pinMode (PULSEPIN, INPUT);	        //  Sonar pulse width pin

     if (Particle.connected() == true)
     {
          Serial.println(F("Connected to Particle Cloud."));
     }
     else
     {
          Serial.println(F("Not connected to Particle Cloud."));
     }
    
     unsigned int timeout = millis() + timeoutInterval;
     while (Time.year() <= 1970 && millis() <= timeout)
     {
          Particle.syncTime();
     }
    
     if (Time.year() <= 1970)
     {
          Serial.println(F("syncTime timed out; time is wrong."));
     }
    
     Particle.publish ("startup", VERSION, PRIVATE);
    
     Time.zone (UTCOFFSET);              //  Set the local time zone
    
     Serial.println (Time.timeStr());    //  Print the time to Serial for confirmation
    
     Cellular.off();                     //  Turn the GSM module off to save power
     RGB.control (true);                 //  Take control of the LED
     RGB.color (0, 0, 0);                //  Shut off the LED to save power
    
     Serial.println(F("Setup complete."));
 }

This seemed to start happening at about the same time that the malfunctioning unit went over 1MB usage, though I cannot say whether this is related.

Any insights welcomed.

Not directly addressing your issue, but you should not call Particle.syncTime() inside that loop, rather call Particle.process() instead.
I fact you don’t need to call Particle.syncTime() at all in setup() as it is called by default during startup.

Since you don’t show your full code, I have to assume you are using default AUTOMATIC mode, hence I’d expect that your code will never start running unless your device got cloud connected, or have you ever seen a "Not connected" message?

BTW:
In this environment the F() macro does absolutely nothing, so you can just leave it out :wink:

1 Like

Thanks for that. I called Particle.syncTime() only because I was having trouble with pulling the correct time, and there were some older references to this as a solution to that problem. I am indeed using the default Automatic mode.

And thanks for the tip on Flash strings; I noticed that some of the code I ported over from the Arduino compiled with the F(), so I wondered if it was a thing on the Particle platforms and couldn’t find anything in the documentation.