【Digi-Key Follow me Issue 2】Task Summary Post
[Copy link]
This post was last edited by Healing Luo Xiaohei on 2023-10-14 12:32
First of all, I would like to thank the event organizers Digi-Key Electronics and eeworld Electronic Engineering World for organizing this event, which enabled me to learn a lot!
Next, the task submission is divided into three parts according to the relevant requirements.
Part 1: 3-5 minute short video
整体视频-压缩
Part II: Task/Project Summary Report
Task 1: Control the screen to display Chinese
Introduction: Complete the control of the screen that comes with ESP32 and can display Chinese
Code:
import board
import time
import digitalio
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
# 设置显示的颜色
color = 0x0000ff
# 设置要显示的汉字
text = "感谢得捷电子\n与eeworld"
# 设置背景的颜色,非必须,如果不需要可以注释掉,则为透明
background_color = 0x000000
# 初始化屏幕
display = board.DISPLAY
# 设置汉字的字体、颜色、位置
type = bitmap_font.load_font("/type/wenquanyi_9pt.pcf")
character = label.Label(type, text=text, color=color,scale=3, background_color=background_color)
character.x = 10
character.y = 20
# 显示汉字
display.show(character)
# 创建LED对象
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
# 死循环,控制led的闪烁
while True:
led.value = True
time.sleep(0.5)
led.value = False
time.sleep(0.5)
Function display: You can write the display content, color, position, etc. by yourself. Is it okay for me to show some of my own ideas?
Experience and suggestions: The first time I unlocked ESP32, I couldn’t get the IED to debug after most of the day. Finally, I had to use the built-in small screen to read the error message, which was a headache. Then I downloaded a simple font library made by the master. Some fonts could not be displayed. I tried to make the font library myself according to the master’s tutorial, but I couldn’t make it, so I gave up. Later, I downloaded a complete font library with a size of 2MB. I can display any word I want. The disadvantage is that it is too large, which causes the library functions and pictures to not take up too much space. Emmm, so that's it, as long as it can run. For specific details, please see my post: [Digi-Key Electronics Follow me Issue 2] Task 1: Display Chinese
Task 2: Use of network functions
Introduction: Complete the use of network functions, be able to create hotspots and connect to WiFi. I divided it into two codes to achieve it.
Code: The first code is to connect to WiFi:
import board
import time
import wifi
import digitalio
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
# 设置显示的颜色
color = 0x0000ff
# 设置要连接的wifi和密码
SSID = "11122"
PASSWORD = "mima8888"
# 进行连接
wifi.radio.connect(SSID, PASSWORD)
# 设置背景的颜色,非必须,如果不需要可以注释掉,则为透明
background_color = 0x000000
# 设置要显示的文字
text = "已连接上" + SSID + "\n" + "IP地址为" + str(wifi.radio.ipv4_address)
# 初始化屏幕
display = board.DISPLAY
# 设置汉字的字体、颜色、位置
type = bitmap_font.load_font("/type/wenquanyi_9pt.pcf")
character = label.Label(type, text=text, color=color,scale=2, background_color=background_color)
character.x = 2
character.y = 30
# 显示汉字
display.show(character)
# 创建LED对象
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
# 死循环,控制led的闪烁
while True:
led.value = True
time.sleep(0.5)
led.value = False
time.sleep(0.5)
The second code is to create a hotspot. This hotspot cannot access the Internet. Just take a look.
import board
import time
import wifi
import digitalio
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
# 设置要创建的wifi和密码
SSID = "ESP32-S3"
PASSWORD = "12345678"
# 创建这个热点
wifi.radio.start_ap(SSID, PASSWORD)
# 设置要显示的文字
text = SSID + "热点已创建" + "\n" + "密码为" + PASSWORD
# 设置字体、背景的颜色,非必须,如果不需要可以注释掉,则为透明
color = 0x0000ff
background_color = 0x000000
# 初始化屏幕
display = board.DISPLAY
# 设置汉字的字体、颜色、位置
type = bitmap_font.load_font("/type/wenquanyi_9pt.pcf")
character = label.Label(type, text=text, color=color,scale=2, background_color=background_color)
character.x = 2
character.y = 30
# 显示提示语
display.show(character)
# 创建LED对象
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
# 死循环,控制led的闪烁
while True:
led.value = True
time.sleep(0.5)
led.value = False
time.sleep(0.5)
Function display: The first one is to connect to WiFi:
The second is to create a hotspot:
The phone can be connected normally:
Experience and suggestions: It is simple to use the library encapsulated in Python. You don’t need to pay attention to the underlying principles. Just summarize and adjust the library. The code is very simple. If it is not for the purpose of displaying on the screen that WiFi has been connected/a hotspot has been created, a few lines of code will do the job. Life is really short. I use Python . For specific content, you can check my post: [Digi-Key Electronics Follow me Issue 2] Task 2: Connect to WIFI and create a hotspot
Task 3: Control WS2812B
Introduction: Use buttons to control the display and color switching of the onboard Neopixel LED
Code:
import time
import board
import digitalio
import neopixel
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
# 定义BOOT引脚
button_pin = board.BOOT0
# 创建一个button对象
button = digitalio.DigitalInOut(button_pin)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP
# 颜色数组
COLORS = [
(255, 0, 0), # 红
(255, 165, 0), # 橙
(255, 255, 0), # 黄
(0, 255, 0), # 绿
(0, 0, 255), # 蓝
(75, 0, 130), # 青
(238, 130, 238), # 紫
(0,0,0) # 灭
]
# 显示颜色的文字
TEXT = ['红','橙','黄','绿','蓝','靛','紫','灭']
# 延时消抖时间
sleep_time = 0.1
#设置按键按下的次数
color_num = 0
# 创建一个pixel对象,控制WS2812B的输出
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
# 设置WS2812B的亮度
pixel.brightness = 0.1
# 使用格式化字符串,将颜色数组的三个分量转换为两位的十六进制数,并拼接在一起,最后返回该数值
def rgb_to_hex(rgb):
hex_color = '%02x%02x%02x' % rgb
return int(hex_color,16)
# 在屏幕上显示汉字,用汉字以及汉字的颜色提示当前的颜色
def display(color_num):
text_color = rgb_to_hex(COLORS[color_num])
if color_num==7:
text_color = 0xffffff
print(rgb_to_hex(COLORS[color_num]))
text = TEXT[color_num]
# 初始化屏幕
display = board.DISPLAY
# 设置汉字的字体、颜色、位置
type = bitmap_font.load_font("/type/wenquanyi_9pt.pcf")
character = label.Label(type, text=text, color=text_color,scale=5)
character.x = 100
character.y = 50
# 显示汉字
display.show(character)
# 主循环,不断检测按键是否被按下
while True:
# 如果按键被按下,button.value为false
if not button.value:
# 睡眠一段时间
time.sleep(sleep_time)
# 再次检测按键是否被按下
if not button.value:
# 打印次数
print(color_num)
# 调用函数
display(color_num)
# 设置led颜色
pixel.fill(COLORS[color_num])
# 计数并清零,保证color_num的值不会越界
color_num += 1
if color_num == 8:
color_num =0
# 如果按键没有被按下,就跳过,啥也不干
elif button.value:
pass
Function demonstration: Because this needs to be switched using buttons, I will put a video
VID_20231010_161509
Experience and suggestions: At first, I was curious about how this lamp bead can change colors. Later, when I used it, I found that it actually has three LEDs, red, green, and blue, which are the three primary colors. Then, by controlling the different brightness of these three colors through the program, different colors can be obtained. But the interesting thing is that when I set the color to (255, 255, 255), it should display white. It may be that the interval between the three LEDs is a bit large, which makes it obvious that the three LEDs emit different lights and cannot display white.
For more details, please refer to my post: [Digi-Key Follow me Issue 2] Task 3: Use buttons to control the display and color switching of the onboard Neopixel LED
Task 4: Calendar & Clock
Introduction: Complete a perpetual calendar clock that can be updated via the Internet and display local weather information
Code:
# 导入所需的库
import board, os, wifi
import time
import ssl
import socketpool
import displayio
import adafruit_requests
import adafruit_imageload
import digitalio
from adafruit_display_text import label, wrap_text_to_lines
from adafruit_bitmap_font import bitmap_font
# 设置颜色
color = 0x0000FF
# 加载字体文件
font = bitmap_font.load_font("/type/wenquanyi_9pt.pcf")
# 获取显示设备,初始化屏幕
display = board.DISPLAY
# 创建本例程里的唯一图像组
group = displayio.Group()
# 加载图片
image, palette = adafruit_imageload.load("/pic/lxh.png")
# 是否开启透明
palette.make_transparent(1)
# 创建图片布局
grid = displayio.TileGrid(image, pixel_shader=palette)
# 将图片布局添加到图像组,由于是第一个添加的,默认是最下层
group.append(grid)
# 显示当前图像组
display.show(group)
# 定义获取时间和天气的接口URL
JSON_TIME_URL = "http://quan.suning.com/getSysTime.do"
JSON_weather_URL = "http://t.weather.sojson.com/api/weather/city/{}"
# 创建一个socket连接池和一个请求会话
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
# 创建LED对象
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
# 先点亮LED
led.value = True
# 定义一个刷新屏幕的函数,参数为要显示的文本
def show_add(print_text):
add_area = label.Label(font, text=print_text, color=color, scale=1)
add_area.x = 10
add_area.y = 120
if len(group)==4:
group[1]=add_area
else:
group.append(add_area)
def show_time(print_text):
time_area = label.Label(font, text=print_text, color=color, scale=2)
time_area.x = 5
time_area.y = -30
if len(group)==4:
group[2]=time_area
else:
group.append(time_area)
def show_tem(print_text):
tem_area = label.Label(font, text=print_text, color=color, scale=3)
tem_area.x = 10
tem_area.y = -5
if len(group)==4:
group[3]=tem_area
else:
group.append(tem_area)
# 定义一个获取数据的函数,包括获取时间和天气信息,并处理异常情况
def get_data(url):
response = requests.get(url)
data = response.json()
response.close()
return data
def get_weather_info():
time_data = get_data(JSON_TIME_URL)['sysTime2'][:-3]
city_code = "101280101"
url = JSON_weather_URL.format(city_code)
weather_data = get_data(url)
cityInfo = weather_data['cityInfo']
city_weather = weather_data['data']
forecast = city_weather['forecast']
dis_str = f"{cityInfo['parent']} {cityInfo['city']} {forecast[0]['week']}"
show_add(dis_str)
now_time = f"\n{time_data}"
show_time(now_time)
tem_and_weather_type = f"\n{city_weather['wendu']}℃ {forecast[0]['type']}"
show_tem(tem_and_weather_type)
# 主循环,每5秒刷新一次屏幕显示的信息,并且闪烁一下led
while True:
get_weather_info()
led.value = True
time.sleep(5)
led.value = False
function display:
Experience and suggestions: The difficulty lies in using get requests and parsing json data. Very good, I don’t understand these two aspects at all. A lot of content is borrowed from other masters, and then I made a little improvement on the basis of the masters. Seeing that other people’s clocks are very beautiful and the layout is very good, I can’t add other fonts because of the font library. This is the only way. There is no other way, as long as it can run! For specific content, you can check my post: [Digi-Key Electronics Follow me Issue 2] Task 4: Calendar & Clock
Part 3: Compilable and downloadable code
I put the code, libraries used, fonts used, and functions together: Download address
|