Skip to content

Commit

Permalink
Merge pull request #33 from makermelissa/main
Browse files Browse the repository at this point in the history
Added a couple different WiFi modules
  • Loading branch information
makermelissa authored Apr 21, 2021
2 parents b09f7b3 + c3d63a7 commit 556f33e
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 0 deletions.
111 changes: 111 additions & 0 deletions adafruit_portalbase/wifi_coprocessor.py
Original file line number Diff line number Diff line change
@@ -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_PortalBase.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
100 changes: 100 additions & 0 deletions adafruit_portalbase/wifi_esp32s2.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 556f33e

Please sign in to comment.