Skip to content

Adding WPA2 Enterprise support to the WiFi manager library #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 46 commits into from
Jul 15, 2019

Conversation

docmollo
Copy link
Contributor

This is my first attempt to add support for WPA2 Enterprise to the WiFi Manager library. It all works in my testing environment. The one thing I was thinking about, but didn't add, was support to check the NINA firmware version and bail out if the ESP32 isn't running a compatible version.

NOTE: Pylint returns some not-happy results, but I'm not sure if they are going to be issues. If so, I'm not sure how to go about resolving them. Some guideance would be appreciated.

martymcguire and others added 5 commits March 13, 2019 15:41
Prevents errors with unsent data on large header values, long URL paths
In certain cases, the socket may not receive a full response (or any
response), causing the while loop in readline to go on forever. After
failing to receive any data, this fix will raise an exception and fail
out.
@ladyada
Copy link
Member

ladyada commented May 19, 2019

nice, please make :param int con_type an enum or at least a named constant!

@docmollo
Copy link
Contributor Author

So...I thought I could just google this and figure it out, but this is getting into actual CS stuff that I haven't dealt with before. Is the fix as simple as just changing the "param int" to "param enum"?

@ladyada
Copy link
Member

ladyada commented May 19, 2019

ok make them named constants then!

@docmollo
Copy link
Contributor Author

Ah ha! I think I figured out what you're asking for. Are you suggesting something like this class in the MAX31856 library?

https://github.com/adafruit/Adafruit_CircuitPython_MAX31856/blob/master/adafruit_max31856.py#L95

@ladyada
Copy link
Member

ladyada commented May 19, 2019

yes!

…R reviews; added WPA2 Enterprise version of aio example script
@docmollo
Copy link
Contributor Author

Just a heads-up - I took @kattni up on her offer to assist with the pylint issues. We're scheduled to chat about this on Thursday (5/23) evening, so there are other commits coming.

@docmollo
Copy link
Contributor Author

Had my meeting with @kattni this evening. I'm going to refactor the connect() function into two different functions for the two different types of connections. As part of that work, I'm also going to work on the ideas @brentru and I have been discussing. It's probably going to take me a week or two to get to all the changes complete, so if you would like a status, feel free to ping me here or on Discord.

@jerryneedell
Copy link
Contributor

ok -- now relevant to this PR - I tried my often used "cherlights" test with this PR and there appears to have been some breaking change -- this code worked on the previous release

Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 4.1.0-beta.0 on 2019-06-14; Adafruit Metro M4 Airlift Lite with samd51j19
>>> 
>>> import cheer_ring
ESP32 SPI webclient test
Fetching json from https://api.thingspeak.com/channels/1417/feeds.json?results=1
Failed to connect, retrying
 ('No such ssid', b'Needell Airport')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "cheer_ring.py", line 55, in <module>
  File "cheer_ring.py", line 45, in <module>
  File "adafruit_esp32spi/adafruit_esp32spi_wifimanager.py", line 170, in get
  File "adafruit_esp32spi/adafruit_esp32spi_wifimanager.py", line 105, in connect
  File "adafruit_esp32spi/adafruit_esp32spi_wifimanager.py", line 129, in connect_normal
  File "adafruit_esp32spi/adafruit_esp32spi_wifimanager.py", line 125, in connect_normal
NameError: local variable referenced before assignment
>>> 

code

import time
import board
import busio
from digitalio import DigitalInOut

from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager

import neopixel
import adafruit_fancyled.adafruit_fancyled as fancy

# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

print("ESP32 SPI webclient test")

DATA_SOURCE = "https://api.thingspeak.com/channels/1417/feeds.json?results=1"
DATA_LOCATION = ["feeds", 0, "field2"]

esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
"""Use below for Most Boards"""
status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards
"""Uncomment below for ItsyBitsy M4"""
#status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)

# neopixels
pixels = neopixel.NeoPixel(board.A1, 16, brightness=0.3)
pixels.fill(0)

# we'll save the value in question
last_value = value = None

while True:
    try:
        print("Fetching json from", DATA_SOURCE)
        response = wifi.get(DATA_SOURCE)
        print(response.json())
        value = response.json()
        for key in DATA_LOCATION:
            value = value[key]
            print(value)
        response.close()
    except (ValueError, RuntimeError) as e:
        print("Failed to get data, retrying\n", e)
        wifi.reset()
        continue

    if not value:
        continue
    if last_value != value:
        color = int(value[1:], 16)
        red = color >> 16 & 0xFF
        green = color >> 8 & 0xFF
        blue = color& 0xFF
        gamma_corrected = fancy.gamma_adjust(fancy.CRGB(red, green, blue)).pack()

        pixels.fill(gamma_corrected)

