|
1 | 1 | # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries |
2 | 2 | # SPDX-License-Identifier: MIT |
| 3 | +# Updated for Circuit Python 9.0 |
| 4 | +""" WiFi Simpletest """ |
3 | 5 |
|
4 | 6 | import os |
5 | | -import ssl |
6 | 7 |
|
7 | | -import socketpool |
| 8 | +import adafruit_connection_manager |
8 | 9 | import wifi |
9 | 10 |
|
10 | 11 | import adafruit_requests |
|
13 | 14 | ssid = os.getenv("CIRCUITPY_WIFI_SSID") |
14 | 15 | password = os.getenv("CIRCUITPY_WIFI_PASSWORD") |
15 | 16 |
|
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 | | - |
32 | 17 | TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" |
33 | 18 | JSON_GET_URL = "https://httpbin.org/get" |
34 | 19 | JSON_POST_URL = "https://httpbin.org/post" |
35 | 20 |
|
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}") |
37 | 37 | 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}") |
42 | 39 | response.close() |
| 40 | +print(f" | ✂️ Disconnected from {TEXT_URL}") |
| 41 | +print("-" * 80) |
43 | 42 |
|
44 | | -print("Fetching JSON data from %s" % JSON_GET_URL) |
| 43 | +print(f" | GET Full Response Test: {JSON_GET_URL}") |
45 | 44 | 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()}") |
50 | 46 | response.close() |
| 47 | +print(f" | ✂️ Disconnected from {JSON_GET_URL}") |
| 48 | +print("-" * 80) |
51 | 49 |
|
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) |
57 | 53 | json_resp = response.json() |
58 | 54 | # 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']}") |
61 | 56 | response.close() |
| 57 | +print(f" | ✂️ Disconnected from {JSON_POST_URL}") |
| 58 | +print("-" * 80) |
62 | 59 |
|
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}") |
65 | 62 | response = requests.post(JSON_POST_URL, json=json_data) |
66 | | -print("-" * 40) |
67 | | - |
68 | 63 | json_resp = response.json() |
69 | 64 | # 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']}") |
72 | 66 | response.close() |
| 67 | +print(f" | ✂️ Disconnected from {JSON_POST_URL}") |
| 68 | +print("-" * 80) |
| 69 | + |
| 70 | +print("Finished!") |
0 commit comments