Skip to content

Commit 5dfafb6

Browse files
committed
WiFiManager updates
1 parent 71a07cc commit 5dfafb6

12 files changed

+134
-143
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import struct
3030
import time
31+
import warnings
3132
from micropython import const
3233
from adafruit_bus_device.spi_device import SPIDevice
3334
from digitalio import Direction
@@ -663,6 +664,10 @@ def connect(self, ssid, password=None, timeout=10):
663664
This upward compatbility will be removed in a future release.
664665
"""
665666
if isinstance(ssid, dict): # secrets
667+
warnings.warn(
668+
"The passing in of `secrets`, is deprecated. Use connect with a `ssid` and "
669+
"`password` instead and fetch values from settings.toml with `os.getenv()`."
670+
)
666671
ssid, password = ssid["ssid"], ssid.get("password")
667672
self.connect_AP(ssid, password, timeout_s=timeout)
668673

adafruit_esp32spi/adafruit_esp32spi_wifimanager.py

+74-14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
# pylint: disable=no-name-in-module
1515

16+
import warnings
1617
from time import sleep
1718
from micropython import const
1819
import adafruit_connection_manager
@@ -21,7 +22,7 @@
2122

2223

2324
# pylint: disable=too-many-instance-attributes
24-
class ESPSPI_WiFiManager:
25+
class WiFiManager:
2526
"""
2627
A class to help manage the Wifi connection
2728
"""
@@ -33,17 +34,22 @@ class ESPSPI_WiFiManager:
3334
def __init__(
3435
self,
3536
esp,
36-
secrets,
37+
ssid,
38+
password=None,
39+
*,
40+
enterprise_ident=None,
41+
enterprise_user=None,
3742
status_pixel=None,
3843
attempts=2,
3944
connection_type=NORMAL,
4045
debug=False,
4146
):
4247
"""
4348
:param ESP_SPIcontrol esp: The ESP object we are using
44-
:param dict secrets: The WiFi and Adafruit IO secrets dict (See examples)
45-
The use of secrets.py to populate the secrets dict is depreciated
46-
in favor of using settings.toml.
49+
:param str ssid: the SSID of the created Access Point. Must be less than 32 chars.
50+
:param str password: the password of the created Access Point. Must be 8-63 chars.
51+
:param str enterprise_ident: the ident when connecting to an enterprise Access Point.
52+
:param str enterprise_user: the user when connecting to an enterprise Access Point.
4753
:param status_pixel: (Optional) The pixel device - A NeoPixel, DotStar,
4854
or RGB LED (default=None). The status LED, if given, turns red when
4955
attempting to connect to a Wi-Fi network or create an access point,
@@ -57,8 +63,8 @@ def __init__(
5763
# Read the settings
5864
self.esp = esp
5965
self.debug = debug
60-
self.ssid = secrets["ssid"]
61-
self.password = secrets.get("password", None)
66+
self.ssid = ssid
67+
self.password = password
6268
self.attempts = attempts
6369
self._connection_type = connection_type
6470
self.statuspix = status_pixel
@@ -70,11 +76,11 @@ def __init__(
7076
ssl_context = adafruit_connection_manager.get_radio_ssl_context(self.esp)
7177
self._requests = adafruit_requests.Session(pool, ssl_context)
7278

73-
# Check for WPA2 Enterprise keys in the secrets dictionary and load them if they exist
74-
self.ent_ssid = secrets.get("ent_ssid", secrets["ssid"])
75-
self.ent_ident = secrets.get("ent_ident", "")
76-
self.ent_user = secrets.get("ent_user")
77-
self.ent_password = secrets.get("ent_password")
79+
# Check for WPA2 Enterprise values
80+
self.ent_ssid = ssid
81+
self.ent_ident = enterprise_ident
82+
self.ent_user = enterprise_user
83+
self.ent_password = password
7884

7985
# pylint: enable=too-many-arguments
8086

@@ -97,9 +103,9 @@ def connect(self):
97103
print("MAC addr:", [hex(i) for i in self.esp.MAC_address])
98104
for access_pt in self.esp.scan_networks():
99105
print("\t%s\t\tRSSI: %d" % (access_pt.ssid, access_pt.rssi))
100-
if self._connection_type == ESPSPI_WiFiManager.NORMAL:
106+
if self._connection_type == WiFiManager.NORMAL:
101107
self.connect_normal()
102-
elif self._connection_type == ESPSPI_WiFiManager.ENTERPRISE:
108+
elif self._connection_type == WiFiManager.ENTERPRISE:
103109
self.connect_enterprise()
104110
else:
105111
raise TypeError("Invalid WiFi connection type specified")
@@ -347,3 +353,57 @@ def signal_strength(self):
347353
if not self.esp.is_connected:
348354
self.connect()
349355
return self.esp.ap_info.rssi
356+
357+
358+
# pylint: disable=too-many-instance-attributes
359+
class ESPSPI_WiFiManager(WiFiManager):
360+
"""
361+
A legacy class to help manage the Wifi connection. Please update to using WiFiManager
362+
"""
363+
364+
# pylint: disable=too-many-arguments
365+
def __init__(
366+
self,
367+
esp,
368+
secrets,
369+
status_pixel=None,
370+
attempts=2,
371+
connection_type=WiFiManager.NORMAL,
372+
debug=False,
373+
):
374+
"""
375+
:param ESP_SPIcontrol esp: The ESP object we are using
376+
:param dict secrets: The WiFi secrets dict
377+
The use of secrets.py to populate the secrets dict is depreciated
378+
in favor of using settings.toml.
379+
:param status_pixel: (Optional) The pixel device - A NeoPixel, DotStar,
380+
or RGB LED (default=None). The status LED, if given, turns red when
381+
attempting to connect to a Wi-Fi network or create an access point,
382+
turning green upon success. Additionally, if given, it will turn blue
383+
when attempting an HTTP method or returning IP address, turning off
384+
upon success.
385+
:type status_pixel: NeoPixel, DotStar, or RGB LED
386+
:param int attempts: (Optional) Failed attempts before resetting the ESP32 (default=2)
387+
:param const connection_type: (Optional) Type of WiFi connection: NORMAL or ENTERPRISE
388+
"""
389+
390+
warnings.warn(
391+
"ESP32WiFiManager, which uses `secrets`, is deprecated. Use WifiManager instead and "
392+
"fetch values from settings.toml with `os.getenv()`."
393+
)
394+
395+
ssid = secrets.get("ssid")
396+
password = secrets.get("secrets", None)
397+
enterprise_ident = secrets.get("ent_ident", "")
398+
enterprise_user = secrets.get("ent_user")
399+
super().__init__(
400+
esp=esp,
401+
ssid=ssid,
402+
password=password,
403+
enterprise_ident=enterprise_ident,
404+
enterprise_user=enterprise_user,
405+
status_pixel=status_pixel,
406+
attempts=attempts,
407+
connection_type=connection_type,
408+
debug=debug,
409+
)

examples/esp32spi_aio_post.py

+9-19
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,18 @@
88
from digitalio import DigitalInOut
99
import neopixel
1010
from adafruit_esp32spi import adafruit_esp32spi
11-
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
11+
from adafruit_esp32spi.adafruit_esp32spi_wifimanager import WiFiManager
1212

1313
print("ESP32 SPI webclient test")
1414

1515
# Get wifi details and more from a settings.toml file
1616
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
17-
# CIRCUITPY_AIO_USERNAME, CIRCUITPY_AIO_KEY
18-
secrets = {}
19-
for token in ["ssid", "password"]:
20-
if getenv("CIRCUITPY_WIFI_" + token.upper()):
21-
secrets[token] = getenv("CIRCUITPY_WIFI_" + token.upper())
22-
for token in ["aio_username", "aio_key"]:
23-
if getenv("CIRCUITPY_" + token.upper()):
24-
secrets[token] = getenv("CIRCUITPY_" + token.upper())
17+
# ADAFRUIT_AIO_USERNAME, ADAFRUIT_AIO_KEY
18+
ssid = getenv("CIRCUITPY_WIFI_SSID")
19+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
2520

26-
if not secrets:
27-
try:
28-
# Fallback on secrets.py until depreciation is over and option is removed
29-
from secrets import secrets
30-
except ImportError:
31-
print("WiFi secrets are kept in settings.toml, please add them there!")
32-
raise
21+
aio_username = getenv("ADAFRUIT_AIO_USERNAME")
22+
aio_key = getenv("ADAFRUIT_AIO_KEY")
3323

3424
# If you are using a board with pre-defined ESP32 Pins:
3525
esp32_cs = DigitalInOut(board.ESP_CS)
@@ -59,7 +49,7 @@
5949
# BLUE_LED = PWMOut.PWMOut(esp, 25)
6050
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
6151

62-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
52+
wifi = WiFiManager(esp, ssid, password, status_light=status_light)
6353

6454
counter = 0
6555

@@ -71,12 +61,12 @@
7161
payload = {"value": data}
7262
response = wifi.post(
7363
"https://io.adafruit.com/api/v2/"
74-
+ secrets["aio_username"]
64+
+ aio_username
7565
+ "/feeds/"
7666
+ feed
7767
+ "/data",
7868
json=payload,
79-
headers={"X-AIO-KEY": secrets["aio_key"]},
69+
headers={"X-AIO-KEY": aio_key},
8070
)
8171
print(response.json())
8272
response.close()

examples/esp32spi_cheerlights.py

+4-13
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,12 @@
1111
import adafruit_fancyled.adafruit_fancyled as fancy
1212

1313
from adafruit_esp32spi import adafruit_esp32spi
14-
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
14+
from adafruit_esp32spi.adafruit_esp32spi_wifimanager import WiFiManager
1515

1616
# Get wifi details and more from a settings.toml file
1717
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
18-
secrets = {
19-
"ssid": getenv("CIRCUITPY_WIFI_SSID"),
20-
"password": getenv("CIRCUITPY_WIFI_PASSWORD"),
21-
}
22-
if secrets == {"ssid": None, "password": None}:
23-
try:
24-
# Fallback on secrets.py until depreciation is over and option is removed
25-
from secrets import secrets
26-
except ImportError:
27-
print("WiFi secrets are kept in settings.toml, please add them there!")
28-
raise
18+
ssid = getenv("CIRCUITPY_WIFI_SSID")
19+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
2920

3021
print("ESP32 SPI webclient test")
3122

@@ -59,7 +50,7 @@
5950
# GREEN_LED = PWMOut.PWMOut(esp, 27)
6051
# BLUE_LED = PWMOut.PWMOut(esp, 25)
6152
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
62-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
53+
wifi = WiFiManager(esp, ssid, password, status_light=status_light)
6354

6455
# neopixels
6556
pixels = neopixel.NeoPixel(board.A1, 16, brightness=0.3)

examples/esp32spi_ipconfig.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,8 @@
1111

1212
# Get wifi details and more from a settings.toml file
1313
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
14-
secrets = {
15-
"ssid": getenv("CIRCUITPY_WIFI_SSID"),
16-
"password": getenv("CIRCUITPY_WIFI_PASSWORD"),
17-
}
18-
if secrets == {"ssid": None, "password": None}:
19-
try:
20-
# Fallback on secrets.py until depreciation is over and option is removed
21-
from secrets import secrets
22-
except ImportError:
23-
print("WiFi secrets are kept in settings.toml, please add them there!")
24-
raise
14+
ssid = getenv("CIRCUITPY_WIFI_SSID")
15+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
2516

2617
HOSTNAME = "esp32-spi-hostname-test"
2718

@@ -66,7 +57,7 @@
6657
print("Connecting to AP...")
6758
while not esp.is_connected:
6859
try:
69-
esp.connect_AP(secrets["ssid"], secrets["password"])
60+
esp.connect_AP(ssid, password)
7061
except OSError as e:
7162
print("could not connect to AP, retrying: ", e)
7263
continue

examples/esp32spi_localtime.py

+4-13
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,12 @@
99
import neopixel
1010
import rtc
1111
from adafruit_esp32spi import adafruit_esp32spi
12-
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
12+
from adafruit_esp32spi.adafruit_esp32spi_wifimanager import WiFiManager
1313

1414
# Get wifi details and more from a settings.toml file
1515
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
16-
secrets = {
17-
"ssid": getenv("CIRCUITPY_WIFI_SSID"),
18-
"password": getenv("CIRCUITPY_WIFI_PASSWORD"),
19-
}
20-
if secrets == {"ssid": None, "password": None}:
21-
try:
22-
# Fallback on secrets.py until depreciation is over and option is removed
23-
from secrets import secrets
24-
except ImportError:
25-
print("WiFi secrets are kept in settings.toml, please add them there!")
26-
raise
16+
ssid = getenv("CIRCUITPY_WIFI_SSID")
17+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
2718

2819
print("ESP32 local time")
2920

@@ -58,7 +49,7 @@
5849
# BLUE_LED = PWMOut.PWMOut(esp, 25)
5950
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
6051

61-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
52+
wifi = WiFiManager(esp, ssid, password, status_light=status_light)
6253

6354
the_rtc = rtc.RTC()
6455

examples/esp32spi_settings.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
CIRCUITPY_WIFI_SSID="yourssid"
1111
CIRCUITPY_WIFI_PASSWORD="yourpassword"
1212
CIRCUITPY_TIMEZONE="America/New_York"
13-
CIRCUITPY_AIO_USERNAME="youraiousername"
14-
CIRCUITPY_AIO_KEY="youraiokey"
13+
ADAFRUIT_AIO_USERNAME="youraiousername"
14+
ADAFRUIT_AIO_KEY="youraiokey"

examples/esp32spi_simpletest.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,8 @@
1111

1212
# Get wifi details and more from a settings.toml file
1313
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
14-
secrets = {
15-
"ssid": getenv("CIRCUITPY_WIFI_SSID"),
16-
"password": getenv("CIRCUITPY_WIFI_PASSWORD"),
17-
}
18-
if secrets == {"ssid": None, "password": None}:
19-
try:
20-
# Fallback on secrets.py until depreciation is over and option is removed
21-
from secrets import secrets
22-
except ImportError:
23-
print("WiFi secrets are kept in settings.toml, please add them there!")
24-
raise
14+
ssid = getenv("CIRCUITPY_WIFI_SSID")
15+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
2516

2617
print("ESP32 SPI webclient test")
2718

@@ -72,7 +63,7 @@
7263
print("Connecting to AP...")
7364
while not esp.is_connected:
7465
try:
75-
esp.connect_AP(secrets["ssid"], secrets["password"])
66+
esp.connect_AP(ssid, password)
7667
except OSError as e:
7768
print("could not connect to AP, retrying: ", e)
7869
continue

examples/esp32spi_simpletest_rp2040.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,8 @@
1111

1212
# Get wifi details and more from a settings.toml file
1313
# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD
14-
secrets = {
15-
"ssid": getenv("CIRCUITPY_WIFI_SSID"),
16-
"password": getenv("CIRCUITPY_WIFI_PASSWORD"),
17-
}
18-
if secrets == {"ssid": None, "password": None}:
19-
try:
20-
# Fallback on secrets.py until depreciation is over and option is removed
21-
from secrets import secrets
22-
except ImportError:
23-
print("WiFi secrets are kept in settings.toml, please add them there!")
24-
raise
14+
ssid = getenv("CIRCUITPY_WIFI_SSID")
15+
password = getenv("CIRCUITPY_WIFI_PASSWORD")
2516

2617
print("Raspberry Pi RP2040 - ESP32 SPI webclient test")
2718

@@ -51,7 +42,7 @@
5142
print("Connecting to AP...")
5243
while not esp.is_connected:
5344
try:
54-
esp.connect_AP(secrets["ssid"], secrets["password"])
45+
esp.connect_AP(ssid, password)
5546
except OSError as e:
5647
print("could not connect to AP, retrying: ", e)
5748
continue

0 commit comments

Comments
 (0)