|
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 """ |
| 5 | +# pylint: disable=import-error |
3 | 6 |
|
4 | 7 | import os
|
5 |
| -import ssl |
6 | 8 |
|
7 |
| -import socketpool |
| 9 | +import adafruit_connection_manager |
8 | 10 | import wifi
|
9 | 11 |
|
10 | 12 | import adafruit_requests
|
|
13 | 15 | ssid = os.getenv("CIRCUITPY_WIFI_SSID")
|
14 | 16 | password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
|
15 | 17 |
|
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 | 18 | TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
|
33 | 19 | JSON_GET_URL = "https://httpbin.org/get"
|
34 | 20 | JSON_POST_URL = "https://httpbin.org/post"
|
35 | 21 |
|
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}") |
37 | 38 | 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}") |
42 | 40 | response.close()
|
| 41 | +print(f" | ✂️ Disconnected from {TEXT_URL}") |
| 42 | +print("-" * 80) |
43 | 43 |
|
44 |
| -print("Fetching JSON data from %s" % JSON_GET_URL) |
| 44 | +print(f" | GET Full Response Test: {JSON_GET_URL}") |
45 | 45 | 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()}") |
50 | 47 | response.close()
|
| 48 | +print(f" | ✂️ Disconnected from {JSON_GET_URL}") |
| 49 | +print("-" * 80) |
51 | 50 |
|
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) |
57 | 54 | json_resp = response.json()
|
58 | 55 | # 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']}") |
61 | 57 | response.close()
|
| 58 | +print(f" | ✂️ Disconnected from {JSON_POST_URL}") |
| 59 | +print("-" * 80) |
62 | 60 |
|
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}") |
65 | 63 | response = requests.post(JSON_POST_URL, json=json_data)
|
66 |
| -print("-" * 40) |
67 |
| - |
68 | 64 | json_resp = response.json()
|
69 | 65 | # 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']}") |
72 | 67 | response.close()
|
| 68 | +print(f" | ✂️ Disconnected from {JSON_POST_URL}") |
| 69 | +print("-" * 80) |
| 70 | + |
| 71 | +print("Finished!") |
0 commit comments