Skip to content

Commit fc71e49

Browse files
committed
adding error handling for settings.toml
Adding error handling for settings.toml to pico w wifi examples. if a settings.toml entry is not found/incorrect then a type error is raised but it isn't very descriptive, especially for beginners
1 parent d110742 commit fc71e49

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
lines changed

Pico_W_CircuitPython_WiFi_Examples/Pico_W_Adafruit_IO/code.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@
1313
import adafruit_ahtx0
1414
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
1515

16-
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
16+
try:
17+
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
18+
except TypeError:
19+
print("Could not find WiFi info. Check your settings.toml file!")
20+
raise
1721

18-
aio_username = os.getenv('aio_username')
19-
aio_key = os.getenv('aio_key')
22+
try:
23+
aio_username = os.getenv('ADAFRUIT_AIO_USERNAME')
24+
aio_key = os.getenv('ADAFRUIT_AIO_KEY')
25+
except TypeError:
26+
print("Could not find Adafruit IO info. Check your settings.toml file!")
27+
raise
2028

2129
pool = socketpool.SocketPool(wifi.radio)
2230
requests = adafruit_requests.Session(pool, ssl.create_default_context())

Pico_W_CircuitPython_WiFi_Examples/Pico_W_Azure_IoT_Central/code.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
aht20 = adafruit_ahtx0.AHTx0(i2c)
2020

2121
print("Connecting to WiFi...")
22-
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
22+
try:
23+
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
24+
except TypeError:
25+
print("Could not find WiFi info. Check your settings.toml file!")
26+
raise
2327

2428
print("Connected to WiFi!")
2529

@@ -38,9 +42,13 @@
3842
# Create an IoT Hub device client and connect
3943
esp = None
4044
pool = socketpool.SocketPool(wifi.radio)
41-
device = IoTCentralDevice(
42-
pool, esp, os.getenv('id_scope'), os.getenv('device_id'), os.getenv('device_primary_key')
43-
)
45+
try:
46+
device = IoTCentralDevice(
47+
pool, esp, os.getenv('id_scope'), os.getenv('device_id'), os.getenv('device_primary_key')
48+
)
49+
except TypeError:
50+
print("Could not find Azure IoT info. Check your settings.toml file!")
51+
raise
4452

4553
print("Connecting to Azure IoT Central...")
4654

Pico_W_CircuitPython_WiFi_Examples/Pico_W_Basic_WiFi_Test/code.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
print("Connecting to WiFi")
1212

1313
# connect to your SSID
14-
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
14+
try:
15+
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
16+
except TypeError:
17+
print("Could not find WiFi info. Check your settings.toml file!")
18+
raise
1519

1620
print("Connected to WiFi")
1721

Pico_W_CircuitPython_WiFi_Examples/Pico_W_JSON_Feed/code.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,23 @@
1010
import microcontroller
1111
import adafruit_requests
1212

13-
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
13+
try:
14+
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
15+
except TypeError:
16+
print("Could not find WiFi info. Check your settings.toml file!")
17+
raise
1418

1519
# Use cityname, country code where countrycode is ISO3166 format.
1620
# E.g. "New York, US" or "London, GB"
1721
location = "Manhattan, US"
1822

1923
# openweathermap URL, brings in your location & your token
20-
url = "http://api.openweathermap.org/data/2.5/weather?q="+location
21-
url += "&appid="+os.getenv('openweather_token')
24+
try:
25+
url = "http://api.openweathermap.org/data/2.5/weather?q="+location
26+
url += "&appid="+os.getenv('openweather_token')
27+
except TypeError:
28+
print("Could not find OpenWeatherMap token. Check your settings.toml file!")
29+
raise
2230

2331
pool = socketpool.SocketPool(wifi.radio)
2432
requests = adafruit_requests.Session(pool, ssl.create_default_context())

Pico_W_CircuitPython_WiFi_Examples/Pico_W_Requests/code.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
quotes_url = "https://www.adafruit.com/api/quotes.php"
1515

1616
# connect to SSID
17-
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
17+
try:
18+
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
19+
except TypeError:
20+
print("Could not find WiFi info. Check your settings.toml file!")
21+
raise
1822

1923
pool = socketpool.SocketPool(wifi.radio)
2024
requests = adafruit_requests.Session(pool, ssl.create_default_context())

0 commit comments

Comments
 (0)