|
2 | 2 | # |
3 | 3 | # SPDX-License-Identifier: MIT |
4 | 4 |
|
| 5 | +import os |
5 | 6 | import ipaddress |
6 | 7 | import ssl |
7 | 8 | import wifi |
|
13 | 14 | JSON_QUOTES_URL = "https://www.adafruit.com/api/quotes.php" |
14 | 15 | JSON_STARS_URL = "https://api.github.com/repos/adafruit/circuitpython" |
15 | 16 |
|
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 | | - |
23 | 17 | print("ESP32-S2 WebClient Test") |
24 | 18 |
|
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]}") |
26 | 20 |
|
27 | 21 | print("Available WiFi networks:") |
28 | 22 | for network in wifi.radio.start_scanning_networks(): |
29 | 23 | print("\t%s\t\tRSSI: %d\tChannel: %d" % (str(network.ssid, "utf-8"), |
30 | | - network.rssi, network.channel)) |
| 24 | + network.rssi, network.channel)) |
31 | 25 | wifi.radio.stop_scanning_networks() |
32 | 26 |
|
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}") |
37 | 31 |
|
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") |
40 | 39 |
|
41 | 40 | pool = socketpool.SocketPool(wifi.radio) |
42 | 41 | requests = adafruit_requests.Session(pool, ssl.create_default_context()) |
43 | 42 |
|
44 | | -print("Fetching text from", TEXT_URL) |
| 43 | +print(f"Fetching text from {TEXT_URL}") |
45 | 44 | response = requests.get(TEXT_URL) |
46 | 45 | print("-" * 40) |
47 | 46 | print(response.text) |
48 | 47 | print("-" * 40) |
49 | 48 |
|
50 | | -print("Fetching json from", JSON_QUOTES_URL) |
| 49 | +print(f"Fetching json from {JSON_QUOTES_URL}") |
51 | 50 | response = requests.get(JSON_QUOTES_URL) |
52 | 51 | print("-" * 40) |
53 | 52 | print(response.json()) |
54 | 53 | print("-" * 40) |
55 | 54 |
|
56 | 55 | print() |
57 | 56 |
|
58 | | -print("Fetching and parsing json from", JSON_STARS_URL) |
| 57 | +print(f"Fetching and parsing json from {JSON_STARS_URL}") |
59 | 58 | response = requests.get(JSON_STARS_URL) |
60 | 59 | print("-" * 40) |
61 | | -print("CircuitPython GitHub Stars", response.json()["stargazers_count"]) |
| 60 | +print(f"CircuitPython GitHub Stars: {response.json()['stargazers_count']}") |
62 | 61 | print("-" * 40) |
63 | 62 |
|
64 | | -print("done") |
| 63 | +print("Done") |
0 commit comments