Skip to content

Commit 4d0a107

Browse files
authored
Merge pull request #216 from justmobilize/wifi-manager-update
WiFiManager updates
2 parents 71a07cc + 45705df commit 4d0a107

12 files changed

+144
-156
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 6 additions & 1 deletion
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
@@ -660,9 +661,13 @@ def connect(self, ssid, password=None, timeout=10):
660661
**Deprecated functionality:** If the first argument (``ssid``) is a ``dict``,
661662
assume it is a dictionary with entries for keys ``"ssid"`` and, optionally, ``"password"``.
662663
This mimics the previous signature for ``connect()``.
663-
This upward compatbility will be removed in a future release.
664+
This upward compatibility 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 `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

Lines changed: 71 additions & 14 deletions
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,23 @@ 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 access point. Must be less than 32 chars.
50+
:param str password: the password for the access point. Must be 8-63 chars.
51+
:param str enterprise_ident: the ident to use when connecting to an enterprise access point.
52+
:param str enterprise_user: the username to use when connecting to an enterprise access
53+
point.
4754
:param status_pixel: (Optional) The pixel device - A NeoPixel, DotStar,
4855
or RGB LED (default=None). The status LED, if given, turns red when
4956
attempting to connect to a Wi-Fi network or create an access point,
@@ -57,8 +64,8 @@ def __init__(
5764
# Read the settings
5865
self.esp = esp
5966
self.debug = debug
60-
self.ssid = secrets["ssid"]
61-
self.password = secrets.get("password", None)
67+
self.ssid = ssid
68+
self.password = password
6269
self.attempts = attempts
6370
self._connection_type = connection_type
6471
self.statuspix = status_pixel
@@ -70,11 +77,11 @@ def __init__(
7077
ssl_context = adafruit_connection_manager.get_radio_ssl_context(self.esp)
7178
self._requests = adafruit_requests.Session(pool, ssl_context)
7279

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")
80+
# Check for WPA2 Enterprise values
81+
self.ent_ssid = ssid
82+
self.ent_ident = enterprise_ident
83+
self.ent_user = enterprise_user
84+
self.ent_password = password
7885

7986
# pylint: enable=too-many-arguments
8087

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

examples/esp32spi_aio_post.py

Lines changed: 12 additions & 22 deletions
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)
@@ -48,18 +38,18 @@
4838
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
4939
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
5040
"""Use below for Most Boards"""
51-
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
41+
status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
5242
"""Uncomment below for ItsyBitsy M4"""
53-
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
43+
# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
5444
"""Uncomment below for an externally defined RGB LED (including Arduino Nano Connect)"""
5545
# import adafruit_rgbled
5646
# from adafruit_esp32spi import PWMOut
5747
# RED_LED = PWMOut.PWMOut(esp, 26)
5848
# GREEN_LED = PWMOut.PWMOut(esp, 27)
5949
# BLUE_LED = PWMOut.PWMOut(esp, 25)
60-
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
50+
# status_pixel = 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_pixel=status_pixel)
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

Lines changed: 7 additions & 16 deletions
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

@@ -49,17 +40,17 @@
4940
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
5041
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
5142
"""Use below for Most Boards"""
52-
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
43+
status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
5344
"""Uncomment below for ItsyBitsy M4"""
54-
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
45+
# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
5546
"""Uncomment below for an externally defined RGB LED (including Arduino Nano Connect)"""
5647
# import adafruit_rgbled
5748
# from adafruit_esp32spi import PWMOut
5849
# RED_LED = PWMOut.PWMOut(esp, 26)
5950
# GREEN_LED = PWMOut.PWMOut(esp, 27)
6051
# BLUE_LED = PWMOut.PWMOut(esp, 25)
61-
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
62-
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
52+
# status_pixel = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
53+
wifi = WiFiManager(esp, ssid, password, status_pixel=status_pixel)
6354

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

examples/esp32spi_ipconfig.py

Lines changed: 3 additions & 12 deletions
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

Lines changed: 7 additions & 16 deletions
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

@@ -47,18 +38,18 @@
4738
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
4839

4940
"""Use below for Most Boards"""
50-
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
41+
status_pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
5142
"""Uncomment below for ItsyBitsy M4"""
52-
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
43+
# status_pixel = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
5344
"""Uncomment below for an externally defined RGB LED (including Arduino Nano Connect)"""
5445
# import adafruit_rgbled
5546
# from adafruit_esp32spi import PWMOut
5647
# RED_LED = PWMOut.PWMOut(esp, 26)
5748
# GREEN_LED = PWMOut.PWMOut(esp, 27)
5849
# BLUE_LED = PWMOut.PWMOut(esp, 25)
59-
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
50+
# status_pixel = 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_pixel=status_pixel)
6253

6354
the_rtc = rtc.RTC()
6455

examples/esp32spi_settings.toml

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 12 deletions
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

0 commit comments

Comments
 (0)