任务1:控制屏幕显示中文
在上一期的活动中使用的是micropython,这期活动使用的是其姐妹Circuit Python,两者都是python在微控制器上的轻量级实现。选择python的重要原因之一是它提供了常用外设模块、编程模块的驱动库,使用起来十分方便,解放用户折腾底层,专心做应用即可。
从Circuit Python官网可以下载到常用的库:
https://circuitpython.org/libraries
本次使用的是8.X版本的,下载该版本的Bundle即可,下载解压后可以看到如下目录:
为了方便使用,可以直接将lib目录复制到板卡的磁盘根目录里面,也可以只选择需要的库,要使用液晶显示中文,需要adafruit_bitmap_font和adafruit_display_text这2个库的支持,将它们拷贝到开发板磁盘:
显示中文,需要安装中文字库,参考网络大神们的设计,发现这个字库十分好用:
下载字库后拷贝wenquanyi_13pt.pcf文件到板卡的磁盘中。
测试代码如下:
import board
import digitalio
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("wenquanyi_13px.pcf")
color = 0xFF0000
text_change = 0
text_group = displayio.Group()
text_area = label.Label(font, text="test", color=color)
text_area.x = 2
text_area.y = 10
text_area.line_spacing = 0.8
text_area.scale = 2
text_area.text="中文测试\n电子工程世界"
text_group.append(text_area)
display.show(text_group)
while True:
pass
显示效果:
任务2:连接wifi和创建热点
circuitpython提供了配置文件settings.toml用于设置环境变量,创建wifi账号和密码的环境变量示例如下:
CIRCUITPY_WIFI_SSID = "your-ssid-here"
CIRCUITPY_WIFI_PASSWORD = "your-ssid-password-here"
连接wifi并将ip地址显示在屏幕上的测试代码如下:
import board
import digitalio
import displayio
from adafruit_display_text import label, wrap_text_to_lines
from adafruit_bitmap_font import bitmap_font
import os
import ipaddress
import wifi
import socketpool
import time
display = board.DISPLAY
board.DISPLAY.brightness = 0.35
board.DISPLAY.rotation = 0
font = bitmap_font.load_font("wenquanyi_13px.pcf")
color = 0xFF0000
text_change = 0
text_group = displayio.Group()
text_area = label.Label(font, text="test", color=color)
text_area.x = 2
text_area.y = 10
text_area.line_spacing = 0.8
text_area.scale = 2
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
text_area.text="ip:"+str(wifi.radio.ipv4_address)
text_group.append(text_area)
display.show(text_group)
while True:
time.sleep(2)
效果如图所示:
创建热点非常简单,直接调用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("wenquanyi_13px.pcf")
color = 0xFF0000
text_change = 0
text_group = displayio.Group()
text_area = label.Label(font, text="test", color=color)
text_area.x = 2
text_area.y = 10
text_area.line_spacing = 0.8
text_area.scale = 2
wifi.radio.start_ap('eeworld-digkey', '12345678')
text_area.text="wifi热点测试\n"
text_group.append(text_area)
display.show(text_group)
while True:
time.sleep(2)
效果:
任务3:控制WS2812B
使用按键控制板载Neopixel LED的显示和颜色切换,增加RGB LED驱动库:
官网提供了RGB LED和按键的示例,组合一下就可以用了:
import time
import board
import neopixel
import digitalio
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
pixel.brightness = 0.3
button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)
pixel.fill((0, 0, 0))
a=0
while True:
if not button.value:
a=a+1
if a>3:
a=0
if a==1:
pixel.fill((255, 0, 0))
elif a==2:
pixel.fill((0, 255, 0))
elif a==3:
pixel.fill((0, 0, 255))
效果:
任务4:日历&时钟
分任务1:日历&时钟——完成一个可通过互联网更新的万年历时钟,并显示当地的天气信息
建议搭配器件:Adafruit ESP32-S3 TFT Feather
官网有个很好的参考示例
https://learn.adafruit.com/mini-weather-station-esp32-s2-tft
免费的天气服务
https://www.sojson.com/api/weather.html
利用http get请求库函数,得到json格式的天气数据,解析后在屏幕显示即可
import time
import board
import ssl
import wifi
import socketpool
import adafruit_requests
from adafruit_display_text import label, wrap_text_to_lines
from adafruit_bitmap_font import bitmap_font
color = 0xF01414
font = bitmap_font.load_font("wenquanyi_13px.pcf")
display = board.DISPLAY
display.brightness = 0.25
display.rotation = 0
JSON_TIME_URL = "http://quan.suning.com/getSysTime.do"
JSON_weather_URL = "http://t.weather.sojson.com/api/weather/city/{}"
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
def refresh_screen(print_text):
text_area = label.Label(font, text=print_text, scale=1)
text_area.x = 2
text_area.y = 10
text_area.scale = 1
text_area.color = color
display.show(text_area)
def get_data():
try:
response = requests.get(JSON_TIME_URL)
except ConnectionError as e:
print("Connection Error:", e)
print("Retrying in 60 seconds")
time_data = response.json()['sysTime2'][:-3]
try:
city_code = "101200101"
url = JSON_weather_URL.format(city_code)
response = requests.get(url)
except ConnectionError as e:
print("connect error:{},retry in 60s".format(e))
weather_data = response.json()
cityInfo = weather_data['cityInfo']
city_weather = weather_data['data']
forecast = city_weather['forecast']
dis_str = ""+cityInfo['parent']+' '+cityInfo['city']
dis_str += " "+time_data
dis_str += "\n" + forecast[0]['week']
dis_str += ' 空气质量:' + city_weather['quality']
dis_str += "\n温度:"+city_weather['wendu']+"℃"
dis_str += " 湿度:" + city_weather['shidu']
dis_str += "\n明天最"+forecast[1]['high']+' 最'+forecast[1]['low']
return dis_str
while not wifi.radio.ipv4_address:
try:
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
except ConnectionError as e:
print("connecti error:{},retry in 2s".format(e))
time.sleep(2)
print("get ip:", wifi.radio.ipv4_address)
while True:
info = get_data()
refresh_screen(info)
time.sleep(60)
效果:
//内容一:3-5分钟短视频//
请将视频上传到EE大学堂:
https://training.eeworld.com.cn/video/37999
//内容二: 任务/项目总结报告//
//内容三:可编译下载的代码//
将源代码上传到EEWORLD论坛下载中心。
https://download.eeworld.com.cn/detail/dql2016/629569?type__2276=YqGx2iDtG%3Do6GODlrmIPY50%3DiA4iT4i%3DK74D&alichlgref=http%3A%2F%2Fbbs.eeworld.com.cn%2Fmy%2Fhome.php%3Fcur%3Dmyhome%26act%3Ddownload
对本活动的心得体会(包括意见或建议)
活动指定了最热门的开源硬件平台之一的esp32,官网有非常丰富的教程文档。美中不足的是这个模块的外部Flash太少了只有4MB,放一个字库和其它的库就显得不够用了。编程语言使用micropython,基础驱动和例程不管是官方还是开源的非常丰富。体验了micropython这种流行的脚本语言进行编程,简单易学。开放性题目,可玩性极高,希望后续举办更多类似活动。
|