Skip to content

Commit

Permalink
Merge pull request #52 from jake1164/ntp-servers
Browse files Browse the repository at this point in the history
Added multiple NTP server support
  • Loading branch information
jake1164 authored Aug 27, 2023
2 parents 8254c20 + b3c5a04 commit 4710529
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<timezone 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"
Expand Down
22 changes: 14 additions & 8 deletions src/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand All @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions src/settings.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 4710529

Please sign in to comment.