|
| 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 |
0 commit comments