
Requests
We've written a
library for web interfacing named
dafruit_CircuitPython_Requests
. This library allows you to send
HTTP/1.1 requests without "crafting" them and provides helpful methods for parsing
the response from the server.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# adafruit_requests usage with an esp32spi_socket
import
board
import
busio
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)
# If you have an AirLift Featherwing or ItsyBitsy Airlift:
# esp32_cs = DigitalInOut(board.D13)
# esp32_ready = DigitalInOut(board.D11)
# esp32_reset = DigitalInOut(board.D12)
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)
TEXT_URL
=
"http://wifitest.adafruit.com/testwifi/index.html"
©Adafruit Industries
Page 20 of 54