Skip to content

Commit

Permalink
Merge pull request #36 from VPTechOps/Add-RTC-set-from-NTP
Browse files Browse the repository at this point in the history
Update code.py to handle RTC and NTP
  • Loading branch information
FoamyGuy authored May 10, 2024
2 parents 97dd7bf + 1e219f2 commit 40e88ed
Showing 1 changed file with 48 additions and 2 deletions.
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

0 comments on commit 40e88ed

Please sign in to comment.