-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathconnect_circuitpython.py
More file actions
executable file
·65 lines (52 loc) · 2.03 KB
/
Copy pathconnect_circuitpython.py
File metadata and controls
executable file
·65 lines (52 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# SPDX-FileCopyrightText: Copyright (c) 2022 Neradoc
# SPDX-License-Identifier: MIT
from secrets import secrets
def connect_wifi():
try:
import wifi
native_wifi = True
except:
native_wifi = False
if native_wifi:
"""
That's for native wifi.
"""
import socketpool
import ssl
print("CONNECT WIFI")
wifi.radio.connect(secrets["ssid"], secrets["password"])
socket = socketpool.SocketPool(wifi.radio)
ssl_context = ssl.create_default_context()
return (socket, ssl_context, None)
else:
"""
That's for an ESP32 wifi coprocessor (airlift breakouts and integrated).
Change the pins to those that match your board.
"""
import board
import busio
from digitalio import DigitalInOut
from adafruit_esp32spi import adafruit_esp32spi
if hasattr(board, "ESP_CS"):
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
else:
# If you have an externally connected ESP32:
esp32_cs = DigitalInOut(board.D13) # CHANGE TO MATCH YOUR BOARD
esp32_ready = DigitalInOut(board.D11) # CHANGE TO MATCH YOUR BOARD
esp32_reset = DigitalInOut(board.D12) # CHANGE TO MATCH YOUR BOARD
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
print("CONNECT WIFI")
while not esp.is_connected:
try:
esp.connect_AP(secrets["ssid"], secrets["password"])
except RuntimeError as e:
print("could not connect to AP, retrying: ", e)
continue
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
socket.set_interface(esp)
return (socket, None, esp)
return (None, None, None)