Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update code.py to handle RTC and NTP #36

Merged
merged 10 commits into from
May 10, 2024
50 changes: 48 additions & 2 deletions examples/camera/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,61 @@
# 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
import ulab.numpy as np

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()

Expand Down Expand Up @@ -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()
Expand Down
Loading