|
| 1 | +import board |
| 2 | +import busio |
| 3 | +from digitalio import DigitalInOut |
| 4 | +from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K |
| 5 | +import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket |
| 6 | +import adafruit_requests as requests |
| 7 | + |
| 8 | +cs = DigitalInOut(board.D10) |
| 9 | +spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) |
| 10 | + |
| 11 | +# Initialize ethernet interface with DHCP |
| 12 | +eth = WIZNET5K(spi_bus, cs) |
| 13 | + |
| 14 | +# Initialize a requests object with a socket and ethernet interface |
| 15 | +requests.set_socket(socket, eth) |
| 16 | + |
| 17 | +TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" |
| 18 | +JSON_GET_URL = "http://httpbin.org/get" |
| 19 | +JSON_POST_URL = "http://httpbin.org/post" |
| 20 | + |
| 21 | +attempts = 3 # Number of attempts to retry each request |
| 22 | +failure_count = 0 |
| 23 | +response = None |
| 24 | + |
| 25 | +print("Fetching text from %s"%TEXT_URL) |
| 26 | +while not response: |
| 27 | + try: |
| 28 | + response = requests.get(TEXT_URL) |
| 29 | + failure_count = 0 |
| 30 | + except AssertionError as error: |
| 31 | + print("Request failed, retrying...\n", error) |
| 32 | + failure_count += 1 |
| 33 | + if failure_count >= attempts: |
| 34 | + raise AssertionError("Failed to resolve hostname, \ |
| 35 | + please check your router's DNS configuration.") |
| 36 | + continue |
| 37 | +print('-'*40) |
| 38 | + |
| 39 | +print("Text Response: ", response.text) |
| 40 | +print('-'*40) |
| 41 | +response.close() |
| 42 | +response = None |
| 43 | + |
| 44 | +print("Fetching JSON data from %s"%JSON_GET_URL) |
| 45 | +while not response: |
| 46 | + try: |
| 47 | + response = requests.get(JSON_GET_URL) |
| 48 | + failure_count = 0 |
| 49 | + except AssertionError as error: |
| 50 | + print("Request failed, retrying...\n", error) |
| 51 | + failure_count += 1 |
| 52 | + if failure_count >= attempts: |
| 53 | + raise AssertionError("Failed to resolve hostname, \ |
| 54 | + please check your router's DNS configuration.") |
| 55 | + continue |
| 56 | +print('-'*40) |
| 57 | + |
| 58 | +print("JSON Response: ", response.json()) |
| 59 | +print('-'*40) |
| 60 | +response.close() |
| 61 | +response = None |
| 62 | + |
| 63 | +data = '31F' |
| 64 | +print("POSTing data to {0}: {1}".format(JSON_POST_URL, data)) |
| 65 | +while not response: |
| 66 | + try: |
| 67 | + response = requests.post(JSON_POST_URL, data=data) |
| 68 | + failure_count = 0 |
| 69 | + except AssertionError as error: |
| 70 | + print("Request failed, retrying...\n", error) |
| 71 | + failure_count += 1 |
| 72 | + if failure_count >= attempts: |
| 73 | + raise AssertionError("Failed to resolve hostname, \ |
| 74 | + please check your router's DNS configuration.") |
| 75 | + continue |
| 76 | +print('-'*40) |
| 77 | + |
| 78 | +json_resp = response.json() |
| 79 | +# Parse out the 'data' key from json_resp dict. |
| 80 | +print("Data received from server:", json_resp['data']) |
| 81 | +print('-'*40) |
| 82 | +response.close() |
| 83 | +response = None |
| 84 | + |
| 85 | +json_data = {"Date" : "July 25, 2019"} |
| 86 | +print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data)) |
| 87 | +while not response: |
| 88 | + try: |
| 89 | + response = requests.post(JSON_POST_URL, json=json_data) |
| 90 | + failure_count = 0 |
| 91 | + except AssertionError as error: |
| 92 | + print("Request failed, retrying...\n", error) |
| 93 | + failure_count += 1 |
| 94 | + if failure_count >= attempts: |
| 95 | + raise AssertionError("Failed to resolve hostname, \ |
| 96 | + please check your router's DNS configuration.") |
| 97 | + continue |
| 98 | +print('-'*40) |
| 99 | + |
| 100 | +json_resp = response.json() |
| 101 | +# Parse out the 'json' key from json_resp dict. |
| 102 | +print("JSON Data received from server:", json_resp['json']) |
| 103 | +print('-'*40) |
| 104 | +response.close() |
0 commit comments