|
5 | 5 | Example to illustrate the device capability to get json data
|
6 | 6 | """
|
7 | 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 |
| 8 | +# NOTE: Make sure you've created your settings.toml file before running this example |
| 9 | +# https://learn.adafruit.com/adafruit-pyportal/create-your-settings-toml-file |
| 10 | +from os import getenv |
| 11 | + |
10 | 12 | import adafruit_connection_manager
|
11 | 13 | import adafruit_requests
|
12 | 14 | import board
|
13 | 15 | from adafruit_esp32spi import adafruit_esp32spi
|
14 | 16 | from digitalio import DigitalInOut
|
15 | 17 |
|
16 |
| -# Get wifi details and more from a secrets.py file |
17 |
| -try: |
18 |
| - from secrets import secrets |
19 |
| -except ImportError: |
20 |
| - print("WiFi secrets are kept in secrets.py, please add them there!") |
21 |
| - raise |
| 18 | +# Get wifi details and more from a settings.toml file |
| 19 | +# tokens used by this Demo: CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD |
| 20 | +ssid = getenv("CIRCUITPY_WIFI_SSID") |
| 21 | +password = getenv("CIRCUITPY_WIFI_PASSWORD") |
22 | 22 |
|
23 | 23 | print("ESP32 SPI webclient test")
|
24 | 24 |
|
25 | 25 | TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
|
26 |
| -JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json" |
| 26 | +JSON_URL = "http://wifitest.adafruit.com/testwifi/sample.json" |
27 | 27 |
|
28 | 28 |
|
29 | 29 | # ESP32 Pins:
|
|
41 | 41 | if esp.status == adafruit_esp32spi.WL_IDLE_STATUS:
|
42 | 42 | print("ESP32 found and in idle mode")
|
43 | 43 | print("Firmware vers.", esp.firmware_version)
|
44 |
| -print("MAC addr:", [hex(i) for i in esp.MAC_address]) |
| 44 | +print("MAC addr:", ":".join("%02X" % byte for byte in esp.MAC_address)) |
45 | 45 |
|
46 | 46 | for ap in esp.scan_networks():
|
47 |
| - print("\t%s\t\tRSSI: %d" % (str(ap["ssid"], "utf-8"), ap["rssi"])) |
| 47 | + print("\t%-23s RSSI: %d" % (ap.ssid, ap.rssi)) |
48 | 48 |
|
49 | 49 | print("Connecting to AP...")
|
50 | 50 | while not esp.is_connected:
|
51 | 51 | try:
|
52 |
| - esp.connect_AP(secrets["ssid"], secrets["password"]) |
| 52 | + esp.connect_AP(ssid, password) |
53 | 53 | except RuntimeError as e:
|
54 | 54 | print("could not connect to AP, retrying: ", e)
|
55 | 55 | continue
|
56 |
| -print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi) |
57 |
| -print("My IP address is", esp.pretty_ip(esp.ip_address)) |
| 56 | +print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi) |
| 57 | +print("My IP address is", esp.ipv4_address) |
58 | 58 | print("IP lookup adafruit.com: %s" % esp.pretty_ip(esp.get_host_by_name("adafruit.com")))
|
59 | 59 | print("Ping google.com: %d ms" % esp.ping("google.com"))
|
60 | 60 |
|
|
0 commit comments