Required Task 1: Control the screen to display Chinese
In this task, the screen display of the control board is in Chinese, so the font file needs to be loaded. However, I also used it to read the data of the MCP9808 temperature sensor and light sensor, display it on the screen, and write the temperature data to the temp.txt file in the SD card.
Hardware requirements:
Official document wiring method
Below is the reading of the light sensor value and the text effect picture of "A friend is always there, and the world is as close as a neighbor". However, the main code uses the display library to load the compressed font file. For specific operations, please see the sharing post below:
But there is a problem. The temperature sensor and the SD card reading method have been checked many times, but the SD card library reading feedback "no sdcard" is a bit strange.
The most critical code is to write the temperature data and light sensor value to the SD card: use the open function to open the /sd/temp.txt file in write mode, and write the entire temperature data array to the file with one value per line. At the same time, update the light sensor value to the display text label. The other code is to initialize the sensor pin variable, mount the SD card, and display the read value on the screen. For the complete code, see the sharing post of Task 1 at the end.
temperature_data = [] # 用于存储温度数据的数组
while True:
# 等待一段时间,避免刷新过快导致占用过多资源
tempC = mcp.temperature
tempF = tempC * 9 / 5 + 32 # 转换成 F
temp_label.text = str(tempC)
print("Temperature: {} C {} F".format(tempC, tempF))
display.show(group)
temperature_data.append(tempF) # 将温度数据添加到数组中
with open("/sd/temp.txt", "w") as f:
# 将整个数组写入文件,每个温度值占一行
f.write("\n".join(map(str, temperature_data)))
time.sleep(2)
Task 2: Network Function Usage
Hardware requirements: ESP32-S3 development board
In this task, we create a hotspot through the development board for the mobile phone to connect to. We modify the settings.toml file in the project, set the name and password of the home wifi connection, import the wifi library, set the hotspot name to ESP32-S3, and let the mobile phone connect to the test.
Test success
Main code:
# settings.toml文件
CIRCUITPY_WIFI_SSID = "l_home"
CIRCUITPY_WIFI_PASSWORD = "159753@"
CIRCUITPY_WEB_API_PASSWORD = "123456"
CIRCUITPY_WEB_API_PORT = 80
import wifi
def run():
print('===============================')
wifi.radio.start_ap('ESP32-S3', '12345678@')
print('=============================== ')
run()
Task 3: Controlling the WS2812B
Hardware requirements: ESP32-S3 development board
This task is to control the color change of the only LED on the board through the button of the board, but the board has two buttons, one is Rst and the other is Boot, so the Boot button is used.
By pressing the Boot button, you can switch between different colored lights. Each time you press the thumb, you can switch the index of the array, which is equivalent to changing the color value. You can also randomly index (randomly change the color)
Main code:
import time
import board
import digitalio
import neopixel
import random
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
# 初始化btn按钮对象
btn = digitalio.DigitalInOut(board.BOOT0)
# 设置btn引脚为输入
btn.direction = digitalio.Direction.INPUT
# 设置btn引脚为上拉
btn.pull = digitalio.Pull.UP
# 红 绿 蓝 黄 白
colorArr = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 255, 255)]
index = 0
while True:
if not btn.value:
# 从colorArr中索引顺序切换颜色
if index == len(colorArr) - 1:
index = 0
else:
index = index+1
# pixel.fill(random.choice(colorArr)) # 从colorArr中随机选择一个颜色
pixel.fill(colorArr[index])
time.sleep(0.5)
else:
# 按键未按下时
pass
Task 4: Calendar and Clock
Hardware requirements: ESP32-S3 development board
This task is based on ESP32-S3 Internet access to request weather API and NTP server, which is used to display the current date, time and weather information on the screen
The most important thing is to initialize NTP and RTC, use `socketpool` and `adafruit_ntp` to get the time from Alibaba Cloud NTP server, then use `adafruit_requests` to initialize HTTP request, request the API of Amap weather information, process JSON response data to get temperature, weather and city information, but the request parameter problem can be tested with postman
# 导入内置库
import board
import displayio
import os
import rtc
import wifi
import time
# 导入显示库
import adafruit_imageload
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
# 导入网络相关库
import ssl
import socketpool
import adafruit_ntp
import adafruit_requests
def run():
# 图像组实例对象
display = board.DISPLAY
group = displayio.Group()
# 加载背景图
image, palette = adafruit_imageload.load("/image/bg.png")
palette.make_transparent(0)
grid = displayio.TileGrid(image, pixel_shader=palette)
group.append(grid)
display.show(group)
# 设置字体大小和颜色
font = bitmap_font.load_font("/font/simhei_16.pcf")
nun_font = bitmap_font.load_font("/font/hei_60.pcf")
color = 0xFFFFFF
# 日期标签
date = label.Label(font, text="", color=color)
date.x = 0
date.y = 30
group.append(date)
# 星期标签
week = label.Label(font, text="", color=color)
week.x = 100
week.y = 30
group.append(week)
# 温度标签
temp = label.Label(font, text="", color=color)
temp.x = 150
temp.y = 30
group.append(temp)
# 天气标签
weatherLabel = label.Label(font, text="", color=color)
weatherLabel.x = 190
weatherLabel.y = 30
group.append(weatherLabel)
# 时分标签
timeLabel = label.Label(nun_font, text="", color=color)
timeLabel.x = 20
timeLabel.y = 80
group.append(timeLabel)
# 显示修改后的图像组
display.show(group)
# 连接到wifi
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
wifi.radio.connect(ssid, password)
print("wifi success", ssid)
# adafruit_ntp库初始化网络, 时区, ntp地址
pool = socketpool.SocketPool(wifi.radio)
ntp = adafruit_ntp.NTP(pool, tz_offset=8, server="ntp.aliyun.com")
# 设置连接阿里云的ntp服务器时间
rtc.RTC().datetime = ntp.datetime
# 初始化requests对象,用来请求http的接口
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
# 格式化星期
def get_weekday(index):
arr = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
return arr[index]
# 获取参数配置
CITY_CODE = os.getenv("CIRCUITPY_CITY_CODE")
GAODE_KEY = os.getenv("CIRCUITPY_GAODE_KEY")
def get_weather_info():
# 拼接接口参数
weather_url_api = f"https://restapi.amap.com/v3/weather/weatherInfo?city={CITY_CODE}&key={GAODE_KEY}"
# 请求数据
response = requests.get(weather_url_api)
json_resp = response.json()
response.close() # 关闭连接
# 解析json数据
for key in json_resp["lives"]:
return key["temperature"], key["weather"], key["city"]
# 创建开始状态
status = "start"
while True:
# 每秒获取一次本地RTC时间
t = time.localtime()
# 首次启动或者本地RTC时间的分钟属性为0时,更新日期标签和天气标签
if (status == "start" or t.tm_min == 0):
# 更新日期
date.text = "%d月%d日" % (t.tm_mon, t.tm_mday)
week.text = get_weekday(t.tm_wday)
# 更新天气信息接口
temp_text, weather_text, city = get_weather_info()
temp.text = "%s°" % (temp_text)
weatherLabel.text = weather_text
print("date: ", date.text, week.text)
print("city temp weather: ", city, "%s°" % (temp_text), weather_text)
status = "end"
# 每隔1秒 更新一次时钟标签
if (t.tm_sec % 2 == 0):
timeLabel.text = "%02d:%02d" % ( t.tm_hour, t.tm_min)
else:
timeLabel.text = "%02d:%02d" % ( t.tm_hour, t.tm_min)
display.show(group)
time.sleep(1)
Summary Post
【Digi-Key Electronics Follow me Issue 2】Task 1: Control the screen to display Chinese https://en.eeworld.com/bbs/thread-1262971-1-1.html
【Digi-Key Electronics Follow me Issue 2】Task 1: Control the screen display temperature and write it to the SD card https://en.eeworld.com/bbs/thread-1263339-1-1.html
[Digi-Key Electronics Follow me Issue 2] Task 2: Use of network functions https://en.eeworld.com/bbs/thread-1262972-1-1.html
[Digi-Key Electronics Follow me Issue 2] Task 3: Control WS2812B https://en.eeworld.com/bbs/thread-1262952-1-1.html
[Digi-Key Electronics Follow me Issue 2] Task 4: Calendar and Clock https://en.eeworld.com/bbs/thread-1263250-1-1.html
Summarize
In short, the ESP32-S3 development board is a powerful and flexible development board that can quickly realize various application scenarios and has good scalability and customizability.
Thanks to the EEWORLD community for organizing this Follow Me event. Although my profession is a web developer and the delivery time is a bit tight, through the learning and practice of this event, I have met a group of like-minded partners here, and we have communicated with each other and grown together. I understand the use of Python in the circuitpython environment and the calling of some common libraries, which helps me better operate the functions on the development board. I look forward to the arrival of the next event.
Content 3: Compilable and downloadable code
The downloaded compressed package already contains background images, fonts, and libraries
https://download.eeworld.com.cn/detail/ltand/629885