Skip to content

Commit 37d277c

Browse files
authored
Merge pull request #3 from brentru/update-examples
Add/Update Examples
2 parents f4e0086 + 061a0dc commit 37d277c

File tree

2 files changed

+93
-18
lines changed

2 files changed

+93
-18
lines changed

examples/requests_advanced.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import board
2+
import busio
3+
from digitalio import DigitalInOut
4+
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
5+
from adafruit_esp32spi import adafruit_esp32spi
6+
import adafruit_requests as requests
7+
8+
# If you are using a board with pre-defined ESP32 Pins:
9+
esp32_cs = DigitalInOut(board.ESP_CS)
10+
esp32_ready = DigitalInOut(board.ESP_BUSY)
11+
esp32_reset = DigitalInOut(board.ESP_RESET)
12+
13+
# If you have an externally connected ESP32:
14+
# esp32_cs = DigitalInOut(board.D9)
15+
# esp32_ready = DigitalInOut(board.D10)
16+
# esp32_reset = DigitalInOut(board.D5)
17+
18+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
19+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
20+
21+
print("Connecting to AP...")
22+
while not esp.is_connected:
23+
try:
24+
esp.connect_AP(b'MY_SSID_NAME', b'MY_SSID_PASSWORD')
25+
except RuntimeError as e:
26+
print("could not connect to AP, retrying: ",e)
27+
continue
28+
print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi)
29+
30+
# Initialize a requests object with a socket and esp32spi interface
31+
requests.set_socket(socket, esp)
32+
33+
JSON_GET_URL = "http://httpbin.org/get"
34+
35+
# Define a custom header as a dict.
36+
headers = {"user-agent" : "blinka/1.0.0"}
37+
38+
print("Fetching JSON data from %s..."%JSON_GET_URL)
39+
response = requests.get(JSON_GET_URL, headers=headers)
40+
print('-'*60)
41+
42+
json_data = response.json()
43+
headers = json_data['headers']
44+
print("Response's Custom User-Agent Header: {0}".format(headers['User-Agent']))
45+
print('-'*60)
46+
47+
# Read Response's HTTP status code
48+
print("Response HTTP Status Code: ", response.status_code)
49+
print('-'*60)
50+
51+
# Read Response, as raw bytes instead of pretty text
52+
print("Raw Response: ", response.content)
53+
54+
# Close, delete and collect the response data
55+
response.close()

examples/requests_simpletest.py

+38-18
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
from adafruit_esp32spi import adafruit_esp32spi
77
import adafruit_requests as requests
88

9-
print("ESP32 SPI webclient test")
10-
11-
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
12-
JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json"
13-
14-
159
# If you are using a board with pre-defined ESP32 Pins:
1610
esp32_cs = DigitalInOut(board.ESP_CS)
1711
esp32_ready = DigitalInOut(board.ESP_BUSY)
@@ -24,7 +18,6 @@
2418

2519
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
2620
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
27-
requests.set_socket(socket, esp)
2821

2922
print("Connecting to AP...")
3023
while not esp.is_connected:
@@ -35,20 +28,47 @@
3528
continue
3629
print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi)
3730

38-
#esp._debug = True
39-
print("Fetching text from", TEXT_URL)
40-
r = requests.get(TEXT_URL)
31+
# Initialize a requests object with a socket and esp32spi interface
32+
requests.set_socket(socket, esp)
33+
34+
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
35+
JSON_GET_URL = "http://httpbin.org/get"
36+
JSON_POST_URL = "http://httpbin.org/post"
37+
38+
print("Fetching text from %s"%TEXT_URL)
39+
response = requests.get(TEXT_URL)
4140
print('-'*40)
42-
print(r.text)
41+
42+
print("Text Response: ", response.text)
4343
print('-'*40)
44-
r.close()
44+
response.close()
4545

46-
print()
47-
print("Fetching json from", JSON_URL)
48-
r = requests.get(JSON_URL)
46+
print("Fetching JSON data from %s"%JSON_GET_URL)
47+
response = requests.get(JSON_GET_URL)
4948
print('-'*40)
50-
print(r.json())
49+
50+
print("JSON Response: ", response.json())
5151
print('-'*40)
52-
r.close()
52+
response.close()
5353

54-
print("Done!")
54+
data = '31F'
55+
print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
56+
response = requests.post(JSON_POST_URL, data=data)
57+
print('-'*40)
58+
59+
json_resp = response.json()
60+
# Parse out the 'data' key from json_resp dict.
61+
print("Data received from server:", json_resp['data'])
62+
print('-'*40)
63+
response.close()
64+
65+
json_data = {"Date" : "July 25, 2019"}
66+
print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
67+
response = requests.post(JSON_POST_URL, json=json_data)
68+
print('-'*40)
69+
70+
json_resp = response.json()
71+
# Parse out the 'json' key from json_resp dict.
72+
print("JSON Data received from server:", json_resp['json'])
73+
print('-'*40)
74+
response.close()

0 commit comments

Comments
 (0)