Closed jkvato closed 1 year ago
Hi jkvato,
I had the same problem, but then found another solution. In the requested json data, what is written at datetime
is correct. That's why I used this.
# Raspberry Pico W
import board
import busio
import wifi
import socketpool
import adafruit_requests as requests
from secrets import secrets
import time
from adafruit_datetime import datetime
import adafruit_ds3231
i2c = busio.I2C(board.GP21, board.GP20)
rtc = adafruit_ds3231.DS3231(i2c)
print(f'Connecting to {secrets["ssid"]}')
wifi.radio.connect(secrets["ssid"], secrets["password"])
print(f'Connected to {secrets["ssid"]}')
pool = socketpool.SocketPool(wifi.radio)
def get_internet_time(socketpool: socketpool.SocketPool) -> time.struct_time:
'''Return a struct_time object with current time from internet request.'''
request = requests.Session(socketpool)
response = request.get('http://worldtimeapi.org/api/ip').json()['datetime']
_datetime = datetime.fromisoformat(response)
return _datetime.timetuple()
rtc.datetime = get_internet_time(pool)
print(rtc.datetime)
Hope this helps you.
Hi @deilers78,
That's a simple approach, for sure. Thanks for sharing!
In the section "Set RTC time from time service," the field
raw_offset
from the json received from the time service does not take daylight savings into consideration. However,utc_offset
does. I was getting the wrong local time here in Southern California during daylight savings until I made the following modification:Here is the json data I received from worldtimeapi.org:
Here we can verify that
raw_offset
of -28800 = -8 hours, bututc_offset
is -7 hours, which is correct during DST.