From b3c5a042f23aa1631b972e0bbdd985e78b972f5f Mon Sep 17 00:00:00 2001 From: Jason Jackson Date: Sun, 27 Aug 2023 10:26:50 -0400 Subject: [PATCH] Added multiple NTP server support --- README.md | 12 ++++++++---- src/network.py | 22 ++++++++++++++-------- src/settings.toml | 5 +++-- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index fccb69f..57610ef 100644 --- a/README.md +++ b/README.md @@ -9,15 +9,19 @@ Go to the [OWM sign up](https://openweathermap.org/appid) and using the free sub Put the token from the [OWM API Keys page](https://home.openweathermap.org/api_keys) into the settings.toml file in the OWM_API_TOKEN="" setting. OWM uses your geolocation which gets looked up via the Geolocation api, for this you need to provide your zipcode and the Country under OWM settings listed below. +### NTP Servers +You can define up to 3 NTP servers, one primary and two fallbacks, to use for time syncronization. The servers are separated by a "pipe" | character. You can find +a list of [NTP Servers](https://timetoolsltd.com/information/public-ntp-server/) to use if you need something closer. + ## Settings Requires a settings.toml file with the following settings in settings file: * WIFI_SSID="your ssid" * WIFI_PASSWORD="yoursupersecretpassword" -* NTP_HOST="0.adafruit.pool.ntp.org" -* TZ_OFFSET= ie TZ_OFFSET=-5 -* NTP_INTERVAL=6 -* UNITS="imperial" # imperial or metric +* NTP_HOST="0.adafruit.pool.ntp.org|0.us.pool.ntp.org" +* TZ_OFFSET=-5 +* NTP_INTERVAL=21600 **ie 21600 = 6hr, 43200 = 12hr, 86400 = 24hr** +* UNITS="imperial" **ie imperial or metric** ### openweathermap.org Data Authorization * OWM_API_TOKEN="Your Token" diff --git a/src/network.py b/src/network.py index c721278..c3912b4 100644 --- a/src/network.py +++ b/src/network.py @@ -18,12 +18,14 @@ def __init__(self) -> None: # NTP specific constants self.TZ = os.getenv('TZ_OFFSET') - self.NTP_HOST = os.getenv('NTP_HOST') + # Offer up to 3 ntp api's. + self.NTP_HOST = os.getenv('NTP_HOST').split('|', 2) self.INTERVAL = os.getenv('NTP_INTERVAL') if self.TZ is None or self.NTP_HOST is None or self.INTERVAL is None: raise Exception("NTP_HOST, NTP_INTERVAL & TZ_OFFSET are stored in settings.toml, please add them") + self._last_ntp_sync = None self.connect() @@ -46,14 +48,18 @@ def connect(self) -> bool: def get_time(self): - # Need better connection testing - # has IP before connecting. - #if wifi.radio.ipv4_address is None: - #self.connect() # This just feels wrong to connect every time. pool = socketpool.SocketPool(wifi.radio) - ntp = adafruit_ntp.NTP(pool, tz_offset=self.TZ, server=self.NTP_HOST) - return ntp.datetime - + ntp_try = 0 + while ntp_try < len(self.NTP_HOST): + try: + ntp = adafruit_ntp.NTP(pool, tz_offset=self.TZ, server=self.NTP_HOST[ntp_try]) + self._last_ntp_sync = ntp.datetime + return ntp.datetime + except Exception as ex: + print(f'Unable to connect to NTP Server {self.NTP_HOST[ntp_try]} with exception:', ex) + ntp_try += 1 + raise Exception("Unable to contact NTP servers") + def getJson(self, url): try: diff --git a/src/settings.toml b/src/settings.toml index d3883a9..d627015 100644 --- a/src/settings.toml +++ b/src/settings.toml @@ -1,8 +1,9 @@ WIFI_SSID="yourWIFI" WIFI_PASSWORD="YourPassword" -NTP_HOST="0.adafruit.pool.ntp.org" +NTP_HOST="0.adafruit.pool.ntp.org|0.us.pool.ntp.org" TZ_OFFSET=-5 -NTP_INTERVAL=60 +# 21600 = 6hr, 43200 = 12hr, 86400 = 24hr +NTP_INTERVAL=21600 # weather settings # imperial or metric UNITS="imperial"