Skip to content

Commit 8e2b787

Browse files
committed
Replaced enum-like class with 2 constants; various fix-ups based on PR reviews; added WPA2 Enterprise version of aio example script
1 parent d8f4ad2 commit 8e2b787

File tree

2 files changed

+68
-19
lines changed

2 files changed

+68
-19
lines changed

adafruit_esp32spi/adafruit_esp32spi_wifimanager.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,18 @@
3131

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

34+
from micropython import const
3435
from adafruit_esp32spi import adafruit_esp32spi
3536
import adafruit_esp32spi.adafruit_esp32spi_requests as requests
3637

37-
class WiFiConnType: # pylint: disable=too-few-public-methods
38-
"""An enum-like class representing the different types of WiFi connections
39-
that can be made. The values can be referenced like ``WiFiConnType.normal``.
40-
Possible values are
41-
- ``ThermocoupleType.normal``
42-
- ``ThermocoupleType.enterprise``
43-
"""
44-
# pylint: disable=invalid-name
45-
normal = 1
46-
enterprise = 2
47-
4838
class ESPSPI_WiFiManager:
4939
"""
5040
A class to help manage the Wifi connection
5141
"""
52-
def __init__(self, esp, secrets, status_pixel=None, attempts=2, wificonntype=WiFiConnType.normal):
42+
NORMAL = const(1)
43+
ENTERPRISE = const(2)
44+
45+
def __init__(self, esp, secrets, status_pixel=None, attempts=2, connection_type=NORMAL):
5346
"""
5447
:param ESP_SPIcontrol esp: The ESP object we are using
5548
:param dict secrets: The WiFi and Adafruit IO secrets dict (See examples)
@@ -68,9 +61,9 @@ def __init__(self, esp, secrets, status_pixel=None, attempts=2, wificonntype=WiF
6861
self.ent_ssid = secrets['ent_ssid']
6962
self.ent_ident = secrets['ent_ident']
7063
self.ent_user = secrets['ent_user']
71-
self.ent_passwd = secrets['ent_passwd']
64+
self.ent_password = secrets['ent_password']
7265
self.attempts = attempts
73-
self.wificonntype = wificonntype
66+
self._connection_type = connection_type
7467
requests.set_interface(self._esp)
7568
self.statuspix = status_pixel
7669
self.pixel_status(0)
@@ -95,7 +88,7 @@ def connect(self):
9588
for access_pt in self._esp.scan_networks():
9689
print("\t%s\t\tRSSI: %d" % (str(access_pt['ssid'], 'utf-8'), access_pt['rssi']))
9790
failure_count = 0
98-
if self.wificonntype == WiFiConnType.normal:
91+
if self._connection_type == ESPSPI_WiFiManager.NORMAL:
9992
while not self._esp.is_connected:
10093
try:
10194
if self.debug:
@@ -111,11 +104,11 @@ def connect(self):
111104
failure_count = 0
112105
self.reset()
113106
continue
114-
elif self.wificonntype == WiFiConnType.enterprise:
107+
elif self._connection_type == ESPSPI_WiFiManager.ENTERPRISE:
115108
self._esp.wifi_set_network(bytes(self.ent_ssid, 'utf-8'))
116109
self._esp.wifi_set_entidentity(bytes(self.ent_ident, 'utf-8'))
117110
self._esp.wifi_set_entusername(bytes(self.ent_user, 'utf-8'))
118-
self._esp.wifi_set_entpassword(bytes(self.ent_passwd, 'utf-8'))
111+
self._esp.wifi_set_entpassword(bytes(self.ent_password, 'utf-8'))
119112
self._esp.wifi_set_entenable()
120113
while not self._esp.is_connected:
121114
try:
@@ -132,8 +125,7 @@ def connect(self):
132125
self.reset()
133126
continue
134127
else:
135-
print("Invalid connection type! Exiting...")
136-
exit(1)
128+
raise TypeError("Invalid WiFi connection type specified")
137129

138130
def get(self, url, **kw):
139131
"""

examples/esp32spi_wpa2ent_aio_post.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
# If you are using a board with pre-defined ESP32 Pins:
19+
esp32_cs = DigitalInOut(board.ESP_CS)
20+
esp32_ready = DigitalInOut(board.ESP_BUSY)
21+
esp32_reset = DigitalInOut(board.ESP_RESET)
22+
23+
# If you have an externally connected ESP32:
24+
# esp32_cs = DigitalInOut(board.D9)
25+
# esp32_ready = DigitalInOut(board.D10)
26+
# esp32_reset = DigitalInOut(board.D5)
27+
28+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
29+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
30+
"""Use below for Most Boards"""
31+
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
32+
"""Uncomment below for ItsyBitsy M4"""
33+
#status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
34+
wifi = ESPSPI_WiFiManager(esp, secrets, status_light, connection_type=ESPSPI_WiFiManager.ENTERPRISE)
35+
36+
counter = 0
37+
38+
while True:
39+
try:
40+
print("Posting data...", end='')
41+
data = counter
42+
feed = 'test'
43+
payload = {'value':data}
44+
response = wifi.post(
45+
"https://io.adafruit.com/api/v2/"+secrets['aio_username']+"/feeds/"+feed+"/data",
46+
json=payload,
47+
headers={"X-AIO-KEY":secrets['aio_key']})
48+
print(response.json())
49+
response.close()
50+
counter = counter + 1
51+
print("OK")
52+
except (ValueError, RuntimeError) as e:
53+
print("Failed to get data, retrying\n", e)
54+
wifi.reset()
55+
continue
56+
response = None
57+
time.sleep(15)

0 commit comments

Comments
 (0)