code.py
 1  # SPDX-FileCopyrightText: 2020 Brent Rubell for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import ipaddress
 6  import ssl
 7  import wifi
 8  import socketpool
 9  import adafruit_requests
10  
11  # URLs to fetch from
12  TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
13  JSON_QUOTES_URL = "https://www.adafruit.com/api/quotes.php"
14  JSON_STARS_URL = "https://api.github.com/repos/adafruit/circuitpython"
15  
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  print("ESP32-S2 WebClient Test")
24  
25  print("My MAC addr:", [hex(i) for i in wifi.radio.mac_address])
26  
27  print("Available WiFi networks:")
28  for network in wifi.radio.start_scanning_networks():
29      print("\t%s\t\tRSSI: %d\tChannel: %d" % (str(network.ssid, "utf-8"),
30              network.rssi, network.channel))
31  wifi.radio.stop_scanning_networks()
32  
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)
37  
38  ipv4 = ipaddress.ip_address("8.8.4.4")
39  print("Ping google.com: %f ms" % (wifi.radio.ping(ipv4)*1000))
40  
41  pool = socketpool.SocketPool(wifi.radio)
42  requests = adafruit_requests.Session(pool, ssl.create_default_context())
43  
44  print("Fetching text from", TEXT_URL)
45  response = requests.get(TEXT_URL)
46  print("-" * 40)
47  print(response.text)
48  print("-" * 40)
49  
50  print("Fetching json from", JSON_QUOTES_URL)
51  response = requests.get(JSON_QUOTES_URL)
52  print("-" * 40)
53  print(response.json())
54  print("-" * 40)
55  
56  print()
57  
58  print("Fetching and parsing json from", JSON_STARS_URL)
59  response = requests.get(JSON_STARS_URL)
60  print("-" * 40)
61  print("CircuitPython GitHub Stars", response.json()["stargazers_count"])
62  print("-" * 40)
63  
64  print("done")