/ examples / esp32spi_simpletest.py
esp32spi_simpletest.py
 1  # SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries
 2  # SPDX-License-Identifier: MIT
 3  
 4  import board
 5  import busio
 6  from digitalio import DigitalInOut
 7  import adafruit_esp32spi.adafruit_esp32spi_socket as socket
 8  from adafruit_esp32spi import adafruit_esp32spi
 9  import adafruit_requests as requests
10  
11  # Get wifi details and more from a secrets.py file
12  try:
13      from secrets import secrets
14  except ImportError:
15      print("WiFi secrets are kept in secrets.py, please add them there!")
16      raise
17  
18  print("ESP32 SPI webclient test")
19  
20  TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
21  JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json"
22  
23  
24  # If you are using a board with pre-defined ESP32 Pins:
25  esp32_cs = DigitalInOut(board.ESP_CS)
26  esp32_ready = DigitalInOut(board.ESP_BUSY)
27  esp32_reset = DigitalInOut(board.ESP_RESET)
28  
29  # If you have an AirLift Shield:
30  # esp32_cs = DigitalInOut(board.D10)
31  # esp32_ready = DigitalInOut(board.D7)
32  # esp32_reset = DigitalInOut(board.D5)
33  
34  # If you have an AirLift Featherwing or ItsyBitsy Airlift:
35  # esp32_cs = DigitalInOut(board.D13)
36  # esp32_ready = DigitalInOut(board.D11)
37  # esp32_reset = DigitalInOut(board.D12)
38  
39  # If you have an externally connected ESP32:
40  # NOTE: You may need to change the pins to reflect your wiring
41  # esp32_cs = DigitalInOut(board.D9)
42  # esp32_ready = DigitalInOut(board.D10)
43  # esp32_reset = DigitalInOut(board.D5)
44  
45  spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
46  esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
47  
48  requests.set_socket(socket, esp)
49  
50  if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
51      print("ESP32 found and in idle mode")
52  print("Firmware vers.", esp.firmware_version)
53  print("MAC addr:", [hex(i) for i in esp.MAC_address])
54  
55  for ap in esp.scan_networks():
56      print("\t%s\t\tRSSI: %d" % (str(ap["ssid"], "utf-8"), ap["rssi"]))
57  
58  print("Connecting to AP...")
59  while not esp.is_connected:
60      try:
61          esp.connect_AP(secrets["ssid"], secrets["password"])
62      except RuntimeError as e:
63          print("could not connect to AP, retrying: ", e)
64          continue
65  print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)
66  print("My IP address is", esp.pretty_ip(esp.ip_address))
67  print(
68      "IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com"))
69  )
70  print("Ping google.com: %d ms" % esp.ping("google.com"))
71  
72  # esp._debug = True
73  print("Fetching text from", TEXT_URL)
74  r = requests.get(TEXT_URL)
75  print("-" * 40)
76  print(r.text)
77  print("-" * 40)
78  r.close()
79  
80  print()
81  print("Fetching json from", JSON_URL)
82  r = requests.get(JSON_URL)
83  print("-" * 40)
84  print(r.json())
85  print("-" * 40)
86  r.close()
87  
88  print("Done!")