Skip to content

Commit 96e8b34

Browse files
committed
Update demo, add settings.toml
1 parent 51a15ab commit 96e8b34

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

ESP32_S2_WiFi_Tests/CPy_Native_WiFi_Test/code.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5+
import os
56
import ipaddress
67
import ssl
78
import wifi
@@ -13,52 +14,50 @@
1314
JSON_QUOTES_URL = "https://www.adafruit.com/api/quotes.php"
1415
JSON_STARS_URL = "https://api.github.com/repos/adafruit/circuitpython"
1516

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
22-
2317
print("ESP32-S2 WebClient Test")
2418

25-
print("My MAC addr:", [hex(i) for i in wifi.radio.mac_address])
19+
print(f"My MAC address: {[hex(i) for i in wifi.radio.mac_address]}")
2620

2721
print("Available WiFi networks:")
2822
for network in wifi.radio.start_scanning_networks():
2923
print("\t%s\t\tRSSI: %d\tChannel: %d" % (str(network.ssid, "utf-8"),
30-
network.rssi, network.channel))
24+
network.rssi, network.channel))
3125
wifi.radio.stop_scanning_networks()
3226

33-
print("Connecting to %s"%secrets["ssid"])
34-
wifi.radio.connect(secrets["ssid"], secrets["password"])
35-
print("Connected to %s!"%secrets["ssid"])
36-
print("My IP address is", wifi.radio.ipv4_address)
27+
print(f"Connecting to {os.getenv('WIFI_SSID')}")
28+
wifi.radio.connect(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD"))
29+
print(f"Connected to {os.getenv('WIFI_SSID')}")
30+
print(f"My IP address: {wifi.radio.ipv4_address}")
3731

38-
ipv4 = ipaddress.ip_address("8.8.4.4")
39-
print("Ping google.com: %f ms" % (wifi.radio.ping(ipv4)*1000))
32+
ping_ip = ipaddress.IPv4Address("8.8.8.8")
33+
ping = wifi.radio.ping(ip=ping_ip) * 1000
34+
if ping is not None:
35+
print(f"Ping google.com: {ping} ms")
36+
else:
37+
ping = wifi.radio.ping(ip=ping_ip)
38+
print(f"Ping google.com: {ping} ms")
4039

4140
pool = socketpool.SocketPool(wifi.radio)
4241
requests = adafruit_requests.Session(pool, ssl.create_default_context())
4342

44-
print("Fetching text from", TEXT_URL)
43+
print(f"Fetching text from {TEXT_URL}")
4544
response = requests.get(TEXT_URL)
4645
print("-" * 40)
4746
print(response.text)
4847
print("-" * 40)
4948

50-
print("Fetching json from", JSON_QUOTES_URL)
49+
print(f"Fetching json from {JSON_QUOTES_URL}")
5150
response = requests.get(JSON_QUOTES_URL)
5251
print("-" * 40)
5352
print(response.json())
5453
print("-" * 40)
5554

5655
print()
5756

58-
print("Fetching and parsing json from", JSON_STARS_URL)
57+
print(f"Fetching and parsing json from {JSON_STARS_URL}")
5958
response = requests.get(JSON_STARS_URL)
6059
print("-" * 40)
61-
print("CircuitPython GitHub Stars", response.json()["stargazers_count"])
60+
print(f"CircuitPython GitHub Stars: {response.json()['stargazers_count']}")
6261
print("-" * 40)
6362

64-
print("done")
63+
print("Done")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-FileCopyrightText: 2023 Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
# This is where you store the credentials necessary for your code.
6+
# The associated demo only requires WiFi, but you can include any
7+
# credentials here, such as Adafruit IO username and key, etc.
8+
WIFI_SSID = "your-wifi-ssid"
9+
WIFI_PASSWORD = "your-wifi-password"

0 commit comments

Comments
 (0)