Skip to content

Commit

Permalink
Update code.py to handle RTC and NTP
Browse files Browse the repository at this point in the history
  • Loading branch information
VPTechOps authored Mar 17, 2024
1 parent 97dd7bf commit e0dc69a
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions examples/camera/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,49 @@
#
# SPDX-License-Identifier: Unlicense

import ipaddress

Check failure on line 6 in examples/camera/code.py

View workflow job for this annotation

GitHub Actions / test

Unused import ipaddress
import ssl
import wifi
import socketpool
import adafruit_requests
import os

Check failure on line 11 in examples/camera/code.py

View workflow job for this annotation

GitHub Actions / test

standard import "import os" should be placed before "import wifi"
import rtc
import adafruit_ntp
from adafruit_datetime import datetime

Check failure on line 14 in examples/camera/code.py

View workflow job for this annotation

GitHub Actions / test

Unused datetime imported from adafruit_datetime

import time

Check failure on line 16 in examples/camera/code.py

View workflow job for this annotation

GitHub Actions / test

standard import "import time" should be placed before "import wifi"
import bitmaptools
import displayio
import gifio
import ulab.numpy as np
UTC_OFFSET = os.getenv('UTC_OFFSET')
TZ = os.getenv('TZ')

# 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"

try:
print("Connecting to %s"%os.getenv("CIRCUITPY_WIFI_SSID"))
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
print("Connected to %s!"%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"]

ntp = adafruit_ntp.NTP(pool, server="pool.ntp.org", tz_offset=UTC_OFFSET // 3600)

rtc.RTC().datetime = ntp.datetime
except Exception as e:
print("Wifi error:", e)
print("Time not set")

Check failure on line 49 in examples/camera/code.py

View workflow job for this annotation

GitHub Actions / test

Catching too general exception Exception
import adafruit_pycamera

Expand Down

0 comments on commit e0dc69a

Please sign in to comment.