Skip to content

Commit 556f33e

Browse files
authored
Merge pull request #33 from makermelissa/main
Added a couple different WiFi modules
2 parents b09f7b3 + c3d63a7 commit 556f33e

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2+
# SPDX-FileCopyrightText: Copyright (c) 2021 Melissa LeBlanc-Williams for Adafruit Industries
3+
#
4+
# SPDX-License-Identifier: MIT
5+
"""
6+
`adafruit_portalbase.wifi_coprocessor`
7+
================================================================================
8+
9+
WiFi Helper module for the board using the WiFi CoProcessor.
10+
11+
* Author(s): Melissa LeBlanc-Williams
12+
13+
Implementation Notes
14+
--------------------
15+
16+
**Software and Dependencies:**
17+
18+
* Adafruit CircuitPython firmware for the supported boards:
19+
https://github.com/adafruit/circuitpython/releases
20+
21+
"""
22+
23+
import gc
24+
import board
25+
import busio
26+
from digitalio import DigitalInOut
27+
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
28+
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
29+
import adafruit_requests as requests
30+
31+
__version__ = "0.0.0-auto.0"
32+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PortalBase.git"
33+
34+
35+
class WiFi:
36+
"""Class representing the ESP.
37+
38+
:param status_led: The initialized object for status DotStar, NeoPixel, or RGB LED. Defaults
39+
to ``None``, to not use the status LED
40+
:param esp: A passed ESP32 object, Can be used in cases where the ESP32 chip needs to be used
41+
before calling the pyportal class. Defaults to ``None``.
42+
:param busio.SPI external_spi: A previously declared spi object. Defaults to ``None``.
43+
44+
"""
45+
46+
def __init__(self, *, status_led=None, esp=None, external_spi=None):
47+
48+
if status_led:
49+
self.neopix = status_led
50+
else:
51+
self.neopix = None
52+
self.neo_status(0)
53+
self.requests = None
54+
55+
if esp: # If there was a passed ESP Object
56+
self.esp = esp
57+
if external_spi: # If SPI Object Passed
58+
spi = external_spi
59+
else: # Else: Make ESP32 connection
60+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
61+
else:
62+
esp32_ready = DigitalInOut(board.ESP_BUSY)
63+
esp32_gpio0 = DigitalInOut(board.ESP_GPIO0)
64+
esp32_reset = DigitalInOut(board.ESP_RESET)
65+
esp32_cs = DigitalInOut(board.ESP_CS)
66+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
67+
68+
self.esp = adafruit_esp32spi.ESP_SPIcontrol(
69+
spi, esp32_cs, esp32_ready, esp32_reset, esp32_gpio0
70+
)
71+
72+
requests.set_socket(socket, self.esp)
73+
if self.esp.is_connected:
74+
self.requests = requests
75+
self._manager = None
76+
77+
gc.collect()
78+
79+
def connect(self, ssid, password):
80+
"""
81+
Connect to WiFi using the settings found in secrets.py
82+
"""
83+
self.esp.connect({"ssid": ssid, "password": password})
84+
self.requests = requests
85+
86+
def neo_status(self, value):
87+
"""The status NeoPixel.
88+
89+
:param value: The color to change the NeoPixel.
90+
91+
"""
92+
if self.neopix:
93+
self.neopix.fill(value)
94+
95+
def manager(self, secrets):
96+
"""Initialize the WiFi Manager if it hasn't been cached and return it"""
97+
if self._manager is None:
98+
self._manager = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(
99+
self.esp, secrets, None
100+
)
101+
return self._manager
102+
103+
@property
104+
def is_connected(self):
105+
"""Return whether we are connected."""
106+
return self.esp.is_connected
107+
108+
@property
109+
def enabled(self):
110+
"""Not currently disablable on the ESP32 Coprocessor"""
111+
return True

adafruit_portalbase/wifi_esp32s2.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2+
# SPDX-FileCopyrightText: Copyright (c) 2021 Melissa LeBlanc-Williams for Adafruit Industries
3+
#
4+
# SPDX-License-Identifier: MIT
5+
"""
6+
`adafruit_portalbase.wifi_esp32s2`
7+
================================================================================
8+
9+
WiFi Helper module for the ESP32-S2 based boards.
10+
11+
12+
* Author(s): Melissa LeBlanc-Williams
13+
14+
Implementation Notes
15+
--------------------
16+
17+
**Software and Dependencies:**
18+
19+
* Adafruit CircuitPython firmware for the supported boards:
20+
https://github.com/adafruit/circuitpython/releases
21+
22+
"""
23+
24+
import gc
25+
import ssl
26+
import wifi
27+
import socketpool
28+
import adafruit_requests
29+
30+
__version__ = "0.0.0-auto.0"
31+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PortalBase.git"
32+
33+
34+
class WiFi:
35+
"""Class representing the WiFi portion of the ESP32-S2.
36+
37+
:param status_led: The initialized object for status DotStar, NeoPixel, or RGB LED. Defaults
38+
to ``None``, to not use the status LED
39+
40+
"""
41+
42+
def __init__(self, *, status_led=None):
43+
if status_led:
44+
self.neopix = status_led
45+
else:
46+
self.neopix = None
47+
self.neo_status(0)
48+
self.requests = None
49+
self.pool = None
50+
self._connected = False
51+
52+
gc.collect()
53+
54+
def connect(self, ssid, password):
55+
"""
56+
Connect to the WiFi Network using the information provided
57+
58+
:param ssid: The WiFi name
59+
:param password: The WiFi password
60+
61+
"""
62+
wifi.radio.connect(ssid, password)
63+
self.pool = socketpool.SocketPool(wifi.radio)
64+
self.requests = adafruit_requests.Session(
65+
self.pool, ssl.create_default_context()
66+
)
67+
self._connected = True
68+
69+
def neo_status(self, value):
70+
"""The status DotStar.
71+
72+
:param value: The color to change the DotStar.
73+
74+
"""
75+
if self.neopix:
76+
self.neopix.fill(value)
77+
78+
@property
79+
def is_connected(self):
80+
"""
81+
Return whether we have already connected since reconnections are handled automatically.
82+
83+
"""
84+
return self._connected
85+
86+
@property
87+
def ip_address(self):
88+
"""
89+
Return the IP Version 4 Address
90+
91+
"""
92+
return wifi.radio.ipv4_address
93+
94+
@property
95+
def enabled(self):
96+
"""
97+
Return whether the WiFi Radio is enabled
98+
99+
"""
100+
return wifi.radio.enabled

0 commit comments

Comments
 (0)