from
digitalio
import
DigitalInOut
import
adafruit_esp32spi.adafruit_esp32spi_socket
as
socket
from
adafruit_esp32spi
import
adafruit_esp32spi
import
adafruit_requests
as
requests
# Add a secrets.py to your filesystem that has a dictionary called secrets with
"ssid" and
# "password" keys with your WiFi credentials. DO NOT share that file or commit it
into Git or other
# source control.
# pylint: disable=no-name-in-module,wrong-import-order
try
:
from
secrets
import
secrets
except
ImportError
:
(
"WiFi secrets are kept in secrets.py, please add them there!"
)
raise
# If you are using a board with pre-defined ESP32 Pins:
esp32_cs
=
DigitalInOut(board.ESP_CS)
esp32_ready
=
DigitalInOut(board.ESP_BUSY)
esp32_reset
=
DigitalInOut(board.ESP_RESET)
# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)
spi
=
busio.SPI(board.SCK, board.MOSI, board.MISO)
esp
=
adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
(
"Connecting to AP..."
)
while
not
esp.is_connected:
try
:
esp.connect_AP(secrets[
"ssid"
], secrets[
"password"
])
except
RuntimeError
as
e:
(
"could not connect to AP, retrying: "
, e)
continue
(
"Connected to"
,
str
(esp.ssid,
"utf-8"
),
"\tRSSI:"
, esp.rssi)
# Initialize a requests object with a socket and esp32spi interface
socket.set_interface(esp)
requests.set_socket(socket, esp)
JSON_GET_URL
=
"http://httpbin.org/get"
# Define a custom header as a dict.
headers
=
{
"user-agent"
:
"blinka/1.0.0"
}
(
"Fetching JSON data from %s..."
%
JSON_GET_URL)
response
=
requests.get(JSON_GET_URL, headers
=
headers)
(
"-"
*
60
)
json_data
=
response.json()
headers
=
json_data[
"headers"
]
(
"Response's Custom User-Agent Header: {0}"
.
format
(headers[
"User-Agent"
]))
(
"-"
*
60
)
# Read Response's HTTP status code
(
"Response HTTP Status Code: "
, response.status_code)
(
"-"
*
60
)
# Close, delete and collect the response data
response.close()
©Adafruit Industries
Page 24 of 54