|
| 1 | +# SPDX-FileCopyrightText: 2019 ladyada for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +""" |
| 5 | +Example to illustrate the device capability to get json data |
| 6 | +""" |
| 7 | + |
| 8 | +# NOTE: Make sure you've created your secrets.py file before running this example |
| 9 | +# https://learn.adafruit.com/adafruit-pyportal/internet-connect#whats-a-secrets-file-17-2 |
| 10 | +import board |
| 11 | +from digitalio import DigitalInOut |
| 12 | +import adafruit_requests as requests |
| 13 | +import adafruit_esp32spi.adafruit_esp32spi_socket as socket |
| 14 | +from adafruit_esp32spi import adafruit_esp32spi |
| 15 | + |
| 16 | + |
| 17 | +# Get wifi details and more from a secrets.py file |
| 18 | +try: |
| 19 | + from secrets import secrets |
| 20 | +except ImportError: |
| 21 | + print("WiFi secrets are kept in secrets.py, please add them there!") |
| 22 | + raise |
| 23 | + |
| 24 | +print("ESP32 SPI webclient test") |
| 25 | + |
| 26 | +TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" |
| 27 | +JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json" |
| 28 | + |
| 29 | + |
| 30 | +# ESP32 Pins: |
| 31 | +esp32_cs = DigitalInOut(board.ESP_CS) |
| 32 | +esp32_ready = DigitalInOut(board.ESP_BUSY) |
| 33 | +esp32_reset = DigitalInOut(board.ESP_RESET) |
| 34 | + |
| 35 | +# SPI Configuration |
| 36 | +spi = board.SPI() |
| 37 | +esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
| 38 | +requests.set_socket(socket, esp) |
| 39 | + |
| 40 | +if esp.status == adafruit_esp32spi.WL_IDLE_STATUS: |
| 41 | + print("ESP32 found and in idle mode") |
| 42 | +print("Firmware vers.", esp.firmware_version) |
| 43 | +print("MAC addr:", [hex(i) for i in esp.MAC_address]) |
| 44 | + |
| 45 | +for ap in esp.scan_networks(): |
| 46 | + print("\t%s\t\tRSSI: %d" % (str(ap["ssid"], "utf-8"), ap["rssi"])) |
| 47 | + |
| 48 | +print("Connecting to AP...") |
| 49 | +while not esp.is_connected: |
| 50 | + try: |
| 51 | + esp.connect_AP(secrets["ssid"], secrets["password"]) |
| 52 | + except RuntimeError as e: |
| 53 | + print("could not connect to AP, retrying: ", e) |
| 54 | + continue |
| 55 | +print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi) |
| 56 | +print("My IP address is", esp.pretty_ip(esp.ip_address)) |
| 57 | +print( |
| 58 | + "IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com")) |
| 59 | +) |
| 60 | +print("Ping google.com: %d ms" % esp.ping("google.com")) |
| 61 | + |
| 62 | +# esp._debug = True |
| 63 | +print("Fetching text from", TEXT_URL) |
| 64 | +r = requests.get(TEXT_URL) |
| 65 | +print("-" * 40) |
| 66 | +print(r.text) |
| 67 | +print("-" * 40) |
| 68 | +r.close() |
| 69 | + |
| 70 | +print() |
| 71 | +print("Fetching json from", JSON_URL) |
| 72 | +r = requests.get(JSON_URL) |
| 73 | +print("-" * 40) |
| 74 | +print(r.json()) |
| 75 | +print("-" * 40) |
| 76 | +r.close() |
| 77 | +print("Done!") |
0 commit comments