/ examples / requests_simpletest.py
requests_simpletest.py
 1  # adafruit_requests usage with an esp32spi_socket
 2  import board
 3  import busio
 4  from digitalio import DigitalInOut
 5  import adafruit_esp32spi.adafruit_esp32spi_socket as socket
 6  from adafruit_esp32spi import adafruit_esp32spi
 7  import adafruit_requests as requests
 8  
 9  # Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
10  # "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
11  # source control.
12  # pylint: disable=no-name-in-module,wrong-import-order
13  try:
14      from secrets import secrets
15  except ImportError:
16      print("WiFi secrets are kept in secrets.py, please add them there!")
17      raise
18  
19  # If you are using a board with pre-defined ESP32 Pins:
20  esp32_cs = DigitalInOut(board.ESP_CS)
21  esp32_ready = DigitalInOut(board.ESP_BUSY)
22  esp32_reset = DigitalInOut(board.ESP_RESET)
23  
24  # If you have an externally connected ESP32:
25  # esp32_cs = DigitalInOut(board.D9)
26  # esp32_ready = DigitalInOut(board.D10)
27  # esp32_reset = DigitalInOut(board.D5)
28  
29  spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
30  esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
31  
32  print("Connecting to AP...")
33  while not esp.is_connected:
34      try:
35          esp.connect_AP(secrets["ssid"], secrets["password"])
36      except RuntimeError as e:
37          print("could not connect to AP, retrying: ", e)
38          continue
39  print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
40  
41  # Initialize a requests object with a socket and esp32spi interface
42  socket.set_interface(esp)
43  requests.set_socket(socket)
44  
45  TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
46  JSON_GET_URL = "http://httpbin.org/get"
47  JSON_POST_URL = "http://httpbin.org/post"
48  
49  print("Fetching text from %s" % TEXT_URL)
50  response = requests.get(TEXT_URL)
51  print("-" * 40)
52  
53  print("Text Response: ", response.text)
54  print("-" * 40)
55  response.close()
56  
57  print("Fetching JSON data from %s" % JSON_GET_URL)
58  response = requests.get(JSON_GET_URL)
59  print("-" * 40)
60  
61  print("JSON Response: ", response.json())
62  print("-" * 40)
63  response.close()
64  
65  data = "31F"
66  print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
67  response = requests.post(JSON_POST_URL, data=data)
68  print("-" * 40)
69  
70  json_resp = response.json()
71  # Parse out the 'data' key from json_resp dict.
72  print("Data received from server:", json_resp["data"])
73  print("-" * 40)
74  response.close()
75  
76  json_data = {"Date": "July 25, 2019"}
77  print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
78  response = requests.post(JSON_POST_URL, json=json_data)
79  print("-" * 40)
80  
81  json_resp = response.json()
82  # Parse out the 'json' key from json_resp dict.
83  print("JSON Data received from server:", json_resp["json"])
84  print("-" * 40)
85  response.close()