Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

relic-se
Copy link

If a device hasn't been configured properly either with CIRCUITPY_WIFI_SSID and CIRCUITPY_WIFI_PASSWORD or the "networks" setting, adafruit_portalbase.network.NetworkBase.connect will result in an obscure TypeError from the adafruit_esp32spi library.

This update filters the adafruit_portalbase.network.NetworkBase._wifi_credentials list to ensure that it includes valid ssid/password pairs. If no valid credentials are available, it will trigger an OSError exception with a descriptive message.

I am testing using the following program written for the Adafruit Fruit Jam with the adafruit_fruitjam library. The device has no WiFi credentials configured.

from adafruit_fruitjam import FruitJam
fruitjam = FruitJam(url="https://www.adafruit.com")
fruitjam.fetch()

Here is the REPL output before this update:

Connecting to AP None
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "adafruit_fruitjam/__init__.py", line 302, in fetch
  File "adafruit_portalbase/network.py", line 552, in fetch
  File "adafruit_portalbase/network.py", line 406, in connect
  File "adafruit_portalbase/wifi_coprocessor.py", line 85, in connect
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 657, in connect
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 680, in connect_AP
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 548, in wifi_set_network
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 421, in _send_command_get_response
  File "adafruit_esp32spi/adafruit_esp32spi.py", line 303, in _send_command
TypeError: object of type 'NoneType' has no len()

Here's the REPL output after this update:

Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "adafruit_fruitjam/__init__.py", line 302, in fetch
  File "/lib/adafruit_portalbase/network.py", line 560, in fetch
  File "/lib/adafruit_portalbase/network.py", line 395, in connect
OSError: No wifi credentials provided

Note: I felt that OSError was appropriate for this exception because it is related to the device configuration before a request is made, but ConnectionError may follow the relative CPython implementation more closely.

"'networks' must be a list/tuple of dicts of 'ssid' and 'password'"
)

self._wifi_credentials = list(
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks good, couple questions:

  1. what about APs with no password?
  2. to isinstance is faster and generally better then type can we use those?
  3. can we add tests?

Copy link
Author

Choose a reason for hiding this comment

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

Got you on 1 and 2, but I'm not very familiar with pytest. Let me know if I got it right.

By the way, I totally forgot that APs could have empty passwords. 😆

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll look at the tests tomorrow. I'll make suggestions for anything to add/change

Copy link
Contributor

Choose a reason for hiding this comment

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

And right - who would want something without a password? But people still complain when they can't connect because of it ...

Copy link
Author

Choose a reason for hiding this comment

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

@justmobilize I think I've got the test figured out now, but let me know if it needs any adjustment.

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.

2 participants