diff --git a/adafruit_esp32spi/adafruit_esp32spi.py b/adafruit_esp32spi/adafruit_esp32spi.py index 346593a..0e61109 100644 --- a/adafruit_esp32spi/adafruit_esp32spi.py +++ b/adafruit_esp32spi/adafruit_esp32spi.py @@ -28,6 +28,7 @@ import struct import time +import warnings from micropython import const from adafruit_bus_device.spi_device import SPIDevice from digitalio import Direction @@ -663,6 +664,10 @@ def connect(self, ssid, password=None, timeout=10): This upward compatbility will be removed in a future release. """ if isinstance(ssid, dict): # secrets + warnings.warn( + "The passing in of `secrets`, is deprecated. Use connect with a `ssid` and " + "`password` instead and fetch values from settings.toml with `os.getenv()`." + ) ssid, password = ssid["ssid"], ssid.get("password") self.connect_AP(ssid, password, timeout_s=timeout) diff --git a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py index 68cd058..a1ebbc8 100755 --- a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py +++ b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py @@ -13,6 +13,7 @@ # pylint: disable=no-name-in-module +import warnings from time import sleep from micropython import const import adafruit_connection_manager @@ -21,7 +22,7 @@ # pylint: disable=too-many-instance-attributes -class ESPSPI_WiFiManager: +class WiFiManager: """ A class to help manage the Wifi connection """ @@ -33,7 +34,11 @@ class ESPSPI_WiFiManager: def __init__( self, esp, - secrets, + ssid, + password=None, + *, + enterprise_ident=None, + enterprise_user=None, status_pixel=None, attempts=2, connection_type=NORMAL, @@ -41,9 +46,10 @@ def __init__( ): """ :param ESP_SPIcontrol esp: The ESP object we are using - :param dict secrets: The WiFi and Adafruit IO secrets dict (See examples) - The use of secrets.py to populate the secrets dict is depreciated - in favor of using settings.toml. + :param str ssid: the SSID of the created Access Point. Must be less than 32 chars. + :param str password: the password of the created Access Point. Must be 8-63 chars. + :param str enterprise_ident: the ident when connecting to an enterprise Access Point. + :param str enterprise_user: the user when connecting to an enterprise Access Point. :param status_pixel: (Optional) The pixel device - A NeoPixel, DotStar, or RGB LED (default=None). The status LED, if given, turns red when attempting to connect to a Wi-Fi network or create an access point, @@ -57,8 +63,8 @@ def __init__( # Read the settings self.esp = esp self.debug = debug - self.ssid = secrets["ssid"] - self.password = secrets.get("password", None) + self.ssid = ssid + self.password = password self.attempts = attempts self._connection_type = connection_type self.statuspix = status_pixel @@ -70,11 +76,11 @@ def __init__( ssl_context = adafruit_connection_manager.get_radio_ssl_context(self.esp) self._requests = adafruit_requests.Session(pool, ssl_context) - # Check for WPA2 Enterprise keys in the secrets dictionary and load them if they exist - self.ent_ssid = secrets.get("ent_ssid", secrets["ssid"]) - self.ent_ident = secrets.get("ent_ident", "") - self.ent_user = secrets.get("ent_user") - self.ent_password = secrets.get("ent_password") + # Check for WPA2 Enterprise values + self.ent_ssid = ssid + self.ent_ident = enterprise_ident + self.ent_user = enterprise_user + self.ent_password = password # pylint: enable=too-many-arguments @@ -97,9 +103,9 @@ def connect(self): print("MAC addr:", [hex(i) for i in self.esp.MAC_address]) for access_pt in self.esp.scan_networks(): print("\t%s\t\tRSSI: %d" % (access_pt.ssid, access_pt.rssi)) - if self._connection_type == ESPSPI_WiFiManager.NORMAL: + if self._connection_type == WiFiManager.NORMAL: self.connect_normal() - elif self._connection_type == ESPSPI_WiFiManager.ENTERPRISE: + elif self._connection_type == WiFiManager.ENTERPRISE: self.connect_enterprise() else: raise TypeError("Invalid WiFi connection type specified") @@ -347,3 +353,60 @@ def signal_strength(self): if not self.esp.is_connected: self.connect() return self.esp.ap_info.rssi + + +# pylint: disable=too-many-instance-attributes +class ESPSPI_WiFiManager(WiFiManager): + """ + A legacy class to help manage the Wifi connection. Please update to using WiFiManager + """ + + NORMAL = const(1) + ENTERPRISE = const(2) + + # pylint: disable=too-many-arguments + def __init__( + self, + esp, + secrets, + status_pixel=None, + attempts=2, + connection_type=NORMAL, + debug=False, + ): + """ + :param ESP_SPIcontrol esp: The ESP object we are using + :param dict secrets: The WiFi secrets dict + The use of secrets.py to populate the secrets dict is depreciated + in favor of using settings.toml. + :param status_pixel: (Optional) The pixel device - A NeoPixel, DotStar, + or RGB LED (default=None). The status LED, if given, turns red when + attempting to connect to a Wi-Fi network or create an access point, + turning green upon success. Additionally, if given, it will turn blue + when attempting an HTTP method or returning IP address, turning off + upon success. + :type status_pixel: NeoPixel, DotStar, or RGB LED + :param int attempts: (Optional) Failed attempts before resetting the ESP32 (default=2) + :param const connection_type: (Optional) Type of WiFi connection: NORMAL or ENTERPRISE + """ + + warnings.warn( + "ESP32WiFiManager, which uses `secrets`, is deprecated. Use WifiManager instead and " + "fetch values from settings.toml with `os.getenv()`." + ) + + ssid = secrets.get("ssid") + password = secrets.get("secrets", None) + enterprise_ident = secrets.get("ent_ident", "") + enterprise_user = secrets.get("ent_user") + super().__init__( + esp=esp, + ssid=ssid, + password=password, + enterprise_ident=enterprise_ident, + enterprise_user=enterprise_user, + status_pixel=status_pixel, + attempts=attempts, + connection_type=connection_type, + debug=debug, + ) diff --git a/examples/esp32spi_aio_post.py b/examples/esp32spi_aio_post.py index c4b5f7f..e916fcd 100644 --- a/examples/esp32spi_aio_post.py +++ b/examples/esp32spi_aio_post.py @@ -8,28 +8,18 @@ from digitalio import DigitalInOut import neopixel from adafruit_esp32spi import adafruit_esp32spi -from adafruit_esp32spi import adafruit_esp32spi_wifimanager +from adafruit_esp32spi.adafruit_esp32spi_wifimanager import WiFiManager print("ESP32 SPI webclient test") # Get wifi details and more from a settings.toml file # tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD # CIRCUITPY_AIO_USERNAME, CIRCUITPY_AIO_KEY -secrets = {} -for token in ["ssid", "password"]: - if getenv("CIRCUITPY_WIFI_" + token.upper()): - secrets[token] = getenv("CIRCUITPY_WIFI_" + token.upper()) -for token in ["aio_username", "aio_key"]: - if getenv("CIRCUITPY_" + token.upper()): - secrets[token] = getenv("CIRCUITPY_" + token.upper()) +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") -if not secrets: - try: - # Fallback on secrets.py until depreciation is over and option is removed - from secrets import secrets - except ImportError: - print("WiFi secrets are kept in settings.toml, please add them there!") - raise +aio_username = getenv("CIRCUITPY_AIO_USERNAME") +aio_key = getenv("CIRCUITPY_AIO_KEY") # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) @@ -59,7 +49,7 @@ # BLUE_LED = PWMOut.PWMOut(esp, 25) # status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +wifi = WiFiManager(esp, ssid, password, status_light=status_light) counter = 0 @@ -71,12 +61,12 @@ payload = {"value": data} response = wifi.post( "https://io.adafruit.com/api/v2/" - + secrets["aio_username"] + + aio_username + "/feeds/" + feed + "/data", json=payload, - headers={"X-AIO-KEY": secrets["aio_key"]}, + headers={"X-AIO-KEY": aio_key}, ) print(response.json()) response.close() diff --git a/examples/esp32spi_cheerlights.py b/examples/esp32spi_cheerlights.py index 9bac915..d1e04fa 100644 --- a/examples/esp32spi_cheerlights.py +++ b/examples/esp32spi_cheerlights.py @@ -11,21 +11,12 @@ import adafruit_fancyled.adafruit_fancyled as fancy from adafruit_esp32spi import adafruit_esp32spi -from adafruit_esp32spi import adafruit_esp32spi_wifimanager +from adafruit_esp32spi.adafruit_esp32spi_wifimanager import WiFiManager # Get wifi details and more from a settings.toml file # tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD -secrets = { - "ssid": getenv("CIRCUITPY_WIFI_SSID"), - "password": getenv("CIRCUITPY_WIFI_PASSWORD"), -} -if secrets == {"ssid": None, "password": None}: - try: - # Fallback on secrets.py until depreciation is over and option is removed - from secrets import secrets - except ImportError: - print("WiFi secrets are kept in settings.toml, please add them there!") - raise +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") print("ESP32 SPI webclient test") @@ -59,7 +50,7 @@ # GREEN_LED = PWMOut.PWMOut(esp, 27) # BLUE_LED = PWMOut.PWMOut(esp, 25) # status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +wifi = WiFiManager(esp, ssid, password, status_light=status_light) # neopixels pixels = neopixel.NeoPixel(board.A1, 16, brightness=0.3) diff --git a/examples/esp32spi_ipconfig.py b/examples/esp32spi_ipconfig.py index 7e98df6..dec0f3a 100644 --- a/examples/esp32spi_ipconfig.py +++ b/examples/esp32spi_ipconfig.py @@ -11,17 +11,8 @@ # Get wifi details and more from a settings.toml file # tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD -secrets = { - "ssid": getenv("CIRCUITPY_WIFI_SSID"), - "password": getenv("CIRCUITPY_WIFI_PASSWORD"), -} -if secrets == {"ssid": None, "password": None}: - try: - # Fallback on secrets.py until depreciation is over and option is removed - from secrets import secrets - except ImportError: - print("WiFi secrets are kept in settings.toml, please add them there!") - raise +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") HOSTNAME = "esp32-spi-hostname-test" @@ -66,7 +57,7 @@ print("Connecting to AP...") while not esp.is_connected: try: - esp.connect_AP(secrets["ssid"], secrets["password"]) + esp.connect_AP(ssid, password) except OSError as e: print("could not connect to AP, retrying: ", e) continue diff --git a/examples/esp32spi_localtime.py b/examples/esp32spi_localtime.py index b6ee671..0a19367 100644 --- a/examples/esp32spi_localtime.py +++ b/examples/esp32spi_localtime.py @@ -9,21 +9,12 @@ import neopixel import rtc from adafruit_esp32spi import adafruit_esp32spi -from adafruit_esp32spi import adafruit_esp32spi_wifimanager +from adafruit_esp32spi.adafruit_esp32spi_wifimanager import WiFiManager # Get wifi details and more from a settings.toml file # tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD -secrets = { - "ssid": getenv("CIRCUITPY_WIFI_SSID"), - "password": getenv("CIRCUITPY_WIFI_PASSWORD"), -} -if secrets == {"ssid": None, "password": None}: - try: - # Fallback on secrets.py until depreciation is over and option is removed - from secrets import secrets - except ImportError: - print("WiFi secrets are kept in settings.toml, please add them there!") - raise +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") print("ESP32 local time") @@ -58,7 +49,7 @@ # BLUE_LED = PWMOut.PWMOut(esp, 25) # status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) +wifi = WiFiManager(esp, ssid, password, status_light=status_light) the_rtc = rtc.RTC() diff --git a/examples/esp32spi_simpletest.py b/examples/esp32spi_simpletest.py index c194648..d9a8edf 100644 --- a/examples/esp32spi_simpletest.py +++ b/examples/esp32spi_simpletest.py @@ -11,17 +11,8 @@ # Get wifi details and more from a settings.toml file # tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD -secrets = { - "ssid": getenv("CIRCUITPY_WIFI_SSID"), - "password": getenv("CIRCUITPY_WIFI_PASSWORD"), -} -if secrets == {"ssid": None, "password": None}: - try: - # Fallback on secrets.py until depreciation is over and option is removed - from secrets import secrets - except ImportError: - print("WiFi secrets are kept in settings.toml, please add them there!") - raise +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") print("ESP32 SPI webclient test") @@ -72,7 +63,7 @@ print("Connecting to AP...") while not esp.is_connected: try: - esp.connect_AP(secrets["ssid"], secrets["password"]) + esp.connect_AP(ssid, password) except OSError as e: print("could not connect to AP, retrying: ", e) continue diff --git a/examples/esp32spi_simpletest_rp2040.py b/examples/esp32spi_simpletest_rp2040.py index eb5b8dd..1ee3fd6 100644 --- a/examples/esp32spi_simpletest_rp2040.py +++ b/examples/esp32spi_simpletest_rp2040.py @@ -11,17 +11,8 @@ # Get wifi details and more from a settings.toml file # tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD -secrets = { - "ssid": getenv("CIRCUITPY_WIFI_SSID"), - "password": getenv("CIRCUITPY_WIFI_PASSWORD"), -} -if secrets == {"ssid": None, "password": None}: - try: - # Fallback on secrets.py until depreciation is over and option is removed - from secrets import secrets - except ImportError: - print("WiFi secrets are kept in settings.toml, please add them there!") - raise +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") print("Raspberry Pi RP2040 - ESP32 SPI webclient test") @@ -51,7 +42,7 @@ print("Connecting to AP...") while not esp.is_connected: try: - esp.connect_AP(secrets["ssid"], secrets["password"]) + esp.connect_AP(ssid, password) except OSError as e: print("could not connect to AP, retrying: ", e) continue diff --git a/examples/esp32spi_tcp_client.py b/examples/esp32spi_tcp_client.py index 2ec5d2a..275c1ce 100644 --- a/examples/esp32spi_tcp_client.py +++ b/examples/esp32spi_tcp_client.py @@ -10,17 +10,8 @@ # Get wifi details and more from a settings.toml file # tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD -secrets = { - "ssid": getenv("CIRCUITPY_WIFI_SSID"), - "password": getenv("CIRCUITPY_WIFI_PASSWORD"), -} -if secrets == {"ssid": None, "password": None}: - try: - # Fallback on secrets.py until depreciation is over and option is removed - from secrets import secrets # pylint: disable=no-name-in-module - except ImportError: - print("WiFi secrets are kept in settings.toml, please add them there!") - raise +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") TIMEOUT = 5 # edit host and port to match server @@ -42,7 +33,7 @@ esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) # connect to wifi AP -esp.connect(secrets) +esp.connect(ssid, password) # test for connectivity to server print("Server ping:", esp.ping(HOST), "ms") diff --git a/examples/esp32spi_udp_client.py b/examples/esp32spi_udp_client.py index 3dace7b..2321c03 100644 --- a/examples/esp32spi_udp_client.py +++ b/examples/esp32spi_udp_client.py @@ -12,17 +12,8 @@ # Get wifi details and more from a settings.toml file # tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD -secrets = { - "ssid": getenv("CIRCUITPY_WIFI_SSID"), - "password": getenv("CIRCUITPY_WIFI_PASSWORD"), -} -if secrets == {"ssid": None, "password": None}: - try: - # Fallback on secrets.py until depreciation is over and option is removed - from secrets import secrets # pylint: disable=no-name-in-module - except ImportError: - print("WiFi secrets are kept in settings.toml, please add them there!") - raise +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") TIMEOUT = 5 # edit host and port to match server @@ -45,7 +36,7 @@ esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) # connect to wifi AP -esp.connect(secrets) +esp.connect(ssid, password) # test for connectivity to server print("Server ping:", esp.ping(HOST), "ms") diff --git a/examples/esp32spi_wpa2ent_aio_post.py b/examples/esp32spi_wpa2ent_aio_post.py index 197f279..2190a9e 100644 --- a/examples/esp32spi_wpa2ent_aio_post.py +++ b/examples/esp32spi_wpa2ent_aio_post.py @@ -8,28 +8,21 @@ from digitalio import DigitalInOut import neopixel from adafruit_esp32spi import adafruit_esp32spi -from adafruit_esp32spi.adafruit_esp32spi_wifimanager import ESPSPI_WiFiManager +from adafruit_esp32spi.adafruit_esp32spi_wifimanager import WiFiManager print("ESP32 SPI WPA2 Enterprise webclient test") # Get wifi details and more from a settings.toml file # tokens used by this Demo: CIRCUITPY_AIO_USERNAME, CIRCUITPY_AIO_KEY, -# CIRCUITPY_WIFI_ENT_SSID, CIRCUITPY_WIFI_ENT_PASSWORD, +# CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD, # CIRCUITPY_WIFI_ENT_USER, CIRCUITPY_WIFI_ENT_IDENT -secrets = {} -for token in ["ssid", "password", "user", "ident"]: - if getenv("CIRCUITPY_WIFI_ENT_" + token.upper()): - secrets[token] = getenv("CIRCUITPY_WIFI_" + token.upper()) -for token in ["aio_username", "aio_key"]: - if getenv("CIRCUITPY_" + token.upper()): - secrets[token] = getenv("CIRCUITPY_" + token.upper()) -if not secrets: - try: - # Fallback on secrets.py until depreciation is over and option is removed - from secrets import secrets # pylint: disable=no-name-in-module - except ImportError: - print("WiFi secrets are kept in settings.toml, please add them there!") - raise +ssid = getenv("CIRCUITPY_WIFI_SSID") +password = getenv("CIRCUITPY_WIFI_PASSWORD") +enterprise_ident = getenv("CIRCUITPY_WIFI_ENT_IDENT") +enterprise_user = getenv("CIRCUITPY_WIFI_ENT_USER") + +aio_username = getenv("CIRCUITPY_AIO_USERNAME") +aio_key = getenv("CIRCUITPY_AIO_KEY") # ESP32 setup # If your board does define the three pins listed below, @@ -62,8 +55,14 @@ # BLUE_LED = PWMOut.PWMOut(esp, 25) # status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) -wifi = ESPSPI_WiFiManager( - esp, secrets, status_light, connection_type=ESPSPI_WiFiManager.ENTERPRISE +wifi = WiFiManager( + esp, + ssid, + password, + enterprise_ident=enterprise_ident, + enterprise_user=enterprise_user, + status_light=status_light, + connection_type=WiFiManager.ENTERPRISE, ) counter = 0 @@ -76,12 +75,12 @@ payload = {"value": data} response = wifi.post( "https://io.adafruit.com/api/v2/" - + secrets["aio_username"] + + aio_username + "/feeds/" + feed + "/data", json=payload, - headers={"X-AIO-KEY": secrets["aio_key"]}, + headers={"X-AIO-KEY": aio_key}, ) print(response.json()) response.close()