diff --git a/examples/camera/code.py b/examples/camera/code.py index 553c035..a03ea64 100644 --- a/examples/camera/code.py +++ b/examples/camera/code.py @@ -2,8 +2,14 @@ # SPDX-FileCopyrightText: 2023 Limor Fried for Adafruit Industries # # SPDX-License-Identifier: Unlicense - +import ssl +import os import time +import socketpool +import adafruit_requests +import rtc +import adafruit_ntp +import wifi import bitmaptools import displayio import gifio @@ -11,6 +17,46 @@ import adafruit_pycamera +# Wifi details are in settings.toml file, also, +# timezone info should be included to allow local time and DST adjustments +# # UTC_OFFSET, if present, will override TZ and DST and no API query will be done +# UTC_OFFSET=-25200 +# # TZ="America/Phoenix" + +UTC_OFFSET = os.getenv("UTC_OFFSET") +TZ = os.getenv("TZ") + +print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}") +SSID = os.getenv("CIRCUITPY_WIFI_SSID") +PASSWORD = os.getenv("CIRCUITPY_WIFI_PASSWORD") + +if SSID and PASSWORD: + wifi.radio.connect( + os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD") + ) + if wifi.radio.connected: + print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}!") + print("My IP address is", wifi.radio.ipv4_address) + pool = socketpool.SocketPool(wifi.radio) + + if UTC_OFFSET is None: + requests = adafruit_requests.Session(pool, ssl.create_default_context()) + response = requests.get("http://worldtimeapi.org/api/timezone/" + TZ) + response_as_json = response.json() + UTC_OFFSET = response_as_json["raw_offset"] + response_as_json["dst_offset"] + print(f"UTC_OFFSET: {UTC_OFFSET}") + + ntp = adafruit_ntp.NTP( + pool, server="pool.ntp.org", tz_offset=UTC_OFFSET // 3600 + ) + + print(f"ntp time: {ntp.datetime}") + rtc.RTC().datetime = ntp.datetime + else: + print("Wifi failed to connect. Time not set.") +else: + print("Wifi config not found in settintgs.toml. Time not set.") + pycam = adafruit_pycamera.PyCamera() # pycam.live_preview_mode() @@ -163,7 +209,7 @@ t0 = t1 pycam._mode_label.text = "GIF" # pylint: disable=protected-access print(f"\nfinal size {f.tell()} for {i} frames") - print(f"average framerate {i/(t1-t00)}fps") + print(f"average framerate {i / (t1 - t00)}fps") print(f"best {max(ft)} worst {min(ft)} std. deviation {np.std(ft)}") f.close() pycam.display.refresh()