Skip to content

Commit d1c581f

Browse files
committed
2 parents 917ef2d + 59203bc commit d1c581f

File tree

3 files changed

+133
-14
lines changed

3 files changed

+133
-14
lines changed

adafruit_esp32spi/adafruit_esp32spi_wifimanager.py

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,55 @@
3131

3232
# pylint: disable=no-name-in-module
3333

34-
import adafruit_esp32spi.adafruit_esp32spi_requests as requests
34+
from time import sleep
35+
from micropython import const
3536
from adafruit_esp32spi import adafruit_esp32spi
37+
import adafruit_esp32spi.adafruit_esp32spi_requests as requests
3638

3739

3840
class ESPSPI_WiFiManager:
3941
"""
4042
A class to help manage the Wifi connection
4143
"""
42-
# pylint: disable=too-many-arguments
43-
def __init__(self, esp, secrets, status_pixel=None, attempts=2, debug=False):
44+
NORMAL = const(1)
45+
ENTERPRISE = const(2)
46+
47+
# pylint: disable=too-many-arguments, line-too-long
48+
def __init__(self, esp, secrets, status_pixel=None, attempts=2, connection_type=NORMAL, debug=False):
4449
"""
4550
:param ESP_SPIcontrol esp: The ESP object we are using
4651
:param dict secrets: The WiFi and Adafruit IO secrets dict (See examples)
4752
:param status_pixel: (Optional) The pixel device - A NeoPixel, DotStar,
4853
or RGB LED (default=None)
4954
:type status_pixel: NeoPixel, DotStar, or RGB LED
5055
:param int attempts: (Optional) Failed attempts before resetting the ESP32 (default=2)
56+
:param const connection_type: (Optional) Type of WiFi connection: NORMAL or ENTERPRISE
5157
"""
5258
# Read the settings
5359
self.esp = esp
5460
self.debug = debug
5561
self.ssid = secrets['ssid']
5662
self.password = secrets.get('password', None)
5763
self.attempts = attempts
64+
self._connection_type = connection_type
5865
requests.set_interface(self.esp)
5966
self.statuspix = status_pixel
6067
self.pixel_status(0)
61-
# pylint: enable=too-many-arguments
68+
69+
# Check for WPA2 Enterprise keys in the secrets dictionary and load them if they exist
70+
if secrets.get('ent_ssid'):
71+
self.ent_ssid = secrets['ent_ssid']
72+
else:
73+
self.ent_ssid = secrets['ssid']
74+
if secrets.get('ent_ident'):
75+
self.ent_ident = secrets['ent_ident']
76+
else:
77+
self.ent_ident = ''
78+
if secrets.get('ent_user'):
79+
self.ent_user = secrets['ent_user']
80+
if secrets.get('ent_password'):
81+
self.ent_password = secrets['ent_password']
82+
# pylint: enable=too-many-arguments, line-too-long
6283

6384
def reset(self):
6485
"""
@@ -79,6 +100,17 @@ def connect(self):
79100
print("MAC addr:", [hex(i) for i in self.esp.MAC_address])
80101
for access_pt in self.esp.scan_networks():
81102
print("\t%s\t\tRSSI: %d" % (str(access_pt['ssid'], 'utf-8'), access_pt['rssi']))
103+
if self._connection_type == ESPSPI_WiFiManager.NORMAL:
104+
self.connect_normal()
105+
elif self._connection_type == ESPSPI_WiFiManager.ENTERPRISE:
106+
self.connect_enterprise()
107+
else:
108+
raise TypeError("Invalid WiFi connection type specified")
109+
110+
def connect_normal(self):
111+
"""
112+
Attempt a regular style WiFi connection
113+
"""
82114
failure_count = 0
83115
while not self.esp.is_connected:
84116
try:
@@ -123,6 +155,33 @@ def create_ap(self):
123155
continue
124156
print("Access Point created! Connect to ssid: {}".format(self.ssid))
125157

158+
def connect_enterprise(self):
159+
"""
160+
Attempt an enterprise style WiFi connection
161+
"""
162+
failure_count = 0
163+
self.esp.wifi_set_network(bytes(self.ent_ssid, 'utf-8'))
164+
self.esp.wifi_set_entidentity(bytes(self.ent_ident, 'utf-8'))
165+
self.esp.wifi_set_entusername(bytes(self.ent_user, 'utf-8'))
166+
self.esp.wifi_set_entpassword(bytes(self.ent_password, 'utf-8'))
167+
self.esp.wifi_set_entenable()
168+
while not self.esp.is_connected:
169+
try:
170+
if self.debug:
171+
print("Waiting for the ESP32 to connect to the WPA2 Enterprise AP...")
172+
self.pixel_status((100, 0, 0))
173+
sleep(1)
174+
failure_count = 0
175+
self.pixel_status((0, 100, 0))
176+
sleep(1)
177+
except (ValueError, RuntimeError) as error:
178+
print("Failed to connect, retrying\n", error)
179+
failure_count += 1
180+
if failure_count >= self.attempts:
181+
failure_count = 0
182+
self.reset()
183+
continue
184+
126185
def get(self, url, **kw):
127186
"""
128187
Pass the Get request to requests and update status LED

