From c9c034603b8e304ae88af6f36f76f846ae0ffbeb Mon Sep 17 00:00:00 2001 From: lady ada Date: Sun, 20 Mar 2022 01:35:41 -0400 Subject: [PATCH] fix native wifi test code for bundler --- .../CPy_Native_WiFi_Test/code.py | 64 +++++++++++++++++++ .../CPy_Native_WiFi_Test/secrets.py | 14 ++++ 2 files changed, 78 insertions(+) create mode 100644 ESP32_S2_WiFi_Tests/CPy_Native_WiFi_Test/code.py create mode 100644 ESP32_S2_WiFi_Tests/CPy_Native_WiFi_Test/secrets.py diff --git a/ESP32_S2_WiFi_Tests/CPy_Native_WiFi_Test/code.py b/ESP32_S2_WiFi_Tests/CPy_Native_WiFi_Test/code.py new file mode 100644 index 000000000..d20135e84 --- /dev/null +++ b/ESP32_S2_WiFi_Tests/CPy_Native_WiFi_Test/code.py @@ -0,0 +1,64 @@ +# SPDX-FileCopyrightText: 2020 Brent Rubell for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +import ipaddress +import ssl +import wifi +import socketpool +import adafruit_requests + +# URLs to fetch from +TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" +JSON_QUOTES_URL = "https://www.adafruit.com/api/quotes.php" +JSON_STARS_URL = "https://api.github.com/repos/adafruit/circuitpython" + +# Get wifi details and more from a secrets.py file +try: + from secrets import secrets +except ImportError: + print("WiFi secrets are kept in secrets.py, please add them there!") + raise + +print("ESP32-S2 WebClient Test") + +print("My MAC addr:", [hex(i) for i in wifi.radio.mac_address]) + +print("Available WiFi networks:") +for network in wifi.radio.start_scanning_networks(): + print("\t%s\t\tRSSI: %d\tChannel: %d" % (str(network.ssid, "utf-8"), + network.rssi, network.channel)) +wifi.radio.stop_scanning_networks() + +print("Connecting to %s"%secrets["ssid"]) +wifi.radio.connect(secrets["ssid"], secrets["password"]) +print("Connected to %s!"%secrets["ssid"]) +print("My IP address is", wifi.radio.ipv4_address) + +ipv4 = ipaddress.ip_address("8.8.4.4") +print("Ping google.com: %f ms" % (wifi.radio.ping(ipv4)*1000)) + +pool = socketpool.SocketPool(wifi.radio) +requests = adafruit_requests.Session(pool, ssl.create_default_context()) + +print("Fetching text from", TEXT_URL) +response = requests.get(TEXT_URL) +print("-" * 40) +print(response.text) +print("-" * 40) + +print("Fetching json from", JSON_QUOTES_URL) +response = requests.get(JSON_QUOTES_URL) +print("-" * 40) +print(response.json()) +print("-" * 40) + +print() + +print("Fetching and parsing json from", JSON_STARS_URL) +response = requests.get(JSON_STARS_URL) +print("-" * 40) +print("CircuitPython GitHub Stars", response.json()["stargazers_count"]) +print("-" * 40) + +print("done") diff --git a/ESP32_S2_WiFi_Tests/CPy_Native_WiFi_Test/secrets.py b/ESP32_S2_WiFi_Tests/CPy_Native_WiFi_Test/secrets.py new file mode 100644 index 000000000..3b3f63d40 --- /dev/null +++ b/ESP32_S2_WiFi_Tests/CPy_Native_WiFi_Test/secrets.py @@ -0,0 +1,14 @@ +# SPDX-FileCopyrightText: 2020 Adafruit Industries +# +# SPDX-License-Identifier: Public Domain + +# This file is where you keep secret settings, passwords, and tokens! +# If you put them in the code you risk committing that info or sharing it + +secrets = { + 'ssid' : 'home_wifi_network', + 'password' : 'wifi_password', + 'aio_username' : 'my_adafruit_io_username', + 'aio_key' : 'my_adafruit_io_key', + 'timezone' : "America/New_York", # http://worldtimeapi.org/timezones + }