Skip to content

Instantly share code, notes, and snippets.

@FoamyGuy
Created February 2, 2022 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FoamyGuy/7440ab871cb9357866f718c2dd43bac7 to your computer and use it in GitHub Desktop.
Save FoamyGuy/7440ab871cb9357866f718c2dd43bac7 to your computer and use it in GitHub Desktop.
# SPDX-FileCopyrightText: 2021 jfabernathy for Adafruit Industries
# SPDX-License-Identifier: MIT
# adafruit_requests usage with a CircuitPython socket
# this has been tested with Adafruit Metro ESP32-S2 Express
import ssl
import time
import wifi
import socketpool
import adafruit_requests as requests
# Get wifi details and more from a secrets.py file
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])
print("My IP address is", wifi.radio.ipv4_address)
socket = socketpool.SocketPool(wifi.radio)
https = requests.Session(socket, ssl.create_default_context())
TEXT_URL = "https://httpbin.org/get"
JSON_GET_URL = "https://jwst.nasa.gov/content/webbLaunch/flightCurrentState2.0.json?unique={}".format(int(time.monotonic()))
print("Fetching JSON from %s" % TEXT_URL)
response = https.get(TEXT_URL)
print("-" * 40)
print("1st JSON Response: ", response.json())
print("-" * 40)
response.close()
print("Fetching JSON data from %s" % JSON_GET_URL)
response = https.get(JSON_GET_URL)
print("-" * 40)
print("JSON Response: ", response.json())
print("-" * 40)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment