【得捷电子Follow me第2期】 任务2:网络功能使用
[复制链接]
本帖最后由 knv 于 2023-10-9 22:47 编辑
【得捷电子Follow me第2期】 任务2:网络功能使用
0x0:创建一个 secrets.py文件用于保存wifi信息
内容以下
secrets = {
"ssid": "这是ssid",
"password": "这是密码",
"timezone": "Asia/Shanghai", # Check http://worldtimeapi.org/timezones
}
0x1 :链接wifi
链接wifi代码如下,其中使用 wifi.radio.connect 进行链接wifi。
import board
import displayio
from adafruit_display_text import label
import adafruit_bitmap_font.bitmap_font
import wifi
import time
import ssl
import socketpool
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])
display = board.DISPLAY
board.DISPLAY.brightness = 1
font = adafruit_bitmap_font.bitmap_font.load_font("mfyh-16.bdf")
text_group = displayio.Group()
text_area = label.Label(font, color=(255,255,255))
text_area.text=str(wifi.radio.ipv4_address)
text_area.x = 0
text_area.y = 20
text_group.append(text_area)
display.show(text_group)
运行结果如下:
0x1:创建一个AP,其中关键点就是使用 wifi.radio.start_ap 函数即可创建成功,非常简单
代码如下:
import board
import digitalio
import displayio
from adafruit_display_text import label, wrap_text_to_lines
from adafruit_bitmap_font import bitmap_font
import wifi
import os
import time
display = board.DISPLAY
board.DISPLAY.brightness = 0.35
board.DISPLAY.rotation = 0
font = bitmap_font.load_font("font/DingTalk_ncn_60.pcf")
color = 0xffffff
text_change = 0
text_group = displayio.Group()
text_area = label.Label(font, text="test", color=color)
text_area.x = 2
text_area.y = 50
text_area.line_spacing = 0.8
text_area.scale = 1
wifi.radio.start_ap('wifitest', '12345678')
text_area.text="running"
text_group.append(text_area)
display.show(text_group)
while True:
time.sleep(2)
运行结果:
以上,本次作业完毕。感谢观看
|