Skip to content

Commit 607697c

Browse files
authored
Merge pull request #34 from docmollo/docs_wpa2_enterprise
Added support for WPA2 Enterprise, including example code.
2 parents c2e0fd2 + d9324b1 commit 607697c

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed

README.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ Introduction
1313
:target: https://travis-ci.com/adafruit/Adafruit_CircuitPython_ESP32SPI
1414
:alt: Build Status
1515

16-
CircuitPython driver library for using ESP32 as WiFi co-processor using SPI
16+
CircuitPython driver library for using ESP32 as WiFi co-processor using SPI.
17+
The companion firmware `is available on GitHub
18+
<https://github.com/adafruit/nina-fw>`_. Please be sure to check the example code for
19+
any specific firmware version dependencies that may exist.
1720

1821

1922
Dependencies

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: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Example code implementing WPA2 Enterprise mode
2+
#
3+
# This code requires firmware version 1.3.0, or newer, running
4+
# on the ESP32 WiFi co-processor. The latest firmware, and wiring
5+
# info if you are using something other than a PyPortal, can be found
6+
# in the Adafruit Learning System:
7+
# https://learn.adafruit.com/adding-a-wifi-co-processor-to-circuitpython-esp8266-esp32/firmware-files#esp32-only-spi-firmware-3-8
8+
9+
import re
10+
import time
11+
import board
12+
import busio
13+
from digitalio import DigitalInOut
14+
15+
from adafruit_esp32spi import adafruit_esp32spi
16+
import adafruit_esp32spi.adafruit_esp32spi_requests as requests
17+
18+
# Version number comparison code. Credit to gnud on stackoverflow
19+
# (https://stackoverflow.com/a/1714190), swapping out cmp() to
20+
# support Python 3.x and thus, CircuitPython
21+
def version_compare(version1, version2):
22+
def normalize(v):
23+
return [int(x) for x in re.sub(r'(\.0+)*$','', v).split(".")]
24+
return (normalize(version1) > normalize(version2)) - (normalize(version1) < normalize(version2))
25+
26+
print("ESP32 SPI WPA2 Enterprise test")
27+
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)
38+
39+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
40+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
41+
42+
requests.set_interface(esp)
43+
44+
if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
45+
print("ESP32 found and in idle mode")
46+
47+
# Get the ESP32 fw version number, remove trailing byte off the returned bytearray
48+
# and then convert it to a string for prettier printing and later comparison
49+
firmware_version = ''.join([chr(b) for b in esp.firmware_version[:-1]])
50+
print("Firmware vers.", firmware_version)
51+
52+
print("MAC addr:", [hex(i) for i in esp.MAC_address])
53+
54+
# WPA2 Enterprise support was added in fw ver 1.3.0. Check that the ESP32
55+
# is running at least that version, otherwise, bail out
56+
assert version_compare(firmware_version, "1.3.0") >= 0, (
57+
"Incorrect ESP32 firmware version; >= 1.3.0 required.")
58+
59+
# Set up the SSID you would like to connect to
60+
# Note that we need to call wifi_set_network prior
61+
# to calling wifi_set_enable.
62+
esp.wifi_set_network(b'YOUR_SSID_HERE')
63+
64+
# If your WPA2 Enterprise network requires an anonymous
65+
# identity to be set, you may set that here
66+
esp.wifi_set_entidentity(b'')
67+
68+
# Set the WPA2 Enterprise username you'd like to use
69+
esp.wifi_set_entusername(b'MY_USERNAME')
70+
71+
# Set the WPA2 Enterprise password you'd like to use
72+
esp.wifi_set_entpassword(b'MY_PASSWORD')
73+
74+
# Once the network settings have been configured,
75+
# we need to enable WPA2 Enterprise mode on the ESP32
76+
esp.wifi_set_entenable()
77+
78+
# Wait for the network to come up
79+
print("Connecting to AP...")
80+
while not esp.is_connected:
81+
print(".", end = "")
82+
time.sleep(2)
83+
84+
print("")
85+
print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi)
86+
print("My IP address is", esp.pretty_ip(esp.ip_address))
87+
print("IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com")))
88+
print("Ping google.com: %d ms" % esp.ping("google.com"))
89+
90+
print("Done!")

0 commit comments

Comments
 (0)