|
6 | 6 | from adafruit_esp32spi import adafruit_esp32spi
|
7 | 7 | import adafruit_requests as requests
|
8 | 8 |
|
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 |
| - |
15 | 9 | # If you are using a board with pre-defined ESP32 Pins:
|
16 | 10 | esp32_cs = DigitalInOut(board.ESP_CS)
|
17 | 11 | esp32_ready = DigitalInOut(board.ESP_BUSY)
|
|
24 | 18 |
|
25 | 19 | spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
|
26 | 20 | esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
|
27 |
| -requests.set_socket(socket, esp) |
28 | 21 |
|
29 | 22 | print("Connecting to AP...")
|
30 | 23 | while not esp.is_connected:
|
|
35 | 28 | continue
|
36 | 29 | print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi)
|
37 | 30 |
|
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) |
41 | 40 | print('-'*40)
|
42 |
| -print(r.text) |
| 41 | + |
| 42 | +print("Text Response: ", response.text) |
43 | 43 | print('-'*40)
|
44 |
| -r.close() |
| 44 | +response.close() |
45 | 45 |
|
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) |
49 | 48 | print('-'*40)
|
50 |
| -print(r.json()) |
| 49 | + |
| 50 | +print("JSON Response: ", response.json()) |
51 | 51 | print('-'*40)
|
52 |
| -r.close() |
| 52 | +response.close() |
53 | 53 |
|
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