70 lines
2 KiB
Python
70 lines
2 KiB
Python
# SPDX-FileCopyrightText: 2020 Brent Rubell for Adafruit Industries
|
|
#
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
from os import getenv
|
|
import ipaddress
|
|
import ssl
|
|
import wifi
|
|
import socketpool
|
|
import adafruit_requests
|
|
|
|
# URLs to fetch from
|
|
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
|
|
JSON_QUOTES_URL = "https://www.adafruit.com/api/quotes.php"
|
|
JSON_STARS_URL = "https://api.github.com/repos/adafruit/circuitpython"
|
|
|
|
# Get WiFi details, ensure these are setup in settings.toml
|
|
ssid = getenv("CIRCUITPY_WIFI_SSID")
|
|
password = getenv("CIRCUITPY_WIFI_PASSWORD")
|
|
|
|
if None in [ssid, password]:
|
|
raise RuntimeError(
|
|
"WiFi settings are kept in settings.toml, "
|
|
"please add them there. The settings file must contain "
|
|
"'CIRCUITPY_WIFI_SSID', 'CIRCUITPY_WIFI_PASSWORD', "
|
|
"at a minimum."
|
|
)
|
|
|
|
print("ESP32-S2 WebClient Test")
|
|
|
|
print("My MAC addr:", [hex(i) for i in wifi.radio.mac_address])
|
|
|
|
print("Available WiFi networks:")
|
|
for network in wifi.radio.start_scanning_networks():
|
|
print("\t%s\t\tRSSI: %d\tChannel: %d" % (str(network.ssid, "utf-8"),
|
|
network.rssi, network.channel))
|
|
wifi.radio.stop_scanning_networks()
|
|
|
|
print(f"Connecting to {ssid}")
|
|
wifi.radio.connect(ssid, password)
|
|
print(f"Connected to {ssid}!")
|
|
print(f"My IP address is {wifi.radio.ipv4_address}")
|
|
|
|
ipv4 = ipaddress.ip_address("8.8.4.4")
|
|
print("Ping google.com: %f ms" % (wifi.radio.ping(ipv4)*1000))
|
|
|
|
pool = socketpool.SocketPool(wifi.radio)
|
|
requests = adafruit_requests.Session(pool, ssl.create_default_context())
|
|
|
|
print("Fetching text from", TEXT_URL)
|
|
response = requests.get(TEXT_URL)
|
|
print("-" * 40)
|
|
print(response.text)
|
|
print("-" * 40)
|
|
|
|
print("Fetching json from", JSON_QUOTES_URL)
|
|
response = requests.get(JSON_QUOTES_URL)
|
|
print("-" * 40)
|
|
print(response.json())
|
|
print("-" * 40)
|
|
|
|
print()
|
|
|
|
print("Fetching and parsing json from", JSON_STARS_URL)
|
|
response = requests.get(JSON_STARS_URL)
|
|
print("-" * 40)
|
|
print("CircuitPython GitHub Stars", response.json()["stargazers_count"])
|
|
print("-" * 40)
|
|
|
|
print("done")
|