@jerryneedell
Copy link
Contributor

jerryneedell commented Jun 18, 2019

hmmm -- on a second run -- it ran ok -- looking further into it.

ah -- it fails only after the conection failure -- in the past, wifimanager would recover gracefully and retry -- something has changed in wifimanager

found it -- the line initializing failure_count was moved so it is not initialized before the exception and then fails the increment at line 125
the error is only present if the initial connection fails -- which is very common for me!

@jerryneedell
Copy link
Contributor

jerryneedell commented Jun 18, 2019

a simple fix -- just have to move the line taht initializes failure_cunt up before the attempt to connect or else it throws the exception before it gets initialized

diff --git a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py
index 6f7c944..0ba847c 100755
--- a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py
+++ b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py
@@ -117,8 +117,8 @@ class ESPSPI_WiFiManager:
                 if self.debug:
                     print("Connecting to AP...")
                 self.pixel_status((100, 0, 0))
-                self._esp.connect_AP(bytes(self.ssid, 'utf-8'), bytes(self.password, 'utf-8'))
                 failure_count = 0
+                self._esp.connect_AP(bytes(self.ssid, 'utf-8'), bytes(self.password, 'utf-8'))
                 self.pixel_status((0, 100, 0))
             except (ValueError, RuntimeError) as error:
                 print("Failed to connect, retrying\n", error)

@jerryneedell
Copy link
Contributor

jerryneedell commented Jun 18, 2019

oops - the proposed fix above is not correct -- it will circumvent the maximum number of retries.
failure_count must be initialized to 0 before the try/except loop stars -- it also should be reset to 0 after a successful connection.
Sorry for the confusion.

this should work better

diff --git a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py
index 6f7c944..f5ac570 100755
--- a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py
+++ b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py
@@ -112,6 +112,7 @@ class ESPSPI_WiFiManager:
         """
         Attempt a regular style WiFi connection
         """
+        failure_count = 0
         while not self._esp.is_connected:
             try:
                 if self.debug:

Copy link
Contributor

@jerryneedell jerryneedell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see additional comment/concern in connect_enterprise

@docmollo
Copy link
Contributor Author

Thanks @jerryneedell and @nevercast for poking at the nina-fw, but I'm going to ignore those issues mentioned above since they are unrelated to this PR. I'll follow-up on the issue opened on the nina-fw repo when I get a chance (probably later this week or weekend).

With regards to the failure_count issue, yeah, that's my fault. As soon as you mentioned it, I knew what had happened. Work suddenly got very busy yesterday after the CP weekly meeting, so I'm only going to have time after work this week to work on fixing the issue. Worst case, I'll definitely have time this weekend.

Thanks again!

@docmollo
Copy link
Contributor Author

@jerryneedell if you could give this fix a try, I'd appreciate it

@docmollo
Copy link
Contributor Author

OK....I think this will do it, @kattni. Much hacking of code and poking at git, and I think I have it. Let me know if you catch something I missed....totally possible at this point ;)

Copy link
Contributor

@kattni kattni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this up!

@kattni kattni merged commit 59203bc into adafruit:master Jul 15, 2019
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jul 16, 2019
Updating https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI to 1.6.0 from 1.5.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_ESP32SPI#45 from docmollo/docs_wpa2_wifimgr

Updating https://github.com/adafruit/Adafruit_CircuitPython_PyPortal to 3.0.5 from 3.0.4:
  > Merge pull request adafruit/Adafruit_CircuitPython_PyPortal#43 from dastels/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_CursorControl to 2.1.1 from 2.1.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_CursorControl#9 from brentru/patch-generate-cursor

Updating https://github.com/adafruit/Adafruit_CircuitPython_Hue to 1.0.1 from v1.0.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_Hue#8 from NanoDano/patch-2
  > Merge pull request adafruit/Adafruit_CircuitPython_Hue#7 from NanoDano/patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_SimpleIO to 1.2.1 from 1.2.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_SimpleIO#41 from iraytrace/master

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Added the following libraries: Adafruit_CircuitPython_MiniMQTT
@docmollo docmollo deleted the docs_wpa2_wifimgr branch September 28, 2019 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.