Skip to content

Commit 40e88ed

Browse files
authored
Merge pull request #36 from VPTechOps/Add-RTC-set-from-NTP
Update code.py to handle RTC and NTP
2 parents 97dd7bf + 1e219f2 commit 40e88ed

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

examples/camera/code.py

+48-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,61 @@
22
# SPDX-FileCopyrightText: 2023 Limor Fried for Adafruit Industries
33
#
44
# SPDX-License-Identifier: Unlicense
5-
5+
import ssl
6+
import os
67
import time
8+
import socketpool
9+
import adafruit_requests
10+
import rtc
11+
import adafruit_ntp
12+
import wifi
713
import bitmaptools
814
import displayio
915
import gifio
1016
import ulab.numpy as np
1117

1218
import adafruit_pycamera
1319

20+
# Wifi details are in settings.toml file, also,
21+
# timezone info should be included to allow local time and DST adjustments
22+
# # UTC_OFFSET, if present, will override TZ and DST and no API query will be done
23+
# UTC_OFFSET=-25200
24+
# # TZ="America/Phoenix"
25+
26+
UTC_OFFSET = os.getenv("UTC_OFFSET")
27+
TZ = os.getenv("TZ")
28+
29+
print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}")
30+
SSID = os.getenv("CIRCUITPY_WIFI_SSID")
31+
PASSWORD = os.getenv("CIRCUITPY_WIFI_PASSWORD")
32+
33+
if SSID and PASSWORD:
34+
wifi.radio.connect(
35+
os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD")
36+
)
37+
if wifi.radio.connected:
38+
print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}!")
39+
print("My IP address is", wifi.radio.ipv4_address)
40+
pool = socketpool.SocketPool(wifi.radio)
41+
42+
if UTC_OFFSET is None:
43+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
44+
response = requests.get("http://worldtimeapi.org/api/timezone/" + TZ)
45+
response_as_json = response.json()
46+
UTC_OFFSET = response_as_json["raw_offset"] + response_as_json["dst_offset"]
47+
print(f"UTC_OFFSET: {UTC_OFFSET}")
48+
49+
ntp = adafruit_ntp.NTP(
50+
pool, server="pool.ntp.org", tz_offset=UTC_OFFSET // 3600
51+
)
52+
53+
print(f"ntp time: {ntp.datetime}")
54+
rtc.RTC().datetime = ntp.datetime
55+
else:
56+
print("Wifi failed to connect. Time not set.")
57+
else:
58+
print("Wifi config not found in settintgs.toml. Time not set.")
59+
1460
pycam = adafruit_pycamera.PyCamera()
1561
# pycam.live_preview_mode()
1662

@@ -163,7 +209,7 @@
163209
t0 = t1
164210
pycam._mode_label.text = "GIF" # pylint: disable=protected-access
165211
print(f"\nfinal size {f.tell()} for {i} frames")
166-
print(f"average framerate {i/(t1-t00)}fps")
212+
print(f"average framerate {i / (t1 - t00)}fps")
167213
print(f"best {max(ft)} worst {min(ft)} std. deviation {np.std(ft)}")
168214
f.close()
169215
pycam.display.refresh()

0 commit comments

Comments
 (0)