|
| 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_requests as requests |
| 8 | +import adafruit_esp32spi.adafruit_esp32spi_socket as socket |
| 9 | +from adafruit_esp32spi import adafruit_esp32spi |
| 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("Raspberry Pi RP2040 - 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 | +# Raspberry Pi RP2040 Pinout |
| 24 | +esp32_cs = DigitalInOut(board.GP13) |
| 25 | +esp32_ready = DigitalInOut(board.GP14) |
| 26 | +esp32_reset = DigitalInOut(board.GP15) |
| 27 | + |
| 28 | +spi = busio.SPI(board.GP10, board.GP11, board.GP12) |
| 29 | +esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
| 30 | + |
| 31 | +requests.set_socket(socket, esp) |
| 32 | + |
| 33 | +if esp.status == adafruit_esp32spi.WL_IDLE_STATUS: |
| 34 | + print("ESP32 found and in idle mode") |
| 35 | +print("Firmware vers.", esp.firmware_version) |
| 36 | +print("MAC addr:", [hex(i) for i in esp.MAC_address]) |
| 37 | + |
| 38 | +for ap in esp.scan_networks(): |
| 39 | + print("\t%s\t\tRSSI: %d" % (str(ap["ssid"], "utf-8"), ap["rssi"])) |
| 40 | + |
| 41 | +print("Connecting to AP...") |
| 42 | +while not esp.is_connected: |
| 43 | + try: |
| 44 | + esp.connect_AP(secrets["ssid"], secrets["password"]) |
| 45 | + except RuntimeError as e: |
| 46 | + print("could not connect to AP, retrying: ", e) |
| 47 | + continue |
| 48 | +print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi) |
| 49 | +print("My IP address is", esp.pretty_ip(esp.ip_address)) |
| 50 | +print( |
| 51 | + "IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com")) |
| 52 | +) |
| 53 | +print("Ping google.com: %d ms" % esp.ping("google.com")) |
| 54 | + |
| 55 | +# esp._debug = True |
| 56 | +print("Fetching text from", TEXT_URL) |
| 57 | +r = requests.get(TEXT_URL) |
| 58 | +print("-" * 40) |
| 59 | +print(r.text) |
| 60 | +print("-" * 40) |
| 61 | +r.close() |
| 62 | + |
| 63 | +print() |
| 64 | +print("Fetching json from", JSON_URL) |
| 65 | +r = requests.get(JSON_URL) |
| 66 | +print("-" * 40) |
| 67 | +print(r.json()) |
| 68 | +print("-" * 40) |
| 69 | +r.close() |
| 70 | + |
| 71 | +print("Done!") |
0 commit comments