WiFiRTC never enters "NTP unreachable" loop

I've been using the WiFiRTC tutorial from https://www.arduino.cc/en/Tutorial/WiFiRTC and found out that on a network where the NTP server can NOT be reached, the code doesn't enter the "NTP unreachable" loop and keeps trying <WiFi.getTime()>

After some research I found the comparison in the following code isn't evaluated correctly:

while ((epoch == 0) || (numberOfTries > maxTries));

To evaluate the comparison correctly, you have to change the code to:

while (((epoch > 0) || (numberOfTries > maxTries)) == false);

The maximum number of tries is then 7, one more than maxTries (=6).

Who can change the code posted on the Arduino website?

The issue of not being able to reach NTP server is not clear to me, but I assume this is dependent on the settings of the local Wifi network. On one network NTP server is reachable with my MKR1000, on another it's not.

I also found out that presumably when port 123 is closed on your local wifi network, the WiFi.getTime() can't reach the NTP server. I still have to test this, by opening port 123.

Edit:
Here's some background info, with link to testing your connection online:
TroubleshootingNTP < Support < Network Time Foundation's NTP Support Wiki.

I still can't reach the NTP server, probably because from this location I'm on a high level security connection with the ISP, which blocks outbound traffic on port 123 (UDP). But I know my MKR1000 works with WiFiRTC on another location, with a different internet connection.

Blocked or restricted ports seem to affect a few users in corporate or even educational settings.

Antivirus/Antimalware and third party firewalls are another source of frustration for some users too trying to talk to Arduinos or vice-versa.

hello I just incurred in the same problem, and I think it's because the while condition in the Arduino Tutorial page is not correctly coded for its purpose.

The tutorial on the Arduino page should be updated accordingly (it is not yet after more than 1 year apparently).

in fact because of the OR condition in the tutorial...

while ((epoch == 0) || (numberOfTries > maxTries));

...the WHILE loop will cycle indefinitely until either 1 of the 2 conditions is true.
So no surprise if the number of tries will be passed, because if epoch is still 0 because the NTP is not received... the loop will continue! :slight_smile:

So, in my opinion the right way to set this condition (and to amend the sketch) is as follow:

while ((epoch == 0) && (numberOfTries < maxTries));

This way, if the program don't get epoch value, the loop will actually stop once reached the max allowed number of trials.

And, for consistency, I personally also gave an actual value before the cycle, so eventually the whole loop looks like this:

  unsigned long epoch = 0;
  int numberOfTries = 0;
  int maxTries = 3;
    do {
    Serial.print("Attempt getting Server Time: "); Serial.print(numberOfTries+1);
    Serial.print("/"); Serial.println(maxTries);
    epoch = WiFi.getTime();
    numberOfTries++;
    delay(1000); //delay is for reading purpose on serial monitor
  } while ((epoch == 0) && (numberOfTries < maxTries));

Now it works nice. I initially thought of a library incompatibility problem in my sketch.

I've seen around similar buggy-ish code for Azure stuff and so on... but I think they never experienced a server not responding to the NTP request, and so they never experienced the problem.