本帖最后由 qinyunti 于 2023-9-28 13:45 编辑
代码
见附件
download.eeworld.com.cn/detail/qinyunti/629280?type__2276=QqAxBD2DcDyitGNDQie0Kit3qqwxRlpYoD&alichlgref=http%3A%2F%2Fbbs.eeworld.com.cn%2Fmy%2Fhome.php%3Fcur%3Dmyhome%26act%3Ddownload
视频
见链接
https://training.eeworld.com.cn/uploadcourse/68108/lesson
https://www.bilibili.com/video/BV1Pp4y1c7vi/
资料准备
参考:https://cdn-learn.adafruit.com/downloads/pdf/adafruit-esp32-s3-tft-feather.pdf
开发环境搭建
下载固件
从如下地址下载UF2固件
https://circuitpython.org/board/adafruit_feather_esp32s3_tft/
接上USB线到电脑,上电
双击如下Rst按键(红色部分),可以看到绿色部分LED变为绿色。
显示如下
电脑识别移动存储如下
将下载的UF2文件拖到该盘
完成之后变为了CIRCUITPY
显示如下
安装编辑器
从如下地址下载
https://codewith.mu/
如果下载慢可以去https://ghproxy.com/输入链接下载
或者直接
安装没有什么特殊的直接安装即可。
编辑器使用
第一次启动要一会
选择CircuitPython->OK
点击新建
编辑代码添加一行
print("Hello World")
点击保存
保存为code.py,该名字的程序会自动运行
可以看到打印如下,没有显示REPL则点击串口按钮显示
任务1:控制屏幕显示中文(必做任务)
< class="p" style="">完成屏幕的控制,并且能显示中文
< class="p" style="">搭配器件:Adafruit ESP32-S3 TFT Feather
查看固件自带的库
< class="p" style="">help('modules')
自带的库就不需要再手动复制到lib目录下了。
下载库
从以下地址下载库
https://circuitpython.org/libraries
根据版本下载Bundle for Version 8.x
解压
复制adafruit_display_text,adafruit_bitmap_font到板子的lib目录下
下载中文字库
https://github.com/carrothu-cn/chinese-bitmap-fonts
如果下载慢可以先导入到gitee再下载。
复制wenquanyi_13px.pcf到板子的lib目录下
中文显示
输入如下代码,保存为code.py
# 导入库
import board
import displayio
from adafruit_display_text import label, wrap_text_to_lines
from adafruit_bitmap_font import bitmap_font
#初始化显示屏参数
display = board.DISPLAY
board.DISPLAY.brightness = 0.35
board.DISPLAY.rotation = 0
# 导入字库
font = bitmap_font.load_font("lib\wenquanyi_13px.pcf")
color = 0xFFFF00
#设定文本显示参数
text_group = displayio.Group()
text_area = label.Label(font, text="你好,Follow me!", color=color)
text_area.x = 1
text_area.y = 70
text_area.line_spacing = 0.8
text_area.scale = 2
#启动屏幕显示
text_group.append(text_area)
display.show(text_group)
while True:
pass
运行显示效果如下
任务2:网络功能使用(必做任务)
< class="p" style="">完成网络功能的使用,能够创建热点和连接到WiFi
< class="p" style="">搭配器件:Adafruit ESP32-S3 TFT Feather
< class="p" style="">参考 https://learn.adafruit.com/adafruit-funhouse/circuitpython-internet-test
WIFI连接
使用如下代码测试连接WIFI并ping同一网络的电脑,注意要关闭电脑防火墙。
import os
import ipaddress
import wifi
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("qiqiqiqi", "cqmygysdss")
ping_ip = ipaddress.IPv4Address("192.168.31.64")
print(wifi.radio.ipv4_address)
while True:
ping = wifi.radio.ping(ip=ping_ip, timeout=3)
if ping is not None:
print(f"192.168.31.64: {ping} ms")
电脑ping开发板
< class="p" style="">
WIFI热点
使用如下代码创建热点
import time
import ipaddress
import wifi
wifi.radio.start_ap(ssid='Adafruit ESP32-S3 TFT Feather', password='12345678', authmode=[wifi.AuthMode.WPA2, wifi.AuthMode.PSK])
wifi.radio.start_dhcp_ap()
while True:
time.sleep(1)
手机连接热点
任务3:控制WS2812B(必做任务)
< class="p" style="">使用按键控制板载Neopixel LED的显示和颜色切换
< class="p" style="">搭配器件:Adafruit ESP32-S3 TFT Feather
Neopixel LED颜色切换
复制neopixel.mpy到开发板的lib目录下
创建如下代码
import time
import board
import neopixel
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
pixel.brightness = 0.3
while True:
pixel.fill((255, 0, 0))
time.sleep(0.5)
pixel.fill((0, 255, 0))
time.sleep(0.5)
pixel.fill((0, 0, 255))
time.sleep(0.5)
运行可以看到LED三色闪烁。
按键切换颜色
import time
import board
import neopixel
import digitalio
button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)
button_buf = button.value
button_change = 0
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
pixel.brightness = 0.3
while True:
if ((button_buf != button.value) and (button.value==True)) :
button_change = button_change + 1
if(button_change == 1):
pixel.fill((255, 0, 0))
elif (button_change == 2):
pixel.fill((0, 255, 0))
elif (button_change == 3):
button_change = 0
pixel.fill((0, 0, 255))
button_buf = button.value
按BOOT按键切换颜色显示
任务4:
选择是分任务1:日历&时钟
获取网络时间
需要使用库
adafruit_ntp
将adafruit_ntp.mpy导入到lib下
测试代码如下
import adafruit_ntp
import rtc
def sync_rtc():
rtc.RTC().datetime = ntp.datetime
ntp = adafruit_ntp.NTP(pool, tz_offset=8)
secs = 1
while True:
secs = secs + 1
if secs > 5:
secs = 0
sync_rtc()
print(rtc.RTC()a.datetime)
time.sleep(1)
获取天气信息
心知天气,注册获取免费版本
获取密钥
API接口
https://api.seniverse.com/v3/weather/now.json?key=your_api_key&location=beijing&language=zh-Hans&unit=c
直接浏览器显示如下
添加库
代码如下
import os
import ipaddress
import ssl
import wifi
import socketpool
import adafruit_requests
import time
wifi.radio.connect(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD"))
print(f"Connected to {os.getenv('WIFI_SSID')}")
print(f"My IP address: {wifi.radio.ipv4_address}")
pool = socketpool.SocketPool(wifi.radio)
context = ssl.create_default_context()
context.check_hostname = False
requests = adafruit_requests.Session(pool, context)
while True:
print(f"Fetching text from {os.getenv("TEXT_URL")}")
response = requests.get(os.getenv("TEXT_URL"))
print("-" * 40)
print(response.text)
print("-" * 40)
time.sleep(5)
参数设置settings.toml
# SPDX-FileCopyrightText: 2023 Adafruit Industries
#
# SPDX-License-Identifier: MIT
# This is where you store the credentials necessary for your code.
# The associated demo only requires WiFi, but you can include any
# credentials here, such as Adafruit IO username and key, etc.
WIFI_SSID = "qiqiqiqi"
WIFI_PASSWORD = "cqmygysdss"
TEXT_URL = "http://api.seniverse.com/v3/weather/now.json?key=SCrUCunRlcvyvEuqG&location=changsha&language=zh-Hans&unit=c"
测试如下
天气信息和网络时间解析显示
def sync_weather():
print(f"Fetching text from {os.getenv("TEXT_URL")}")
response = requests.get(os.getenv("TEXT_URL"))
print("-" * 40)
print(response.text)
print("-" * 40)
obj = json.loads(response.text)
print(obj, type(obj))
temperature = obj['results'][0]['now']['temperature']
print(temperature, type(temperature))
weather = obj['results'][0]['now']['text']
print(weather, type(weather))
city = obj['results'][0]['location']['path']
print(city, type(city))
return temperature,weather,city
def disp_time_weather():
#显示日期
text_group = displayio.Group()
text_area = label.Label(font, text=str(datetime.tm_year)+"年"+str(datetime.tm_mon)+"月"+str(datetime.tm_mday)+"日"+" 星期"+str(datetime.tm_wday), color=color)
text_area.x = 50
text_area.y = 20
text_area.line_spacing = 0.8
text_area.scale = 1
text_group.append(text_area)
#display.show(text_group)
#显示时间
text_area1 = label.Label(font, text=str(datetime.tm_hour)+"时"+str(datetime.tm_min)+"分"+str(datetime.tm_sec)+"秒", color=color)
text_area1.x = 80
text_area1.y = 40
text_area1.line_spacing = 0.8
text_area1.scale = 1
text_group.append(text_area1)
print(datetime.tm_sec)
#显示城市
text_area2 = label.Label(font, text=city, color=color)
text_area2.x = 50
text_area2.y = 60
text_area2.line_spacing = 0.8
text_area2.scale = 1
text_group.append(text_area2)
print(city, type(city))
#显示天气
text_area3 = label.Label(font, text=weather, color=color)
text_area3.x = 100
text_area3.y = 80
text_area3.line_spacing = 0.8
text_area3.scale = 1
text_group.append(text_area3)
print(weather, type(weather))
#显示温度
text_area4 = label.Label(font, text=temperature+"度", color=color)
text_area4.x = 100
text_area4.y = 100
text_area4.line_spacing = 0.8
text_area4.scale = 1
text_group.append(text_area4)
print(temperature, type(temperature))
display.show(text_group)
总结
基于本次活动熟悉了基于Adafruit ESP32-S3 TFT Feather的circuitpyhon开发。
了解了中文显示,网络使用,WS2812B的控制等,并完成了时钟天气的Demo,可以通过网络获取时间天气显示。活动非常有意义,希望以后能更多的推出类似活动。
|