Skip to content

Commit

Permalink
Merge pull request #177 from DJDevon3/DJDevon3-WifiSimpleTest
Browse files Browse the repository at this point in the history
update wifi_simpletest with Connection Manager
  • Loading branch information
FoamyGuy committed Apr 10, 2024
2 parents 4f88beb + f66686f commit cbf4142
Showing 1 changed file with 39 additions and 41 deletions.
80 changes: 39 additions & 41 deletions examples/wifi/requests_wifi_simpletest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# Updated for Circuit Python 9.0
""" WiFi Simpletest """

import os
import ssl

import socketpool
import adafruit_connection_manager
import wifi

import adafruit_requests
Expand All @@ -13,60 +14,57 @@
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")

# Initialize WiFi Pool (There can be only 1 pool & top of script)
radio = wifi.radio
pool = socketpool.SocketPool(radio)

print("Connecting to AP...")
while not wifi.radio.ipv4_address:
try:
wifi.radio.connect(ssid, password)
except ConnectionError as e:
print("could not connect to AP, retrying: ", e)
print("Connected to", str(radio.ap_info.ssid, "utf-8"), "\tRSSI:", radio.ap_info.rssi)

# Initialize a requests session
ssl_context = ssl.create_default_context()
requests = adafruit_requests.Session(pool, ssl_context)

TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
JSON_GET_URL = "https://httpbin.org/get"
JSON_POST_URL = "https://httpbin.org/post"

print("Fetching text from %s" % TEXT_URL)
# Initalize Wifi, Socket Pool, Request Session
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio)
requests = adafruit_requests.Session(pool, ssl_context)
rssi = wifi.radio.ap_info.rssi

print(f"\nConnecting to {ssid}...")
print(f"Signal Strength: {rssi}")
try:
# Connect to the Wi-Fi network
wifi.radio.connect(ssid, password)
except OSError as e:
print(f"❌ OSError: {e}")
print("✅ Wifi!")

print(f" | GET Text Test: {TEXT_URL}")
response = requests.get(TEXT_URL)
print("-" * 40)

print("Text Response: ", response.text)
print("-" * 40)
print(f" | ✅ GET Response: {response.text}")
response.close()
print(f" | ✂️ Disconnected from {TEXT_URL}")
print("-" * 80)

print("Fetching JSON data from %s" % JSON_GET_URL)
print(f" | GET Full Response Test: {JSON_GET_URL}")
response = requests.get(JSON_GET_URL)
print("-" * 40)

print("JSON Response: ", response.json())
print("-" * 40)
print(f" | ✅ Unparsed Full JSON Response: {response.json()}")
response.close()
print(f" | ✂️ Disconnected from {JSON_GET_URL}")
print("-" * 80)

data = "31F"
print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
response = requests.post(JSON_POST_URL, data=data)
print("-" * 40)

DATA = "This is an example of a JSON value"
print(f" | ✅ JSON 'value' POST Test: {JSON_POST_URL} {DATA}")
response = requests.post(JSON_POST_URL, data=DATA)
json_resp = response.json()
# Parse out the 'data' key from json_resp dict.
print("Data received from server:", json_resp["data"])
print("-" * 40)
print(f" | ✅ JSON 'value' Response: {json_resp['data']}")
response.close()
print(f" | ✂️ Disconnected from {JSON_POST_URL}")
print("-" * 80)

json_data = {"Date": "July 25, 2019"}
print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
json_data = {"Date": "January 1, 1970"}
print(f" | ✅ JSON 'key':'value' POST Test: {JSON_POST_URL} {json_data}")
response = requests.post(JSON_POST_URL, json=json_data)
print("-" * 40)

json_resp = response.json()
# Parse out the 'json' key from json_resp dict.
print("JSON Data received from server:", json_resp["json"])
print("-" * 40)
print(f" | ✅ JSON 'key':'value' Response: {json_resp['json']}")
response.close()
print(f" | ✂️ Disconnected from {JSON_POST_URL}")
print("-" * 80)

print("Finished!")

0 comments on commit cbf4142

Please sign in to comment.