examples/esp32spi_wpa2ent_aio_post.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import time
2+
import board
3+
import busio
4+
from digitalio import DigitalInOut
5+
import neopixel
6+
from adafruit_esp32spi import adafruit_esp32spi
7+
from adafruit_esp32spi.adafruit_esp32spi_wifimanager import ESPSPI_WiFiManager
8+
9+
print("ESP32 SPI WPA2 Enterprise webclient test")
10+
11+
# Get wifi details and more from a secrets.py file
12+
try:
13+
from secrets import secrets
14+
except ImportError:
15+
print("WiFi secrets are kept in secrets.py, please add them there!")
16+
raise
17+
18+
# ESP32 setup
19+
# If your board does define the three pins listed below,
20+
# you can set the correct pins in the second block
21+
try:
22+
esp32_cs = DigitalInOut(board.ESP_CS)
23+
esp32_ready = DigitalInOut(board.ESP_BUSY)
24+
esp32_reset = DigitalInOut(board.ESP_RESET)
25+
except AttributeError:
26+
esp32_cs = DigitalInOut(board.D9)
27+
esp32_ready = DigitalInOut(board.D10)
28+
esp32_reset = DigitalInOut(board.D5)
29+
30+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
31+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
32+
"""Use below for Most Boards"""
33+
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
34+
"""Uncomment below for ItsyBitsy M4"""
35+
#status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
36+
wifi = ESPSPI_WiFiManager(esp, secrets, status_light, connection_type=ESPSPI_WiFiManager.ENTERPRISE)
37+
38+
counter = 0
39+
40+
while True:
41+
try:
42+
print("Posting data...", end='')
43+
data = counter
44+
feed = 'test'
45+
payload = {'value':data}
46+
response = wifi.post(
47+
"https://io.adafruit.com/api/v2/"+secrets['aio_username']+"/feeds/"+feed+"/data",
48+
json=payload,
49+
headers={"X-AIO-KEY":secrets['aio_key']})
50+
print(response.json())
51+
response.close()
52+
counter = counter + 1
53+
print("OK")
54+
except (ValueError, RuntimeError) as e:
55+
print("Failed to get data, retrying\n", e)
56+
wifi.reset()
57+
continue
58+
response = None
59+
time.sleep(15)

examples/esp32spi_wpa2ent_simpletest.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@ def normalize(v):
2525

2626
print("ESP32 SPI WPA2 Enterprise test")
2727

28-
# For running on the PyPortal, use this block
29-
esp32_cs = DigitalInOut(board.ESP_CS)
30-
esp32_ready = DigitalInOut(board.ESP_BUSY)
31-
esp32_reset = DigitalInOut(board.ESP_RESET)
32-
33-
# For a board that doesn't have the ESP pin definitions, use this block and
34-
# set the pins as needed.
35-
#esp32_cs = DigitalInOut(board.D8)
36-
#esp32_ready = DigitalInOut(board.D5)
37-
#esp32_reset = DigitalInOut(board.D7)
28+
# ESP32 setup
29+
# If your board does define the three pins listed below,
30+
# you can set the correct pins in the second block
31+
try:
32+
esp32_cs = DigitalInOut(board.ESP_CS)
33+
esp32_ready = DigitalInOut(board.ESP_BUSY)
34+
esp32_reset = DigitalInOut(board.ESP_RESET)
35+
except AttributeError:
36+
esp32_cs = DigitalInOut(board.D9)
37+
esp32_ready = DigitalInOut(board.D10)
38+
esp32_reset = DigitalInOut(board.D5)
3839

3940
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
4041
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

0 commit comments

Comments
 (0)