Skip to content

Commit 185a454

Browse files
authored
Merge pull request #12 from ladyada/master
Update to use adafruit.io for strftime service
2 parents 1939a1e + 9d778c7 commit 185a454

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

adafruit_pyportal.py

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@
7878
# pylint: disable=line-too-long
7979
# you'll need to pass in an io username, width, height, format (bit depth), io key, and then url!
8080
IMAGE_CONVERTER_SERVICE = "https://io.adafruit.com/api/v2/%s/integrations/image-formatter?x-aio-key=%s&width=%d&height=%d&output=BMP%d&url=%s"
81-
82-
TIME_SERVICE_IPADDR = "http://worldtimeapi.org/api/ip"
83-
TIME_SERVICE_LOCATION = "http://worldtimeapi.org/api/timezone/"
81+
# you'll need to pass in an io username and key
82+
TIME_SERVICE = "https://io.adafruit.com/api/v2/%s/integrations/time/strftime?x-aio-key=%s"
83+
# our strftime is %Y-%m-%d %H:%M:%S.%L %j %u %z %Z see http://strftime.net/ for decoding details
84+
# See https://apidock.com/ruby/DateTime/strftime for full options
85+
TIME_SERVICE_STRFTIME = '&fmt=%25Y-%25m-%25d+%25H%3A%25M%3A%25S.%25L+%25j+%25u+%25z+%25Z'
8486
LOCALFILE = "local.txt"
8587
# pylint: enable=line-too-long
8688

@@ -224,22 +226,22 @@ def __init__(self, *, url=None, json_path=None, regexp_path=None,
224226
esp32_cs = DigitalInOut(board.ESP_CS)
225227
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
226228

227-
if url and not self._uselocal:
228-
self._esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready,
229-
esp32_reset, esp32_gpio0)
230-
#self._esp._debug = 1
231-
for _ in range(3): # retries
232-
try:
233-
print("ESP firmware:", self._esp.firmware_version)
234-
break
235-
except RuntimeError:
236-
print("Retrying ESP32 connection")
237-
time.sleep(1)
238-
self._esp.reset()
239-
else:
240-
raise RuntimeError("Was not able to find ESP32")
229+
self._esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready,
230+
esp32_reset, esp32_gpio0)
231+
#self._esp._debug = 1
232+
for _ in range(3): # retries
233+
try:
234+
print("ESP firmware:", self._esp.firmware_version)
235+
break
236+
except RuntimeError:
237+
print("Retrying ESP32 connection")
238+
time.sleep(1)
239+
self._esp.reset()
240+
else:
241+
raise RuntimeError("Was not able to find ESP32")
242+
requests.set_interface(self._esp)
241243

242-
requests.set_interface(self._esp)
244+
if url and not self._uselocal:
243245
self._connect_esp()
244246

245247
# set the default background
@@ -512,24 +514,33 @@ def get_local_time(self, location=None):
512514
# pylint: enable=line-too-long
513515
self._connect_esp()
514516
api_url = None
517+
try:
518+
aio_username = secrets['aio_username']
519+
aio_key = secrets['aio_key']
520+
except KeyError:
521+
raise KeyError("\n\nOur time service requires a login/password to rate-limit. Please register for a free adafruit.io account and place the user/key in your secrets file under 'aio_username' and 'aio_key'")# pylint: disable=line-too-long
522+
515523
location = secrets.get('timezone', location)
516524
if location:
517525
print("Getting time for timezone", location)
518-
api_url = TIME_SERVICE_LOCATION + location
526+
api_url = (TIME_SERVICE + "&tz=%s") % (aio_username, aio_key, location)
519527
else: # we'll try to figure it out from the IP address
520528
print("Getting time from IP address")
521-
api_url = TIME_SERVICE_IPADDR
522-
529+
api_url = TIME_SERVICE % (aio_username, aio_key)
530+
api_url += TIME_SERVICE_STRFTIME
523531
try:
524532
response = requests.get(api_url)
525-
time_json = response.json()
526-
current_time = time_json['datetime']
527-
year_day = time_json['day_of_year']
528-
week_day = time_json['day_of_week']
529-
is_dst = time_json['dst']
533+
if self._debug:
534+
print("Time request: ", api_url)
535+
print("Time reply: ", response.text)
536+
times = response.text.split(' ')
537+
the_date = times[0]
538+
the_time = times[1]
539+
year_day = int(times[2])
540+
week_day = int(times[3])
541+
is_dst = None # no way to know yet
530542
except KeyError:
531543
raise KeyError("Was unable to lookup the time, try setting secrets['timezone'] according to http://worldtimeapi.org/timezones") # pylint: disable=line-too-long
532-
the_date, the_time = current_time.split('T')
533544
year, month, mday = [int(x) for x in the_date.split('-')]
534545
the_time = the_time.split('.')[0]
535546
hours, minutes, seconds = [int(x) for x in the_time.split(':')]
@@ -539,7 +550,6 @@ def get_local_time(self, location=None):
539550
rtc.RTC().datetime = now
540551

541552
# now clean up
542-
time_json = None
543553
response.close()
544554
response = None
545555
gc.collect()
@@ -612,7 +622,7 @@ def image_converter_url(image_url, width, height, color_depth=16):
612622
aio_username = secrets['aio_username']
613623
aio_key = secrets['aio_key']
614624
except KeyError:
615-
raise KeyError("\n\nOur image converter service require a login/password to rate-limit. Please register for a freeadafruit.io account and place the user/key in your secrets file under 'aio_username' and 'aio_key'")# pylint: disable=line-too-long
625+
raise KeyError("\n\nOur image converter service require a login/password to rate-limit. Please register for a free adafruit.io account and place the user/key in your secrets file under 'aio_username' and 'aio_key'")# pylint: disable=line-too-long
616626

617627
return IMAGE_CONVERTER_SERVICE % (aio_username, aio_key,
618628
width, height,

0 commit comments

Comments
 (0)