本开发板使用的主控是ESP32-S3,ESP32的一大优点是内置WiFi功能。
由于WiFi名称与密码都是比较私密的文件,所以例程中创建了一个secret .py用于保存自定义数据,可以有效保护个人隐私。
教程中的secret .py包含如下信息:
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
}
就像C语言中创建一个类一样,如果你想增加一个条目就可以用例如'ssid' : 'home_wifi_network',这样添加,注意用(,)隔开。
首先进行wifi连接功能的测试:
在本次网络功能测试中,主要使用'ssid'和'password'。
代码如下:
import time
import board
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"
JSON_TIME_URL = "http://quan.suning.com/getSysTime.do"
# 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\n")
# 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()
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to {}!\n".format(secrets["ssid"]))
print("IP:", wifi.radio.ipv4_address)
print(f"My MAC address: {[hex(i) for i in wifi.radio.mac_address]}\n")
ping_ip = ipaddress.IPv4Address("8.8.8.8")
ping = wifi.radio.ping(ip=ping_ip) * 1000
if ping is not None:
print(f"Ping google.com: {ping} ms")
else:
ping = wifi.radio.ping(ip=ping_ip)
print(f"Ping google.com: {ping} ms")
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
time.sleep(6)
print(f"Fetching text from {TEXT_URL}")
response = requests.get(TEXT_URL)
print("-" * 40)
print(response.text)
print("-" * 40)
print()
time.sleep(6)
print(f"Fetching and parsing json from {JSON_TIME_URL}")
response = requests.get(JSON_TIME_URL)
print("-" * 40)
print(f"Time: {response.json()['sysTime2']}")
print("-" * 40)
print("Success")
while True:
response = requests.get(JSON_TIME_URL)
print(f"Time: {response.json()['sysTime2']}")
time.sleep(6)
效果如图:
下面进行创建wifi热点的测试:首先说一下失败了
代码如下:
import wifi
import adafruit_requests
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
print("ESP32-S3 AP Test")
print(f"My MAC address: {[hex(i) for i in wifi.radio.mac_address]}")
wifi.radio.start_ap(secrets["aio_username"], secrets["aio_key"])
现象是没有提示任何错误,不过并没有产生热点,通过查询circuitpython中有关wifi模块的操作,发现有关创建热点的操作是:
start_ap(ssid: str | circuitpython_typing.ReadableBuffer, password: str | circuitpython_typing.ReadableBuffer = b'', *, channel: int = 1, authmode: Iterable[AuthMode] = (), max_connections: int | None = 4)
相关参数能够理解但不知道怎么用,也是wifi.Radio下的,可是怎么写都是错误,希望大神指导!
问题已经解决:
加入以下代码就创建热点成功了
while True:
pass
应该是程序没有循环起来导致的。