|
| 1 | +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import adafruit_connection_manager |
| 5 | +import board |
| 6 | +import busio |
| 7 | +from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K |
| 8 | +from digitalio import DigitalInOut |
| 9 | +import ssl |
| 10 | +import adafruit_requests |
| 11 | + |
| 12 | +if hasattr(board, 'W5500_CS'): |
| 13 | + # Wiznet W5500-EVB-Pico |
| 14 | + cs = DigitalInOut(board.W5500_CS) |
| 15 | + reset = DigitalInOut(board.W5500_RST) |
| 16 | +else: |
| 17 | + # Adafruit Ethernet Featherwing |
| 18 | + cs = DigitalInOut(board.D10) |
| 19 | + reset = None |
| 20 | + |
| 21 | +spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) |
| 22 | + |
| 23 | +# Initialize ethernet interface with DHCP |
| 24 | +radio = WIZNET5K(spi_bus, cs, reset=reset) |
| 25 | + |
| 26 | +# Initialize a requests session |
| 27 | +pool = adafruit_connection_manager.get_radio_socketpool(radio) |
| 28 | +ssl_context = ssl.create_default_context() |
| 29 | +requests = adafruit_requests.Session(pool, ssl_context) |
| 30 | + |
| 31 | +TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" |
| 32 | +JSON_GET_URL = "http://httpbin.org/get" |
| 33 | +JSON_GET_URL = "https://httpbin.org/get" |
| 34 | +JSON_POST_URL = "https://httpbin.org/post" |
| 35 | + |
| 36 | +if __name__ == '__main__': |
| 37 | + print("Fetching text from %s" % TEXT_URL) |
| 38 | + with requests.get(TEXT_URL) as response: |
| 39 | + print("-" * 40) |
| 40 | + |
| 41 | + print("Text Response: ", response.text) |
| 42 | + print("-" * 40) |
| 43 | + |
| 44 | + print("Fetching JSON data from %s" % JSON_GET_URL) |
| 45 | + with requests.get(JSON_GET_URL) as response: |
| 46 | + print(response) |
| 47 | + print("-" * 40) |
| 48 | + |
| 49 | + print("JSON Response: ", response.json()) |
| 50 | + print("-" * 40) |
| 51 | + |
| 52 | + data = "31F" |
| 53 | + print("POSTing data to {0}: {1}".format(JSON_POST_URL, data)) |
| 54 | + with requests.post(JSON_POST_URL, data=data) as response: |
| 55 | + print("-" * 40) |
| 56 | + |
| 57 | + json_resp = response.json() |
| 58 | + # Parse out the 'data' key from json_resp dict. |
| 59 | + print("Data received from server:", json_resp["data"]) |
| 60 | + print("-" * 40) |
| 61 | + |
| 62 | + json_data = {"Date": "July 25, 2019"} |
| 63 | + print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data)) |
| 64 | + with requests.post(JSON_POST_URL, json=json_data) as response: |
| 65 | + print("-" * 40) |
| 66 | + |
| 67 | + json_resp = response.json() |
| 68 | + # Parse out the 'json' key from json_resp dict. |
| 69 | + print("JSON Data received from server:", json_resp["json"]) |
| 70 | + print("-" * 40) |
0 commit comments