
try:
esp.connect_AP(b'MY_SSID_NAME', b'MY_SSID_PASSWORD')
except RuntimeError as e:
print("could not connect to AP, retrying: ",e)
continue
print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi)
# Initialize a requests object with a socket and esp32spi interface
requests.set_socket(socket, esp)
HTTP GET with Requests
The code makes a HTTP GET request to Adafruit's WiFi testing website -
wifitest.adafruit.com/testwifi/index.html
.
To do this, we'll pass the URL into
requests.get()
. We're also going to save the
response from the server into a variable named
response
.
While we requested data from the server, we'd what the server responded with. Since
we already saved the server's
response
, we can read it back. Luckily for us, request
s automatically decodes the server's response into human-readable text, you can
read it back by calling
response.text
.
Lastly, we'll perform a bit of cleanup by calling
response.close()
. This closes,
deletes, and collect's the response's data.
print("Fetching text from %s"%TEXT_URL)
response = requests.get(TEXT_URL)
print('-'*40)
print("Text Response: ", response.text)
print('-'*40)
response.close()
While some servers respond with text, some respond with json-formatted data
consisting of attribute–value pairs.
CircuitPython_Requests can convert a JSON-formatted response from a server into a
CPython
dict.
object.
We can also fetch and parse json data. We'll send a HTTP get to a url we know returns
a json-formatted response (instead of text data).
Then, the code calls
response.json()
to convert the response to a CPython
dict
.
©Adafruit Industries
Page 22 of 54
Содержание Airlift Bitsy Add-On
Страница 4: ...Adafruit Industries Page 4 of 54...
Страница 7: ...Pinouts Adafruit Industries Page 7 of 54...
Страница 42: ...Adafruit Industries Page 42 of 54...
Страница 54: ...Fab Print Adafruit Industries Page 54 of 54...