Skip to content

Commit 21d8f50

Browse files
authored
Example using adafruit_connection_manager
Example using adafruit_connection_manager to allow alternate radios (sources of sockets) Addresses: adafruit/Adafruit_CircuitPython_Wiznet5k#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.
1 parent 82c9abf commit 21d8f50

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

examples/ntp_connection_manager.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import time
2+
import adafruit_connection_manager
3+
import adafruit_ntp
4+
5+
try:
6+
import wifi
7+
import os
8+
# adjust method to get credentials as necessary...
9+
wifi_ssid = os.getenv("CIRCUITPY_WIFI_SSID")
10+
wifi_password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
11+
radio = wifi.radio
12+
while not radio.connected:
13+
radio.connect(wifi_ssid, wifi_password)
14+
except ImportError:
15+
import board
16+
from digitalio import DigitalInOut
17+
spi = board.SPI()
18+
try:
19+
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
20+
# adjust pin for the specific board...
21+
eth_cs = DigitalInOut(board.D10)
22+
radio = WIZNET5K(spi, eth_cs)
23+
except ImportError:
24+
from adafruit_esp32spi.adafruit_esp32spi import ESP_SPIcontrol
25+
# adjust pins for the specific board...
26+
esp32_cs = DigitalInOut(board.ESP_CS)
27+
esp32_ready = DigitalInOut(board.ESP_BUSY)
28+
esp32_reset = DigitalInOut(board.ESP_RESET)
29+
radio = ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
30+
31+
# get the socket pool from connection manager
32+
socket = adafruit_connection_manager.get_radio_socketpool(radio)
33+
34+
# adjust tz_offset for locale...
35+
ntp = adafruit_ntp.NTP(socket, tz_offset=-5)
36+
37+
while True:
38+
print(ntp.datetime)
39+
time.sleep(5)

0 commit comments

Comments
 (0)