adafruit / Adafruit_CircuitPython_NTP

Network Time Protocol (NTP) Helper for CircuitPython
MIT License
9 stars 18 forks source link

NTP fails after 4 seconds #28

Open andrewluebke opened 1 year ago

andrewluebke commented 1 year ago

I'm tinkering with a Raspberry Pi Pico W, to make a clock. I compiled the latest version of Adafruit CircuitPython. This may be an issue over in that repository. If I use the naive code to get NTP time it will crash with an OSError after 4 ntp.datetime. If I try/except it will continue. But anyway I do it will fail after about 4 seconds. I noticed running pings that the 4th ping will also fail but more gracefully. Here's my code:

import wifi
import ipaddress
import socketpool
import adafruit_ntp
import time

try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

print("Connecting to %s" % secrets['ssid'])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s" % secrets['ssid'])
print(f" IP Address: {wifi.radio.ipv4_address}")
print(f"Subnet Mask: {wifi.radio.ipv4_subnet}")
print(f"    Gateway: {wifi.radio.ipv4_gateway}\n")
pool = socketpool.SocketPool(wifi.radio)
ntp = adafruit_ntp.NTP(pool, server="192.168.1.17", tz_offset=0)
count = 0
while True:
    try:
        count += 1
        print(f"Count: {count}, {ntp.datetime}")
    except OSError:
        print(f"Count: {count}, Lost ntp")
        pass
    time.sleep(1)

My output always looks something like this:

Connecting to Luebke-2.4Ghz
Connected to Luebke-2.4Ghz
 IP Address: 192.168.0.3
Subnet Mask: 255.255.254.0
    Gateway: 192.168.0.128

Count: 1, struct_time(tm_year=2022, tm_mon=10, tm_mday=13, tm_hour=21, tm_min=51, tm_sec=53, tm_wday=3, tm_yday=286, tm_isdst=-1)
Count: 2, struct_time(tm_year=2022, tm_mon=10, tm_mday=13, tm_hour=21, tm_min=51, tm_sec=54, tm_wday=3, tm_yday=286, tm_isdst=-1)
Count: 3, struct_time(tm_year=2022, tm_mon=10, tm_mday=13, tm_hour=21, tm_min=51, tm_sec=55, tm_wday=3, tm_yday=286, tm_isdst=-1)
Count: 4, Lost ntp
Count: 5, struct_time(tm_year=2022, tm_mon=10, tm_mday=13, tm_hour=21, tm_min=52, tm_sec=7, tm_wday=3, tm_yday=286, tm_isdst=-1)
Count: 6, struct_time(tm_year=2022, tm_mon=10, tm_mday=13, tm_hour=21, tm_min=52, tm_sec=8, tm_wday=3, tm_yday=286, tm_isdst=-1)

It seems to get ntp info forever after that.

anecdata commented 1 year ago

Using my local router as NTP server... same behavior ([Errno 116] ETIMEDOUT), but If I change it to 2 seconds, it works.

andrewluebke commented 1 year ago

I've tried using several servers and different delays all with the same result, a missing ntp packet around 4 seconds.

If I run this code to ping google and my gateway:

import wifi
import ipaddress
import time

try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

print("Connecting to %s" % secrets['ssid'])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s" % secrets['ssid'])
print(f" IP Address: {wifi.radio.ipv4_address}")
print(f"Subnet Mask: {wifi.radio.ipv4_subnet}")
print(f"    Gateway: {wifi.radio.ipv4_gateway}\n")
count = 0
while True:
    print(f"Ping: {wifi.radio.ping(ipaddress.ip_address('8.8.8.8'))}, {wifi.radio.ping(ipaddress.ip_address('192.168.0.128'))}")
    time.sleep(1)

I always seem to lose the 7th packet:

Connecting to Luebke-2.4Ghz
Connected to Luebke-2.4Ghz
 IP Address: 192.168.0.3
Subnet Mask: 255.255.254.0
    Gateway: 192.168.0.128

Ping: 0.133, 0.039
Ping: 0.129, 0.139
Ping: 0.09, 0.139
Ping: None, 0.015
Ping: 0.048, 0.051
Ping: 0.047, 0.05
Ping: 0.048, 0.051
anecdata commented 1 year ago

Occasional [Errno 116] ETIMEDOUT is expected with UDP (and ICMP), but the pattern is... interesting. NTP timeout does not occur on Adafruit CircuitPython 7.3.3 on 2022-08-29; Adafruit Feather ESP32-S2 TFT with ESP32S2 or on Adafruit CircuitPython 8.0.0-beta.1-21-gfc549fe34 on 2022-10-07; Adafruit Feather ESP32-S3 TFT with ESP32S3.

Default socket timeout is set to 10 seconds (!!)

I lose the 5th & 6th pings:

Connecting... Connected.
Ping: 0.31, 0.044
Ping: 0.306, 0.139
Ping: None, None
Ping: 0.261, 0.023

But again, fine with a 2-second delay.

BTW, I'm running Adafruit CircuitPython 8.0.0-beta.1-20-g66f84f5f9 on 2022-10-07; Raspberry Pi Pico W with rp2040

Addendum:

Updated to Adafruit CircuitPython 8.0.0-beta.1-39-g1569c7ed3 on 2022-10-13; Raspberry Pi Pico W with rp2040 Same ping and ntp behavior.

But... for ntp, if I switch from local server to pool.ntp.org, it's fine even with a 1-second delay.

andrewluebke commented 1 year ago

I agree that there should be some loss, but as you say it's interesting that it always seems to occur for me around 4 seconds. I can't test 7.3.3 on the Pico W since I think it was added in 8.0.0. I'm running the latest 8.0.0-beta.1-39-g1569c7ed3. Just tried pool.ntp.org and I get the same lost ntp after 4 seconds. 2 second delay still loses the ntp at the third try.