From e0dc69aaf9d5d64b6c5ce1e9a51564d7bce560d6 Mon Sep 17 00:00:00 2001 From: Fred Meyer <56278928+VPTechOps@users.noreply.github.com> Date: Sun, 17 Mar 2024 10:21:26 -0700 Subject: [PATCH 1/9] Update code.py to handle RTC and NTP --- examples/camera/code.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/examples/camera/code.py b/examples/camera/code.py index 553c035..5a3ae9a 100644 --- a/examples/camera/code.py +++ b/examples/camera/code.py @@ -3,11 +3,49 @@ # # SPDX-License-Identifier: Unlicense +import ipaddress +import ssl +import wifi +import socketpool +import adafruit_requests +import os +import rtc +import adafruit_ntp +from adafruit_datetime import datetime + import time 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") import adafruit_pycamera From 076ea3c6452eb50a4f52415433f5656ee8be8c56 Mon Sep 17 00:00:00 2001 From: Fred Meyer <56278928+VPTechOps@users.noreply.github.com> Date: Sun, 17 Mar 2024 10:36:59 -0700 Subject: [PATCH 2/9] Update code.py to correct errors in pull request checks --- examples/camera/code.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/camera/code.py b/examples/camera/code.py index 5a3ae9a..4f2e17a 100644 --- a/examples/camera/code.py +++ b/examples/camera/code.py @@ -3,21 +3,21 @@ # # SPDX-License-Identifier: Unlicense -import ipaddress +import adafruit_pycamera + import ssl -import wifi import socketpool import adafruit_requests import os import rtc import adafruit_ntp -from adafruit_datetime import datetime - import time +import wifi import bitmaptools import displayio import gifio import ulab.numpy as np + UTC_OFFSET = os.getenv('UTC_OFFSET') TZ = os.getenv('TZ') From 481409725594815284201c905da0c810905d7497 Mon Sep 17 00:00:00 2001 From: Fred Meyer <56278928+VPTechOps@users.noreply.github.com> Date: Sun, 17 Mar 2024 10:40:00 -0700 Subject: [PATCH 3/9] Update code.py correct errora in pull request checks --- examples/camera/code.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/camera/code.py b/examples/camera/code.py index 4f2e17a..71c4d7f 100644 --- a/examples/camera/code.py +++ b/examples/camera/code.py @@ -47,8 +47,6 @@ print("Wifi error:", e) print("Time not set") -import adafruit_pycamera - pycam = adafruit_pycamera.PyCamera() # pycam.live_preview_mode() From 272f80e2c07b8769885d8d0260c9e1be80a14dfa Mon Sep 17 00:00:00 2001 From: Fred Meyer <56278928+VPTechOps@users.noreply.github.com> Date: Sun, 17 Mar 2024 10:51:20 -0700 Subject: [PATCH 4/9] Update code.py Correct errors in pull request checks --- examples/camera/code.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/camera/code.py b/examples/camera/code.py index 71c4d7f..61c0a56 100644 --- a/examples/camera/code.py +++ b/examples/camera/code.py @@ -3,21 +3,21 @@ # # SPDX-License-Identifier: Unlicense -import adafruit_pycamera - import ssl +import os +import time import socketpool import adafruit_requests -import os import rtc import adafruit_ntp -import time import wifi import bitmaptools import displayio import gifio import ulab.numpy as np +import adafruit_pycamera + UTC_OFFSET = os.getenv('UTC_OFFSET') TZ = os.getenv('TZ') From ffa8575fc389bad95b9292af1c518ed12ebba3a9 Mon Sep 17 00:00:00 2001 From: Fred Meyer Date: Sun, 17 Mar 2024 16:57:17 -0700 Subject: [PATCH 5/9] Change to run pre-commit --- examples/camera/code.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/examples/camera/code.py b/examples/camera/code.py index 61c0a56..a4e6868 100644 --- a/examples/camera/code.py +++ b/examples/camera/code.py @@ -2,7 +2,6 @@ # SPDX-FileCopyrightText: 2023 Limor Fried for Adafruit Industries # # SPDX-License-Identifier: Unlicense - import ssl import os import time @@ -18,19 +17,21 @@ import adafruit_pycamera -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 +# timezone info should be included in settings.toml 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") + 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(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}") + wifi.radio.connect( + os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD") + ) + print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}!") print("My IP address is", wifi.radio.ipv4_address) pool = socketpool.SocketPool(wifi.radio) From bf348c19d46424d4dc3cac940a759ba94f06809f Mon Sep 17 00:00:00 2001 From: Fred Meyer Date: Sun, 17 Mar 2024 17:26:13 -0700 Subject: [PATCH 6/9] Correct 1 more pre-commit error --- examples/camera/code.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/examples/camera/code.py b/examples/camera/code.py index a4e6868..a8dfc48 100644 --- a/examples/camera/code.py +++ b/examples/camera/code.py @@ -26,11 +26,11 @@ UTC_OFFSET = os.getenv("UTC_OFFSET") TZ = os.getenv("TZ") -try: - print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}") - wifi.radio.connect( - os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD") +print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}") +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) @@ -44,9 +44,8 @@ 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") +else: + print("Wifi failed to connect. Time not set.") pycam = adafruit_pycamera.PyCamera() # pycam.live_preview_mode() From 3fb24e986a43998a9beaa0105fe7e27fb24ba526 Mon Sep 17 00:00:00 2001 From: Fred Meyer Date: Sun, 17 Mar 2024 17:59:58 -0700 Subject: [PATCH 7/9] Correct DOS file endings --- examples/camera/code.py | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/examples/camera/code.py b/examples/camera/code.py index 5a3ae9a..6c957c0 100644 --- a/examples/camera/code.py +++ b/examples/camera/code.py @@ -2,35 +2,37 @@ # SPDX-FileCopyrightText: 2023 Limor Fried for Adafruit Industries # # SPDX-License-Identifier: Unlicense - -import ipaddress import ssl -import wifi +import os +import time import socketpool import adafruit_requests -import os import rtc import adafruit_ntp -from adafruit_datetime import datetime - -import time +import wifi import bitmaptools import displayio import gifio import ulab.numpy as np -UTC_OFFSET = os.getenv('UTC_OFFSET') -TZ = os.getenv('TZ') + +import adafruit_pycamera # Wifi details are in settings.toml file, also, -# timezone info should be included to allow local time and DST adjustments +# timezone info should be included in settings.toml 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")) +UTC_OFFSET = os.getenv("UTC_OFFSET") +TZ = os.getenv("TZ") + +print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}") +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) @@ -43,11 +45,8 @@ 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") - -import adafruit_pycamera +else: + print("Wifi failed to connect. Time not set.") pycam = adafruit_pycamera.PyCamera() # pycam.live_preview_mode() From bf5559c3d3dc13c00187328cf4195af3c21b1e5d Mon Sep 17 00:00:00 2001 From: Fred Meyer Date: Sun, 17 Mar 2024 18:13:18 -0700 Subject: [PATCH 8/9] Correct Another DOS line ending --- examples/camera/code.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/camera/code.py b/examples/camera/code.py index 37b7b0b..1825f23 100644 --- a/examples/camera/code.py +++ b/examples/camera/code.py @@ -29,7 +29,7 @@ print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}") 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) From 1e219f2d85a90cacab3819a224b15da727ed2997 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Fri, 10 May 2024 17:39:41 -0500 Subject: [PATCH 9/9] continue if no WIFI --- examples/camera/code.py | 42 +++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/examples/camera/code.py b/examples/camera/code.py index 1825f23..a03ea64 100644 --- a/examples/camera/code.py +++ b/examples/camera/code.py @@ -27,25 +27,35 @@ TZ = os.getenv("TZ") print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}") -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) +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"] + 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) + ntp = adafruit_ntp.NTP( + pool, server="pool.ntp.org", tz_offset=UTC_OFFSET // 3600 + ) - rtc.RTC().datetime = ntp.datetime + print(f"ntp time: {ntp.datetime}") + rtc.RTC().datetime = ntp.datetime + else: + print("Wifi failed to connect. Time not set.") else: - print("Wifi failed to connect. Time not set.") + print("Wifi config not found in settintgs.toml. Time not set.") pycam = adafruit_pycamera.PyCamera() # pycam.live_preview_mode() @@ -199,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()