From 21d8f501b35228a41382ecf325a142a6fc49b21d Mon Sep 17 00:00:00 2001 From: anecdata <16617689+anecdata@users.noreply.github.com> Date: Thu, 14 Mar 2024 21:22:57 -0500 Subject: [PATCH 1/9] Example using adafruit_connection_manager Example using adafruit_connection_manager to allow alternate radios (sources of sockets) Addresses: https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k/issues/132 Tested with CP9.0.0.* on: Adafruit QY Py ESP32-S3 Raspberry Pi Pico W Adafruit Ethernet FeatherWing Tested on Adafruit PyPortal, but ESP32SPI currently has no context manager for sockets, and the NTP library requires this. --- examples/ntp_connection_manager.py | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 examples/ntp_connection_manager.py diff --git a/examples/ntp_connection_manager.py b/examples/ntp_connection_manager.py new file mode 100644 index 0000000..93fcdd0 --- /dev/null +++ b/examples/ntp_connection_manager.py @@ -0,0 +1,39 @@ +import time +import adafruit_connection_manager +import adafruit_ntp + +try: + import wifi + import os + # adjust method to get credentials as necessary... + wifi_ssid = os.getenv("CIRCUITPY_WIFI_SSID") + wifi_password = os.getenv("CIRCUITPY_WIFI_PASSWORD") + radio = wifi.radio + while not radio.connected: + radio.connect(wifi_ssid, wifi_password) +except ImportError: + import board + from digitalio import DigitalInOut + spi = board.SPI() + try: + from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K + # adjust pin for the specific board... + eth_cs = DigitalInOut(board.D10) + radio = WIZNET5K(spi, eth_cs) + except ImportError: + from adafruit_esp32spi.adafruit_esp32spi import ESP_SPIcontrol + # adjust pins for the specific board... + esp32_cs = DigitalInOut(board.ESP_CS) + esp32_ready = DigitalInOut(board.ESP_BUSY) + esp32_reset = DigitalInOut(board.ESP_RESET) + radio = ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) + +# get the socket pool from connection manager +socket = adafruit_connection_manager.get_radio_socketpool(radio) + +# adjust tz_offset for locale... +ntp = adafruit_ntp.NTP(socket, tz_offset=-5) + +while True: + print(ntp.datetime) + time.sleep(5) \ No newline at end of file From 5f56ecbcfa6b7cdf9ba089627c6c6b7597d7ef44 Mon Sep 17 00:00:00 2001 From: anecdata <16617689+anecdata@users.noreply.github.com> Date: Thu, 14 Mar 2024 21:39:14 -0500 Subject: [PATCH 2/9] Update ntp_connection_manager.py pre-commit fixes --- examples/ntp_connection_manager.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/examples/ntp_connection_manager.py b/examples/ntp_connection_manager.py index 93fcdd0..7439c99 100644 --- a/examples/ntp_connection_manager.py +++ b/examples/ntp_connection_manager.py @@ -1,3 +1,10 @@ +# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries +# SPDX-FileCopyrightText: 2024 anecdata for Adafruit Industries +# +# SPDX-License-Identifier: Unlicense + +"""Print out time based on NTP.""" + import time import adafruit_connection_manager import adafruit_ntp @@ -36,4 +43,4 @@ while True: print(ntp.datetime) - time.sleep(5) \ No newline at end of file + time.sleep(5) From b8f2b47a5caf3767a025104e040301053de13c5f Mon Sep 17 00:00:00 2001 From: anecdata <16617689+anecdata@users.noreply.github.com> Date: Thu, 14 Mar 2024 21:46:43 -0500 Subject: [PATCH 3/9] Update ntp_connection_manager.py black fixes --- examples/ntp_connection_manager.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/ntp_connection_manager.py b/examples/ntp_connection_manager.py index 7439c99..d2bfde4 100644 --- a/examples/ntp_connection_manager.py +++ b/examples/ntp_connection_manager.py @@ -3,7 +3,7 @@ # # SPDX-License-Identifier: Unlicense -"""Print out time based on NTP.""" +"""Print out time based on NTP, using connection manager""" import time import adafruit_connection_manager @@ -12,6 +12,7 @@ try: import wifi import os + # adjust method to get credentials as necessary... wifi_ssid = os.getenv("CIRCUITPY_WIFI_SSID") wifi_password = os.getenv("CIRCUITPY_WIFI_PASSWORD") @@ -21,14 +22,17 @@ except ImportError: import board from digitalio import DigitalInOut + spi = board.SPI() try: from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K + # adjust pin for the specific board... eth_cs = DigitalInOut(board.D10) radio = WIZNET5K(spi, eth_cs) except ImportError: from adafruit_esp32spi.adafruit_esp32spi import ESP_SPIcontrol + # adjust pins for the specific board... esp32_cs = DigitalInOut(board.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) From 2ae8d7de97b4d4b5bc5ba3d4dabb8bee6ec2aec3 Mon Sep 17 00:00:00 2001 From: anecdata <16617689+anecdata@users.noreply.github.com> Date: Fri, 15 Mar 2024 10:42:16 -0500 Subject: [PATCH 4/9] Update ntp_connection_manager.py added SPI comment --- examples/ntp_connection_manager.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/ntp_connection_manager.py b/examples/ntp_connection_manager.py index d2bfde4..8d49426 100644 --- a/examples/ntp_connection_manager.py +++ b/examples/ntp_connection_manager.py @@ -23,6 +23,7 @@ import board from digitalio import DigitalInOut + # adjust with busio.SPI() as necessary... spi = board.SPI() try: from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K From 0308e8af8785761561776709e0f14baa55ae1c01 Mon Sep 17 00:00:00 2001 From: anecdata <16617689+anecdata@users.noreply.github.com> Date: Fri, 15 Mar 2024 10:46:44 -0500 Subject: [PATCH 5/9] Update ntp_connection_manager.py added comment about radios --- examples/ntp_connection_manager.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/ntp_connection_manager.py b/examples/ntp_connection_manager.py index 8d49426..dec5fc6 100644 --- a/examples/ntp_connection_manager.py +++ b/examples/ntp_connection_manager.py @@ -9,6 +9,7 @@ import adafruit_connection_manager import adafruit_ntp +# determine which radio is available try: import wifi import os From 6ecae6f8a043d5c2c9ce8cce94731e355c82ce33 Mon Sep 17 00:00:00 2001 From: anecdata <16617689+anecdata@users.noreply.github.com> Date: Fri, 15 Mar 2024 12:43:35 -0500 Subject: [PATCH 6/9] Update ntp_connection_manager.py remove esp32spi for now (several incompatibilities with NTP implementation) --- examples/ntp_connection_manager.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/examples/ntp_connection_manager.py b/examples/ntp_connection_manager.py index dec5fc6..8787209 100644 --- a/examples/ntp_connection_manager.py +++ b/examples/ntp_connection_manager.py @@ -33,13 +33,7 @@ eth_cs = DigitalInOut(board.D10) radio = WIZNET5K(spi, eth_cs) except ImportError: - from adafruit_esp32spi.adafruit_esp32spi import ESP_SPIcontrol - - # adjust pins for the specific board... - esp32_cs = DigitalInOut(board.ESP_CS) - esp32_ready = DigitalInOut(board.ESP_BUSY) - esp32_reset = DigitalInOut(board.ESP_RESET) - radio = ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) + raise # get the socket pool from connection manager socket = adafruit_connection_manager.get_radio_socketpool(radio) From 3b31b06970e2eea7f88211d5a322551c88a201fa Mon Sep 17 00:00:00 2001 From: anecdata <16617689+anecdata@users.noreply.github.com> Date: Fri, 15 Mar 2024 12:51:04 -0500 Subject: [PATCH 7/9] Update ntp_connection_manager.py once more, with feeling --- examples/ntp_connection_manager.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/examples/ntp_connection_manager.py b/examples/ntp_connection_manager.py index 8787209..dc2d1f4 100644 --- a/examples/ntp_connection_manager.py +++ b/examples/ntp_connection_manager.py @@ -23,17 +23,13 @@ except ImportError: import board from digitalio import DigitalInOut + from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K # adjust with busio.SPI() as necessary... spi = board.SPI() - try: - from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K - - # adjust pin for the specific board... - eth_cs = DigitalInOut(board.D10) - radio = WIZNET5K(spi, eth_cs) - except ImportError: - raise + # adjust pin for the specific board... + eth_cs = DigitalInOut(board.D10) + radio = WIZNET5K(spi, eth_cs) # get the socket pool from connection manager socket = adafruit_connection_manager.get_radio_socketpool(radio) From 1c801d4affd34f936441731d1081590afda93c78 Mon Sep 17 00:00:00 2001 From: anecdata <16617689+anecdata@users.noreply.github.com> Date: Sun, 17 Mar 2024 08:38:00 -0500 Subject: [PATCH 8/9] remove while True --- examples/ntp_connection_manager.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/ntp_connection_manager.py b/examples/ntp_connection_manager.py index dc2d1f4..e833fc8 100644 --- a/examples/ntp_connection_manager.py +++ b/examples/ntp_connection_manager.py @@ -37,6 +37,4 @@ # adjust tz_offset for locale... ntp = adafruit_ntp.NTP(socket, tz_offset=-5) -while True: - print(ntp.datetime) - time.sleep(5) +print(ntp.datetime) From 541c3945a795d49541a85efffc6153e5e17fced2 Mon Sep 17 00:00:00 2001 From: anecdata <16617689+anecdata@users.noreply.github.com> Date: Sun, 17 Mar 2024 08:41:48 -0500 Subject: [PATCH 9/9] remove unneeded import time --- examples/ntp_connection_manager.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/ntp_connection_manager.py b/examples/ntp_connection_manager.py index e833fc8..b3cd28b 100644 --- a/examples/ntp_connection_manager.py +++ b/examples/ntp_connection_manager.py @@ -5,7 +5,6 @@ """Print out time based on NTP, using connection manager""" -import time import adafruit_connection_manager import adafruit_ntp