From ca5374f89e33fa7a28e8f70724b93ebbf1c1a19d Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 25 Jul 2019 16:15:30 -0400 Subject: [PATCH 1/5] add more robust requests test --- examples/requests_simpletest.py | 35 ++++++++++++++++----------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/examples/requests_simpletest.py b/examples/requests_simpletest.py index b598a20..ee4011e 100755 --- a/examples/requests_simpletest.py +++ b/examples/requests_simpletest.py @@ -6,12 +6,6 @@ from adafruit_esp32spi import adafruit_esp32spi import adafruit_requests as requests -print("ESP32 SPI webclient test") - -TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" -JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json" - - # If you are using a board with pre-defined ESP32 Pins: esp32_cs = DigitalInOut(board.ESP_CS) esp32_ready = DigitalInOut(board.ESP_BUSY) @@ -24,7 +18,6 @@ spi = busio.SPI(board.SCK, board.MOSI, board.MISO) esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) -requests.set_socket(socket, esp) print("Connecting to AP...") while not esp.is_connected: @@ -35,20 +28,26 @@ continue print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi) -#esp._debug = True -print("Fetching text from", TEXT_URL) -r = requests.get(TEXT_URL) +requests.set_socket(socket, esp) + +# Initialize a requests object with a socket and esp32spi interface +TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" + +print("Fetching text from %s"%TEXT_URL) +response = requests.get(TEXT_URL) +print('-'*40) + +print("Text Response: ", response.text) print('-'*40) -print(r.text) + +print("Raw Response, as bytes: {0}".format(response.content)) print('-'*40) -r.close() -print() -print("Fetching json from", JSON_URL) -r = requests.get(JSON_URL) +print("Response's HTTP Status Code: %d"%response.status_code) print('-'*40) -print(r.json()) + +print("Response's HTTP Headers: %s"%response.headers) print('-'*40) -r.close() -print("Done!") +# Close, delete and collect the response data +response.close() \ No newline at end of file From 5ca40ae93db5e1c7de1a6b7052c369b967020404 Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 25 Jul 2019 16:19:10 -0400 Subject: [PATCH 2/5] Add an example of using custom user agents, posting data, posting json data! --- examples/requests_json.py | 77 +++++++++++++++++++++++++++++++++ examples/requests_simpletest.py | 2 +- 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 examples/requests_json.py diff --git a/examples/requests_json.py b/examples/requests_json.py new file mode 100644 index 0000000..8e98abd --- /dev/null +++ b/examples/requests_json.py @@ -0,0 +1,77 @@ +import board +import busio +from digitalio import DigitalInOut +import adafruit_esp32spi.adafruit_esp32spi_socket as socket +from adafruit_esp32spi import adafruit_esp32spi +import adafruit_requests as requests + +# If you are using a board with pre-defined ESP32 Pins: +esp32_cs = DigitalInOut(board.ESP_CS) +esp32_ready = DigitalInOut(board.ESP_BUSY) +esp32_reset = DigitalInOut(board.ESP_RESET) + +# If you have an externally connected ESP32: +# esp32_cs = DigitalInOut(board.D9) +# esp32_ready = DigitalInOut(board.D10) +# esp32_reset = DigitalInOut(board.D5) + +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) +esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) + +print("Connecting to AP...") +while not esp.is_connected: + 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) + +requests.set_socket(socket, esp) + +JSON_URL_GET = "http://httpbin.org/get" +JSON_URL_POST = "http://httpbin.org/post" + +# Custom Header +headers = {"user-agent" : "blinka/1.0.0"} + +print("Fetching JSON from %s"%JSON_URL_GET) +response = requests.get(JSON_URL_GET, headers=headers) +print('-'*40) + +json_data = response.json() +print("JSON Response: ", json_data) +print('-'*40) + +headers = json_data['headers'] +print("Returned Custom User-Agent Header: {0}".format(headers['User-Agent'])) +print('-'*40) +# Close, delete and collect the response data +response.close() + +# Let's POST some data! + +print("Posting data to %s..."%JSON_URL_POST) +response = requests.post(JSON_URL_POST, data="hello server!") +print('-'*40) + +json_data = response.json() +print("JSON Response: ", json_data) +print('-'*40) +print("Data Returned: ", json_data['data']) +print('-'*40) +# Close, delete and collect the response data +response.close() + +# Let's post some JSON data! +print("Posting JSON data to %s..."%JSON_URL_POST) +response = requests.post(JSON_URL_POST, json={"date" : "Jul 25, 2019"}) +print('-'*40) + +json_data = response.json() +print("JSON Response: ", json_data) +print('-'*40) +print("JSON Data Returned: ", json_data['json']) +print('-'*40) +# Close, delete and collect the response data +response.close() diff --git a/examples/requests_simpletest.py b/examples/requests_simpletest.py index ee4011e..9cfe72a 100755 --- a/examples/requests_simpletest.py +++ b/examples/requests_simpletest.py @@ -50,4 +50,4 @@ print('-'*40) # Close, delete and collect the response data -response.close() \ No newline at end of file +response.close() From 86eb53080af9f583527a6a9015cc30f24220f9be Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 25 Jul 2019 16:40:06 -0400 Subject: [PATCH 3/5] add a better simpletest --- examples/requests_simpletest.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/examples/requests_simpletest.py b/examples/requests_simpletest.py index 9cfe72a..3e5ab7f 100755 --- a/examples/requests_simpletest.py +++ b/examples/requests_simpletest.py @@ -28,10 +28,12 @@ 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) -# Initialize a requests object with a socket and esp32spi interface TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" +JSON_GET_URL = "http://httpbin.org/get" +JSON_POST_URL = "http://httpbin.org/post" print("Fetching text from %s"%TEXT_URL) response = requests.get(TEXT_URL) @@ -39,15 +41,34 @@ print("Text Response: ", response.text) print('-'*40) +response.close() -print("Raw Response, as bytes: {0}".format(response.content)) +print("Fetching JSON data from %s"%JSON_GET_URL) +response = requests.get(JSON_GET_URL) print('-'*40) -print("Response's HTTP Status Code: %d"%response.status_code) +print("JSON Response: ", response.json()) print('-'*40) +response.close() -print("Response's HTTP Headers: %s"%response.headers) +data = '31F' +print("POSTing data to {0}: {1}".format(JSON_POST_URL, data)) +response = requests.post(JSON_POST_URL, data=data) print('-'*40) -# Close, delete and collect the response data +json_resp = response.json() +# Parse out the 'data' key from json_resp dict. +print("Data received from server:", json_resp['data']) +print('-'*40) response.close() + +json_data = {"Date" : "July 25, 2019"} +print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data)) +response = requests.post(JSON_POST_URL, json=json_data) +print('-'*40) + +json_resp = response.json() +# Parse out the 'json' key from json_resp dict. +print("JSON Data received from server:", json_resp['json']) +print('-'*40) +response.close() \ No newline at end of file From ad35f8bfbaa771fa06b5d3ffe5ad7c072f6b6bfa Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 25 Jul 2019 16:46:21 -0400 Subject: [PATCH 4/5] add an advanced usage example and lint --- examples/requests_advanced.py | 55 +++++++++++++++++++++++++++++++++ examples/requests_simpletest.py | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 examples/requests_advanced.py diff --git a/examples/requests_advanced.py b/examples/requests_advanced.py new file mode 100644 index 0000000..11401ba --- /dev/null +++ b/examples/requests_advanced.py @@ -0,0 +1,55 @@ +import board +import busio +from digitalio import DigitalInOut +import adafruit_esp32spi.adafruit_esp32spi_socket as socket +from adafruit_esp32spi import adafruit_esp32spi +import adafruit_requests as requests + +# If you are using a board with pre-defined ESP32 Pins: +esp32_cs = DigitalInOut(board.ESP_CS) +esp32_ready = DigitalInOut(board.ESP_BUSY) +esp32_reset = DigitalInOut(board.ESP_RESET) + +# If you have an externally connected ESP32: +# esp32_cs = DigitalInOut(board.D9) +# esp32_ready = DigitalInOut(board.D10) +# esp32_reset = DigitalInOut(board.D5) + +spi = busio.SPI(board.SCK, board.MOSI, board.MISO) +esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) + +print("Connecting to AP...") +while not esp.is_connected: + 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) + +JSON_GET_URL = "http://httpbin.org/get" + +# Define a custom header as a dict. +headers = {"user-agent" : "blinka/1.0.0"} + +print("Fetching JSON data from %s..."%JSON_GET_URL) +response = requests.get(JSON_GET_URL, headers=headers) +print('-'*60) + +json_data = response.json() +headers = json_data['headers'] +print("Response's Custom User-Agent Header: {0}".format(headers['User-Agent'])) +print('-'*60) + +# Read Response's HTTP status code +print("Response HTTP Status Code: ", response.status_code) +print('-'*60) + +# Read Response, as raw bytes instead of pretty text +print("Raw Response: ", response.content) + +# Close, delete and collect the response data +response.close() diff --git a/examples/requests_simpletest.py b/examples/requests_simpletest.py index 3e5ab7f..36d2b28 100755 --- a/examples/requests_simpletest.py +++ b/examples/requests_simpletest.py @@ -71,4 +71,4 @@ # Parse out the 'json' key from json_resp dict. print("JSON Data received from server:", json_resp['json']) print('-'*40) -response.close() \ No newline at end of file +response.close() From 061a0dc893ec389b737f2f061d523c6fb6c2411a Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 25 Jul 2019 16:47:15 -0400 Subject: [PATCH 5/5] remove requests json --- examples/requests_json.py | 77 --------------------------------------- 1 file changed, 77 deletions(-) delete mode 100644 examples/requests_json.py diff --git a/examples/requests_json.py b/examples/requests_json.py deleted file mode 100644 index 8e98abd..0000000 --- a/examples/requests_json.py +++ /dev/null @@ -1,77 +0,0 @@ -import board -import busio -from digitalio import DigitalInOut -import adafruit_esp32spi.adafruit_esp32spi_socket as socket -from adafruit_esp32spi import adafruit_esp32spi -import adafruit_requests as requests - -# If you are using a board with pre-defined ESP32 Pins: -esp32_cs = DigitalInOut(board.ESP_CS) -esp32_ready = DigitalInOut(board.ESP_BUSY) -esp32_reset = DigitalInOut(board.ESP_RESET) - -# If you have an externally connected ESP32: -# esp32_cs = DigitalInOut(board.D9) -# esp32_ready = DigitalInOut(board.D10) -# esp32_reset = DigitalInOut(board.D5) - -spi = busio.SPI(board.SCK, board.MOSI, board.MISO) -esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) - -print("Connecting to AP...") -while not esp.is_connected: - 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) - -requests.set_socket(socket, esp) - -JSON_URL_GET = "http://httpbin.org/get" -JSON_URL_POST = "http://httpbin.org/post" - -# Custom Header -headers = {"user-agent" : "blinka/1.0.0"} - -print("Fetching JSON from %s"%JSON_URL_GET) -response = requests.get(JSON_URL_GET, headers=headers) -print('-'*40) - -json_data = response.json() -print("JSON Response: ", json_data) -print('-'*40) - -headers = json_data['headers'] -print("Returned Custom User-Agent Header: {0}".format(headers['User-Agent'])) -print('-'*40) -# Close, delete and collect the response data -response.close() - -# Let's POST some data! - -print("Posting data to %s..."%JSON_URL_POST) -response = requests.post(JSON_URL_POST, data="hello server!") -print('-'*40) - -json_data = response.json() -print("JSON Response: ", json_data) -print('-'*40) -print("Data Returned: ", json_data['data']) -print('-'*40) -# Close, delete and collect the response data -response.close() - -# Let's post some JSON data! -print("Posting JSON data to %s..."%JSON_URL_POST) -response = requests.post(JSON_URL_POST, json={"date" : "Jul 25, 2019"}) -print('-'*40) - -json_data = response.json() -print("JSON Response: ", json_data) -print('-'*40) -print("JSON Data Returned: ", json_data['json']) -print('-'*40) -# Close, delete and collect the response data -response.close()