From 5b9078d00d9e6cfee9d3723205b61fa4092679fa Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Wed, 21 Apr 2021 11:40:10 -0700 Subject: [PATCH 1/3] Added a couple different WiFi modules --- adafruit_portalbase/wifi_coprocessor.py | 111 ++++++++++++++++++++++++ adafruit_portalbase/wifi_esp32s2.py | 100 +++++++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100755 adafruit_portalbase/wifi_coprocessor.py create mode 100755 adafruit_portalbase/wifi_esp32s2.py diff --git a/adafruit_portalbase/wifi_coprocessor.py b/adafruit_portalbase/wifi_coprocessor.py new file mode 100755 index 0000000..070910d --- /dev/null +++ b/adafruit_portalbase/wifi_coprocessor.py @@ -0,0 +1,111 @@ +# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries +# SPDX-FileCopyrightText: Copyright (c) 2021 Melissa LeBlanc-Williams for Adafruit Industries +# +# SPDX-License-Identifier: MIT +""" +`adafruit_portalbase.wifi_coprocessor` +================================================================================ + +WiFi Helper module for the board using the WiFi CoProcessor. + +* Author(s): Melissa LeBlanc-Williams + +Implementation Notes +-------------------- + +**Software and Dependencies:** + +* Adafruit CircuitPython firmware for the supported boards: + https://github.com/adafruit/circuitpython/releases + +""" + +import gc +import board +import busio +from digitalio import DigitalInOut +from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager +import adafruit_esp32spi.adafruit_esp32spi_socket as socket +import adafruit_requests as requests + +__version__ = "0.0.0-auto.0" +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MatrixPortal.git" + + +class WiFi: + """Class representing the ESP. + + :param status_led: The initialized object for status DotStar, NeoPixel, or RGB LED. Defaults to ``None``, + to not use the status LED + :param esp: A passed ESP32 object, Can be used in cases where the ESP32 chip needs to be used + before calling the pyportal class. Defaults to ``None``. + :param busio.SPI external_spi: A previously declared spi object. Defaults to ``None``. + + """ + + def __init__(self, *, status_led=None, esp=None, external_spi=None): + + if status_led: + self.neopix = status_led + else: + self.neopix = None + self.neo_status(0) + self.requests = None + + if esp: # If there was a passed ESP Object + self.esp = esp + if external_spi: # If SPI Object Passed + spi = external_spi + else: # Else: Make ESP32 connection + spi = busio.SPI(board.SCK, board.MOSI, board.MISO) + else: + esp32_ready = DigitalInOut(board.ESP_BUSY) + esp32_gpio0 = DigitalInOut(board.ESP_GPIO0) + esp32_reset = DigitalInOut(board.ESP_RESET) + esp32_cs = DigitalInOut(board.ESP_CS) + spi = busio.SPI(board.SCK, board.MOSI, board.MISO) + + self.esp = adafruit_esp32spi.ESP_SPIcontrol( + spi, esp32_cs, esp32_ready, esp32_reset, esp32_gpio0 + ) + + requests.set_socket(socket, self.esp) + if self.esp.is_connected: + self.requests = requests + self._manager = None + + gc.collect() + + def connect(self, ssid, password): + """ + Connect to WiFi using the settings found in secrets.py + """ + self.esp.connect({"ssid": ssid, "password": password}) + self.requests = requests + + def neo_status(self, value): + """The status NeoPixel. + + :param value: The color to change the NeoPixel. + + """ + if self.neopix: + self.neopix.fill(value) + + def manager(self, secrets): + """Initialize the WiFi Manager if it hasn't been cached and return it""" + if self._manager is None: + self._manager = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager( + self.esp, secrets, None + ) + return self._manager + + @property + def is_connected(self): + """Return whether we are connected.""" + return self.esp.is_connected + + @property + def enabled(self): + """Not currently disablable on the ESP32 Coprocessor""" + return True diff --git a/adafruit_portalbase/wifi_esp32s2.py b/adafruit_portalbase/wifi_esp32s2.py new file mode 100755 index 0000000..cb175d0 --- /dev/null +++ b/adafruit_portalbase/wifi_esp32s2.py @@ -0,0 +1,100 @@ +# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries +# SPDX-FileCopyrightText: Copyright (c) 2021 Melissa LeBlanc-Williams for Adafruit Industries +# +# SPDX-License-Identifier: MIT +""" +`adafruit_portalbase.wifi_esp32s2` +================================================================================ + +WiFi Helper module for the ESP32-S2 based boards. + + +* Author(s): Melissa LeBlanc-Williams + +Implementation Notes +-------------------- + +**Software and Dependencies:** + +* Adafruit CircuitPython firmware for the supported boards: + https://github.com/adafruit/circuitpython/releases + +""" + +import gc +import ssl +import wifi +import socketpool +import adafruit_requests + +__version__ = "0.0.0-auto.0" +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PortalBase.git" + + +class WiFi: + """Class representing the WiFi portion of the ESP32-S2. + + :param status_led: The initialized object for status DotStar, NeoPixel, or RGB LED. Defaults to ``None``, + to not use the status LED + + """ + + def __init__(self, *, status_led=None): + if status_led: + self.neopix = status_led + else: + self.neopix = None + self.neo_status(0) + self.requests = None + self.pool = None + self._connected = False + + gc.collect() + + def connect(self, ssid, password): + """ + Connect to the WiFi Network using the information provided + + :param ssid: The WiFi name + :param password: The WiFi password + + """ + wifi.radio.connect(ssid, password) + self.pool = socketpool.SocketPool(wifi.radio) + self.requests = adafruit_requests.Session( + self.pool, ssl.create_default_context() + ) + self._connected = True + + def neo_status(self, value): + """The status DotStar. + + :param value: The color to change the DotStar. + + """ + if self.neopix: + self.neopix.fill(value) + + @property + def is_connected(self): + """ + Return whether we have already connected since reconnections are handled automatically. + + """ + return self._connected + + @property + def ip_address(self): + """ + Return the IP Version 4 Address + + """ + return wifi.radio.ipv4_address + + @property + def enabled(self): + """ + Return whether the WiFi Radio is enabled + + """ + return wifi.radio.enabled From 7e06390f3a2c96ff40c713b2101bb13deb5e1f6c Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Wed, 21 Apr 2021 11:41:28 -0700 Subject: [PATCH 2/3] Fix repo url --- adafruit_portalbase/wifi_coprocessor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_portalbase/wifi_coprocessor.py b/adafruit_portalbase/wifi_coprocessor.py index 070910d..1b59c95 100755 --- a/adafruit_portalbase/wifi_coprocessor.py +++ b/adafruit_portalbase/wifi_coprocessor.py @@ -29,7 +29,7 @@ import adafruit_requests as requests __version__ = "0.0.0-auto.0" -__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MatrixPortal.git" +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PortalBase.git" class WiFi: From c3d63a760c8d5a34faa55480299f8d4ae96c8927 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Wed, 21 Apr 2021 11:57:51 -0700 Subject: [PATCH 3/3] Fix line too long --- adafruit_portalbase/wifi_coprocessor.py | 4 ++-- adafruit_portalbase/wifi_esp32s2.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/adafruit_portalbase/wifi_coprocessor.py b/adafruit_portalbase/wifi_coprocessor.py index 1b59c95..0532118 100755 --- a/adafruit_portalbase/wifi_coprocessor.py +++ b/adafruit_portalbase/wifi_coprocessor.py @@ -35,8 +35,8 @@ class WiFi: """Class representing the ESP. - :param status_led: The initialized object for status DotStar, NeoPixel, or RGB LED. Defaults to ``None``, - to not use the status LED + :param status_led: The initialized object for status DotStar, NeoPixel, or RGB LED. Defaults + to ``None``, to not use the status LED :param esp: A passed ESP32 object, Can be used in cases where the ESP32 chip needs to be used before calling the pyportal class. Defaults to ``None``. :param busio.SPI external_spi: A previously declared spi object. Defaults to ``None``. diff --git a/adafruit_portalbase/wifi_esp32s2.py b/adafruit_portalbase/wifi_esp32s2.py index cb175d0..ca0a153 100755 --- a/adafruit_portalbase/wifi_esp32s2.py +++ b/adafruit_portalbase/wifi_esp32s2.py @@ -34,8 +34,8 @@ class WiFi: """Class representing the WiFi portion of the ESP32-S2. - :param status_led: The initialized object for status DotStar, NeoPixel, or RGB LED. Defaults to ``None``, - to not use the status LED + :param status_led: The initialized object for status DotStar, NeoPixel, or RGB LED. Defaults + to ``None``, to not use the status LED """