ezTime icon indicating copy to clipboard operation
ezTime copied to clipboard

waitForSync is not returning true

Open nospam2k opened this issue 5 years ago • 7 comments

Sometimes, after doing a firmware update. waitForSync won't return true. Here is my code.

Timezone US;
...

void setup()
{
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  
  Serial.println("Connecting to Wifi");

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  Serial.println("Connected to " + String(ssid) + " on address: " + WiFi.localIP().toString());

  while (!waitForSync(3))
  {
   Serial.println("Retrying timeSync");
  }
  Serial.println("Got timeSync");
}

void loop() {}

nospam2k avatar Mar 22 '19 05:03 nospam2k

Can you retry this? I set the NTP error-retry back to 20 seconds.

ropg avatar Mar 26 '19 12:03 ropg

Sorry for delay. Because of the bug I mentioned, I've just setup to reset the ESP8266 until it gets the time as I don't really have time to test it. Waiting 20 seconds isn't really an option anyway as when it does connect it usually does so within 3 seconds so I would just leave the time reset code anyway. Feel free to close this if you wish.

nospam2k avatar Apr 17 '19 05:04 nospam2k

Im having the same problem. It has reconnected almost 15 times with 20s retry and Im not getting anything back. I have tried multiple times and without the loop. Luckily sometime it gets the time quickly!! Surprisingly connecting to the ntp server directly using a code I found in the net works every time. This probably needs to be retested?

` delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.println(IPAddress2String(WiFi.localIP()));

Serial.print("Connecting");
while (IPAddress2String(WiFi.localIP()) == "0.0.0.0") {
  delay(1000);
  Serial.print(".");
}
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Synching Time Server...");
int attemptCnt = 1;
while (!waitForSync(20))
{
  attemptCnt++;
  Serial.println("Reconnecting...");
}
Serial.println("UTC: " + UTC.dateTime());
myTZ.setLocation(F("America/New_York"));
Serial.print("My Local Time:");
Serial.println(myTZ.dateTime());`

vbbalaji avatar Jun 15 '19 01:06 vbbalaji

I will have a look soon. Can you tell me what Arduino board this is ran on?

ropg avatar Jun 15 '19 13:06 ropg

ESP8266 - nodeMCU

vbbalaji avatar Jun 15 '19 19:06 vbbalaji

I experience exactly the same like @vbbalaji. Sometimes it just doesnt get the time back. Board: esp32 (heltec wifi kit lora 32)

ernfel avatar Mar 10 '20 09:03 ernfel

I have run into the fact that time is not synced in the waitforsync()... then after a while it "just syncs". I think I have fixed it now by using updateNTP() before waitforsync, but somehow I was not expecting this. I am using a esp8266 on a custom PCB design.

rvdbreemen avatar Aug 11 '20 16:08 rvdbreemen