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