Skip to content

Commit cbf4142

Browse files
authored
Merge pull request #177 from DJDevon3/DJDevon3-WifiSimpleTest
update wifi_simpletest with Connection Manager
2 parents 4f88beb + f66686f commit cbf4142

File tree

1 file changed

+39
-41
lines changed

1 file changed

+39
-41
lines changed
Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
# SPDX-License-Identifier: MIT
3+
# Updated for Circuit Python 9.0
4+
""" WiFi Simpletest """
35

46
import os
5-
import ssl
67

7-
import socketpool
8+
import adafruit_connection_manager
89
import wifi
910

1011
import adafruit_requests
@@ -13,60 +14,57 @@
1314
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
1415
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
1516

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-
3217
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
3318
JSON_GET_URL = "https://httpbin.org/get"
3419
JSON_POST_URL = "https://httpbin.org/post"
3520

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

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

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-
50+
DATA = "This is an example of a JSON value"
51+
print(f" | ✅ JSON 'value' POST Test: {JSON_POST_URL} {DATA}")
52+
response = requests.post(JSON_POST_URL, data=DATA)
5753
json_resp = response.json()
5854
# Parse out the 'data' key from json_resp dict.
59-
print("Data received from server:", json_resp["data"])
60-
print("-" * 40)
55+
print(f" | ✅ JSON 'value' Response: {json_resp['data']}")
6156
response.close()
57+
print(f" | ✂️ Disconnected from {JSON_POST_URL}")
58+
print("-" * 80)
6259

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

0 commit comments

Comments
 (0)