|
| 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