|
| 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 | +requests.set_socket(socket, esp) |
| 31 | + |
| 32 | +JSON_URL_GET = "http://httpbin.org/get" |
| 33 | +JSON_URL_POST = "http://httpbin.org/post" |
| 34 | + |
| 35 | +# Custom Header |
| 36 | +headers = {"user-agent" : "blinka/1.0.0"} |
| 37 | + |
| 38 | +print("Fetching JSON from %s"%JSON_URL_GET) |
| 39 | +response = requests.get(JSON_URL_GET, headers=headers) |
| 40 | +print('-'*40) |
| 41 | + |
| 42 | +json_data = response.json() |
| 43 | +print("JSON Response: ", json_data) |
| 44 | +print('-'*40) |
| 45 | + |
| 46 | +headers = json_data['headers'] |
| 47 | +print("Returned Custom User-Agent Header: {0}".format(headers['User-Agent'])) |
| 48 | +print('-'*40) |
| 49 | +# Close, delete and collect the response data |
| 50 | +response.close() |
| 51 | + |
| 52 | +# Let's POST some data! |
| 53 | + |
| 54 | +print("Posting data to %s..."%JSON_URL_POST) |
| 55 | +response = requests.post(JSON_URL_POST, data="hello server!") |
| 56 | +print('-'*40) |
| 57 | + |
| 58 | +json_data = response.json() |
| 59 | +print("JSON Response: ", json_data) |
| 60 | +print('-'*40) |
| 61 | +print("Data Returned: ", json_data['data']) |
| 62 | +print('-'*40) |
| 63 | +# Close, delete and collect the response data |
| 64 | +response.close() |
| 65 | + |
| 66 | +# Let's post some JSON data! |
| 67 | +print("Posting JSON data to %s..."%JSON_URL_POST) |
| 68 | +response = requests.post(JSON_URL_POST, json={"date" : "Jul 25, 2019"}) |
| 69 | +print('-'*40) |
| 70 | + |
| 71 | +json_data = response.json() |
| 72 | +print("JSON Response: ", json_data) |
| 73 | +print('-'*40) |
| 74 | +print("JSON Data Returned: ", json_data['json']) |
| 75 | +print('-'*40) |
| 76 | +# Close, delete and collect the response data |
| 77 | +response.close() |
0 commit comments