import board
import displayio
from adafruit_display_text import label, wrap_text_to_lines
from adafruit_bitmap_font import bitmap_font
# 设置要显示的字符串
dis_str = "很荣欣参\n加这次活动"
def screen_dispstr(str):
# 根据屏幕旋转角度确定每行显示的字符数
if board.DISPLAY.rotation % 180 == 0:
char_num = 23 # 横屏
else:
char_num = 13 # 竖屏
strbuf = ""
for i in range(len(str) / char_num):
# 将字符串按每行字符数拆分,并添加换行符
strbuf = strbuf + str[i * char_num:(i + 1) * char_num] + "\n"
return strbuf
# 初始化显示屏
display = board.DISPLAY
board.DISPLAY.brightness = 0.35
board.DISPLAY.rotation = 0
# 加载字体和设置颜色
font = bitmap_font.load_font("wenquanyi_10pt.pcf")
color = 0x00FFFF
# 创建显示文本的组和标签
text_group = displayio.Group()
text_area = label.Label(font, text=screen_dispstr(dis_str), color=color)
text_area.x = 30
text_area.y = 20
text_area.line_spacing = 1
text_area.scale = 3
text_group.append(text_area)
display.show(text_group)
# 持续显示
while True:
pass
import os
import wifi
import adafruit_requests
print("ESP32-S3 Access Point Test")
print(f"My MAC address: {[hex(i) for i in wifi.radio.mac_address]}")
wifi.radio.start_ap(os.getenv("AP_SSID"), os.getenv("AP_PASSWORD"))
print(f"My SSID: {"esp32"}")
print(f"My PASSWORD: {"987654321"}")
while True:
pass
import time
import board
import digitalio
import neopixel
RAINBOW_COLORS = [
(255, 0, 0), # 红
(255, 165, 0), # 橙
(255, 255, 0), # 黄
(0, 255, 0), # 绿
(0, 0, 255), # 蓝
(75, 0, 130), # 靛蓝
(238, 130, 238) # 紫
]
#初始为红
Color = RAINBOW_COLORS[0]
# 初始化btn按钮对象
button = digitalio.DigitalInOut(board.BOOT0)
# 设置btn引脚为输入
button.direction = digitalio.Direction.INPUT
# 设置btn引脚为上拉
button.pull = digitalio.Pull.UP
debounce_delay = 0.1 # 消抖延迟,单位为秒
button_pressed = False
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
pixel.brightness = 0.3
while True:
if not button.value and not button_pressed: # 按键被按下且之前未被按下
button_pressed = True
time.sleep(debounce_delay) # 延迟一段时间进行消抖
if not button.value: # 确认按键仍然被按下
Color = RAINBOW_COLORS[Color_Num]
# 设置led颜色
pixel.fill(Color)
Color_Num += 1
if Color_Num == 7:
Color_Num -= 7
elif button.value: # 按键未被按下
button_pressed = False
import board
# 导入displayio库(内置的)
import displayio
# 导入外部库adafruit_imageload,如果没有就在教程附件下载
import adafruit_imageload
# 导入外部库adafruit_display_text里的lable,用于显示标签
from adafruit_display_text import label
# 导入外部库adafruit_bitmap_font里的lable
from adafruit_bitmap_font import bitmap_font#用于显示字体
# 导入os库,用来获取wifi信息
import os
# 导入rtc库,实现RTC时钟
import rtc
# 导入wifi、time库备用
import wifi
import time
# 导入网络库备用
import ssl
import socketpool
import adafruit_ntp
import adafruit_requests
# 使用固件自带的屏幕设备,不需要另行初始化屏幕参数
display = board.DISPLAY
group = displayio.Group()
# 加载图片
image, palette = adafruit_imageload.load("/pic/pic.png")
# 是否开启透明
palette.make_transparent(0)
# 创建图片布局
grid = displayio.TileGrid(image, pixel_shader=palette)
# 将图片布局添加到图像组,由于是第一个添加的,默认是最下层
group.append(grid)
# 加载字体并定义字体颜色为黑色
font = bitmap_font.load_font("/font/wenquanyi_13px.pcf")
nun_font = bitmap_font.load_font("/font/wenquanyi_13px.pcf")
color = 0x000000
# 初始化日期标签并设置x,y轴绘图坐标,然后将标签添加到图像组
date = label.Label(font, text="11月7日", color=color,scale=2)
date.x = 20
date.y = 25
group.append(date)
# 初始化地点标签并设置x,y轴绘图坐标,然后将标签添加到图像组
location = label.Label(font, text="孝感市孝南区", color=color,scale=2)
location.x = 20
location.y = 50
group.append(location)
# 初始化星期标签并设置x,y轴绘图坐标,然后将标签添加到图像组
week = label.Label(font, text="周二", color=color,scale=2)
week.x = 20
week.y = 75
group.append(week)
# 初始化温度标签并设置x,y轴绘图坐标,然后将标签添加到图像组
temp = label.Label(font, text="1°", color=color,scale=2)
temp.x = 20
temp.y = 100
group.append(temp)
# 初始化时间标签并设置x,y轴绘图坐标,然后将标签添加到图像组
timeL = label.Label(font, text="22:20", color=color,scale=2)
timeL.x = 60
timeL.y = 100
group.append(timeL)
# 初始化天气标签并设置x,y轴绘图坐标,然后将标签添加到图像组
tempzh = label.Label(font, text="晴", color=color,scale=2)
tempzh.x = 150
tempzh.y = 100
group.append(tempzh)
# 显示修改后的图像组
display.show(group)
# 使用os.getenv函数,从setting.toml文件里获取wifi ssid和密码
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
# 连接到 wifi
print("Connecting to", ssid)
wifi.radio.connect(ssid, password)
print("Connected to", ssid)
# 使用adafruit_ntp.NTP函数初始化ntp服务,第一个函数是确定网络连接端口,
# 第二个函数设置时区,中国是+8时区,第三个函数用来指定ntp服务器地址
pool = socketpool.SocketPool(wifi.radio)
ntp = adafruit_ntp.NTP(pool, tz_offset=8, server="ntp.aliyun.com")
# 使用ntp时间更新系统时间
rtc.RTC().datetime = ntp.datetime
def get_wday(wday):
if (wday == 0):
return "周一"
elif (wday == 1):
return "周二"
elif (wday == 2):
return "周三"
elif (wday == 3):
return "周四"
elif (wday == 4):
return "周五"
elif (wday == 5):
return "周六"
elif (wday == 6):
return "周日"
# 初始化requests对象
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
def get_weather():
# 设置城市id
city = "420902"
# 这个函数使用的是高德API,使用该API需要先去注册相关账户,申请key。
key = "83e63e24774f7cdcc123e664f1a017a3"
# 拼接天气链接url
getweather_url = "https://restapi.amap.com/v3/weather/weatherInfo?city=" + city + "&key=" + key
# 获取天气json数据
response = requests.get(getweather_url)
json_resp = response.json()
# 关闭连接
response.close()
# 解析json数据,并返回温度和天气信息
for da in json_resp["lives"]:
#print(da["temperature"])
return da["temperature"], da["weather"]
# 先创建一个status变量,用来在设备启动时获取天气信息
status = "boot"
# 主循环
while True:
# 每秒获取一次本地RTC时间
t = time.localtime()
# 首次启动或者本地RTC时间的分钟属性为0时,更新日期标签和天气标签
if (status == "boot" or t.tm_min == 0):
# 更新日期标签
date.text = "%d月%d日" % (t.tm_mon, t.tm_mday)
week.text = get_wday(t.tm_wday)
# 获取天气信息
str_t, str_tz = get_weather()
# 更新温度标签
temp.text = "%s°" % (str_t)
# 更新天气标签
tempzh.text = str_tz
status = "updated"
# 每隔1秒 更新一次时钟标签,用于动态显示
if (t.tm_sec % 2 == 0):
timeL.text = "%02d:%02d" % ( t.tm_hour, t.tm_min)
timeL.color = 0x000000
else:
timeL.text = "%02d:%02d" % ( t.tm_hour, t.tm_min)
timeL.color = 0xD9D7C9
# 刷新屏幕
display.show(group)
# 休眠1秒
time.sleep(1)