Skip to content

Commit a9fd632

Browse files
committed
Added support for WPA2 Enterprise, including example code.
1 parent c2e0fd2 commit a9fd632

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@
8282

8383
_SEND_DATA_TCP_CMD = const(0x44)
8484
_GET_DATABUF_TCP_CMD = const(0x45)
85+
_SET_ENT_IDENT_CMD = const(0x4A)
86+
_SET_ENT_UNAME_CMD = const(0x4B)
87+
_SET_ENT_PASSWD_CMD = const(0x4C)
88+
_SET_ENT_ENABLE_CMD = const(0x4F)
8589

8690
_START_CMD = const(0xE0)
8791
_END_CMD = const(0xEE)
@@ -376,6 +380,30 @@ def wifi_set_passphrase(self, ssid, passphrase):
376380
if resp[0][0] != 1:
377381
raise RuntimeError("Failed to set passphrase")
378382

383+
def wifi_set_entidentity(self, ident):
384+
"""Sets the WPA2 Enterprise anonymous identity"""
385+
resp = self._send_command_get_response(_SET_ENT_IDENT_CMD, [ident])
386+
if resp[0][0] != 1:
387+
raise RuntimeError("Failed to set enterprise anonymous identity")
388+
389+
def wifi_set_entusername(self, username):
390+
"""Sets the desired WPA2 Enterprise username"""
391+
resp = self._send_command_get_response(_SET_ENT_UNAME_CMD, [username])
392+
if resp[0][0] != 1:
393+
raise RuntimeError("Failed to set enterprise username")
394+
395+
def wifi_set_entpassword(self, password):
396+
"""Sets the desired WPA2 Enterprise password"""
397+
resp = self._send_command_get_response(_SET_ENT_PASSWD_CMD, [password])
398+
if resp[0][0] != 1:
399+
raise RuntimeError("Failed to set enterprise password")
400+
401+
def wifi_set_entenable(self):
402+
"""Enables WPA2 Enterprise mode"""
403+
resp = self._send_command_get_response(_SET_ENT_ENABLE_CMD)
404+
if resp[0][0] != 1:
405+
raise RuntimeError("Failed to enable enterprise mode")
406+
379407
@property
380408
def ssid(self):
381409
"""The name of the access point we're connected to"""
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import time
2+
import board
3+
import busio
4+
from digitalio import DigitalInOut
5+
6+
from adafruit_esp32spi import adafruit_esp32spi
7+
import adafruit_esp32spi.adafruit_esp32spi_requests as requests
8+
9+
print("ESP32 SPI WPA2 Enterprise test")
10+
11+
# For running on the PyPortal, use this block
12+
esp32_cs = DigitalInOut(board.ESP_CS)
13+
esp32_ready = DigitalInOut(board.ESP_BUSY)
14+
esp32_reset = DigitalInOut(board.ESP_RESET)
15+
16+
# For a board that doesn't have the ESP pin definitions, use this block and
17+
# set the pins as needed. To connect your board to an ESP32 HUZZAH, here
18+
# are the pin-outs on the HUZZAH32:
19+
# https://learn.adafruit.com/adding-a-wifi-co-processor-to-circuitpython-esp8266-esp32/firmware-files#esp32-only-spi-firmware-3-8
20+
#esp32_cs = DigitalInOut(board.D8)
21+
#esp32_ready = DigitalInOut(board.D5)
22+
#esp32_reset = DigitalInOut(board.D7)
23+
24+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
25+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
26+
27+
requests.set_interface(esp)
28+
29+
if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
30+
print("ESP32 found and in idle mode")
31+
print("Firmware vers.", esp.firmware_version)
32+
print("MAC addr:", [hex(i) for i in esp.MAC_address])
33+
34+
# Set up the SSID you would like to connect to
35+
esp.wifi_set_network(b'YOUR_SSID_HERE')
36+
37+
# If your WPA2 Enterprise network requires an anonymous
38+
# identity to be set, you may set that here
39+
esp.wifi_set_entidentity(b'')
40+
41+
# Set the WPA2 Enterprise username you'd like to use
42+
esp.wifi_set_entusername(b'MY_USERNAME')
43+
44+
# Set the WPA2 Enterprise password you'd like to use
45+
esp.wifi_set_entpassword(b'MY_PASSWORD')
46+
47+
# Once the network settings have been configured,
48+
# we need to enable WPA2 Enterprise mode on the ESP32
49+
esp.wifi_set_entenable()
50+
51+
# Wait for the network to come up
52+
print("Connecting to AP...")
53+
while not esp.is_connected:
54+
print(".", end = "")
55+
time.sleep(2)
56+
57+
print("")
58+
print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi)
59+
print("My IP address is", esp.pretty_ip(esp.ip_address))
60+
print("IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com")))
61+
print("Ping google.com: %d ms" % esp.ping("google.com"))
62+
63+
print("Done!")

0 commit comments

Comments
 (0)