Skip to content

Commit 1a275c7

Browse files
committed
update wifi_simpletest with Connection Manager
1 parent fdf4cbf commit 1a275c7

File tree

1 file changed

+40
-41
lines changed

1 file changed

+40
-41
lines changed
Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
3+
# Updated for Circuit Python 9.0
4+
""" WiFi Simpletest """
5+
# pylint: disable=import-error
36

47
import os
5-
import ssl
68

7-
import socketpool
9+
import adafruit_connection_manager
810
import wifi
911

1012
import adafruit_requests
@@ -13,60 +15,57 @@
1315
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
1416
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
1517

16-
# Initialize WiFi Pool (There can be only 1 pool & top of script)
17-
radio = wifi.radio
18-
pool = socketpool.SocketPool(radio)
19-
20-
print("Connecting to AP...")
21-
while not wifi.radio.ipv4_address:
22-
try:
23-
wifi.radio.connect(ssid, password)
24-
except ConnectionError as e:
25-
print("could not connect to AP, retrying: ", e)
26-
print("Connected to", str(radio.ap_info.ssid, "utf-8"), "\tRSSI:", radio.ap_info.rssi)
27-
28-
# Initialize a requests session
29-
ssl_context = ssl.create_default_context()
30-
requests = adafruit_requests.Session(pool, ssl_context)
31-
3218
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
3319
JSON_GET_URL = "https://httpbin.org/get"
3420
JSON_POST_URL = "https://httpbin.org/post"
3521

36-
print("Fetching text from %s" % TEXT_URL)
22+
# Initalize Wifi, Socket Pool, Request Session
23+
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
24+
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
25+
requests = adafruit_requests.Session(pool, ssl_context)
26+
rssi = wifi.radio.ap_info.rssi
27+
28+
print(f"\nConnecting to {ssid}...")
29+
print(f"Signal Strength: {rssi}")
30+
try:
31+
# Connect to the Wi-Fi network
32+
wifi.radio.connect(ssid, password)
33+
except OSError as e:
34+
print(f"❌ OSError: {e}")
35+
print("✅ Wifi!")
36+
37+
print(f" | GET Text Test: {TEXT_URL}")
3738
response = requests.get(TEXT_URL)
38-
print("-" * 40)
39-
40-
print("Text Response: ", response.text)
41-
print("-" * 40)
39+
print(f" | ✅ GET Response: {response.text}")
4240
response.close()
41+
print(f" | ✂️ Disconnected from {TEXT_URL}")
42+
print("-" * 80)
4343

44-
print("Fetching JSON data from %s" % JSON_GET_URL)
44+
print(f" | GET Full Response Test: {JSON_GET_URL}")
4545
response = requests.get(JSON_GET_URL)
46-
print("-" * 40)
47-
48-
print("JSON Response: ", response.json())
49-
print("-" * 40)
46+
print(f" | ✅ Unparsed Full JSON Response: {response.json()}")
5047
response.close()
48+
print(f" | ✂️ Disconnected from {JSON_GET_URL}")
49+
print("-" * 80)
5150

52-
data = "31F"
53-
print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
54-
response = requests.post(JSON_POST_URL, data=data)
55-
print("-" * 40)
56-
51+
DATA = "This is an example of a JSON value"
52+
print(f" | ✅ JSON 'value' POST Test: {JSON_POST_URL} {DATA}")
53+
response = requests.post(JSON_POST_URL, data=DATA)
5754
json_resp = response.json()
5855
# Parse out the 'data' key from json_resp dict.
59-
print("Data received from server:", json_resp["data"])
60-
print("-" * 40)
56+
print(f" | ✅ JSON 'value' Response: {json_resp['data']}")
6157
response.close()
58+
print(f" | ✂️ Disconnected from {JSON_POST_URL}")
59+
print("-" * 80)
6260

63-
json_data = {"Date": "July 25, 2019"}
64-
print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
61+
json_data = {"Date": "January 1, 1970"}
62+
print(f" | ✅ JSON 'key':'value' POST Test: {JSON_POST_URL} {json_data}")
6563
response = requests.post(JSON_POST_URL, json=json_data)
66-
print("-" * 40)
67-
6864
json_resp = response.json()
6965
# Parse out the 'json' key from json_resp dict.
70-
print("JSON Data received from server:", json_resp["json"])
71-
print("-" * 40)
66+
print(f" | ✅ JSON 'key':'value' Response: {json_resp['json']}")
7267
response.close()
68+
print(f" | ✂️ Disconnected from {JSON_POST_URL}")
69+
print("-" * 80)
70+
71+
print("Finished!")

0 commit comments

Comments
 (0)