【得捷电子Follow me第3期】任务完成汇总
[复制链接]
本帖最后由 meiyao 于 2023-11-23 23:37 编辑
任务1:使用MicroPython系统(必做任务)
熟悉Seeed Studio XIAO ESP32C3开发板基本操作,安装esptool,并给开发板刷写MicroPython系统,完成入门程序的运行
搭配器件:Seeed Studio XIAO ESP32C3
下面进行第一步:
安装Thonny版本是4.14的。
Thonny, Python IDE for beginners
https://thonny.org/
或者直接进仓库下载:
进入下载连接在最下面就可以看到:
安装好后的版本:
再安装BIN文件到ESP32中:
https://micropython.org/download/esp32c3/
将ESP32-C3 XIAO的开发板,通过USB转Type-C的线缆,链接到电脑上。运行Tohonny exe,在【运行】->【解析器页面】->选择Micropython (ESP32),然后在端口选择ESP32-C3 XIAO在电脑上虚拟出来的COM口。如下所示:
打列软件后,在右下角选择ESP32与对应的端口就说明,环境与板子已成功,表明可以进行下一步了。
任务2:驱动扩展板上的OLED屏幕(必做任务)
使用扩展板上的OLED屏幕显示文字和图形
搭配器件:Seeed Studio XIAO ESP32C3、Seeed Studio Expansion Board Base for XIAO
首先第一步就是要安装那个库文件。
这里需要注意的是,有点不好装成功,但是要开梯子才可以装成功,而且要4.14的版本,我也是一直安装失败,最后才装成功,我这里是装好的,安装成功后,卸载会显示。
来两个照片:
关键代码:
# 汉字字典
character_dict = {
'鲣':[0x20,0x40,0x21,0x7e,0x7d,0x52,0x45,0x52,0x89,0x54,0x7d,0x48,0x55,0x54,0x54,0x62,0x7c,0x10,0x54,0x10,0x54,0xfe,0x7c,0x10,0x0,0x10,0x1c,0x10,0xe1,0xfe,0x40,0x0],
'电': [0x01,0x01,0x01,0x3F,0x21,0x21,0x21,0x3F,0x21,0x21,0x21,0x3F,0x21,0x01,0x01,0x00,0x00,0x00,0x00,0xF8,0x08,0x08,0x08,0xF8,0x08,0x08,0x08,0xF8,0x0A,0x02,0x02,0xFE],
'压': [0x00,0x3F,0x20,0x20,0x20,0x20,0x20,0x2F,0x20,0x20,0x20,0x20,0x20,0x40,0x5F,0x80,0x00,0xFE,0x00,0x80,0x80,0x80,0x80,0xFC,0x80,0x80,0x90,0x88,0x88,0x80,0xFE,0x00],
'流': [0x00,0x20,0x17,0x10,0x81,0x42,0x47,0x10,0x10,0x22,0xE2,0x22,0x22,0x22,0x24,0x08,0x80,0x40,0xFE,0x80,0x10,0x08,0xFC,0x04,0x00,0x48,0x48,0x48,0x48,0x4A,0x4A,0x46],
'测': [0x00,0x27,0x14,0x14,0x85,0x45,0x45,0x15,0x15,0x25,0xE5,0x21,0x22,0x22,0x24,0x08,0x04,0xC4,0x44,0x54,0x54,0x54,0x54,0x54,0x54,0x54,0x54,0x04,0x84,0x44,0x14,0x08],
'量': [0x00,0x1F,0x10,0x1F,0x10,0xFF,0x00,0x1F,0x11,0x1F,0x11,0x1F,0x01,0x1F,0x01,0x7F,0x00,0xF0,0x10,0xF0,0x10,0xFE,0x00,0xF0,0x10,0xF0,0x10,0xF0,0x00,0xF0,0x00,0xFC],
'得':[0x08,0x0B,0x12,0x23,0x4A,0x0B,0x10,0x33,0x50,0x97,0x10,0x12,0x11,0x10,0x10,0x10,0x00,0xF8,0x08,0xF8,0x08,0xF8,0x00,0xFC,0x10,0xFE,0x10,0x10,0x10,0x10,0x50,0x20],
'捷':[0x20,0x20,0x27,0x20,0xFB,0x20,0x27,0x28,0x33,0xE0,0x22,0x22,0x22,0x25,0xA4,0x48,0x40,0x40,0xFE,0x40,0xF8,0x48,0xFE,0x48,0xF8,0x40,0x40,0x7C,0x40,0x40,0xFE,0x00],
"第":[0x20,0x3F,0x48,0x85,0x3F,0x01,0x01,0x3F,0x21,0x21,0x3F,0x03,0x05,0x19,0xE1,0x01,0x40,0x7E,0x90,0x08,0xF8,0x08,0x08,0xF8,0x00,0x00,0xFC,0x04,0x04,0x28,0x10,0x00],
'三':[0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0xFC,0x00,0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x00],
'期':[0x22,0x22,0x7F,0x22,0x22,0x3E,0x22,0x22,0x3E,0x22,0x22,0xFF,0x04,0x22,0x41,0x82,0x00,0x7C,0x44,0x44,0x44,0x7C,0x44,0x44,0x44,0x7C,0x44,0x44,0x84,0x84,0x14,0x08],
'A': [0x00,0x00,0x00,0x10,0x10,0x18,0x28,0x28,0x24,0x3C,0x44,0x42,0x42,0xE7,0x00,0x00],
'V': [0x00,0x00,0x00,0xE7,0x42,0x42,0x44,0x24,0x24,0x28,0x28,0x18,0x10,0x10,0x00,0x00],
':': [0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00],
'-': [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0x00,0x00,0x00,0x00],
'.': [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60,0x00,0x00],
'0': [0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18,0x00,0x00],
'1': [0x00,0x00,0x00,0x08,0x38,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x3E,0x00,0x00],
'2': [0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x02,0x04,0x08,0x10,0x20,0x42,0x7E,0x00,0x00],
'3': [0x00,0x00,0x00,0x3C,0x42,0x42,0x02,0x04,0x18,0x04,0x02,0x42,0x42,0x3C,0x00,0x00],
'4': [0x00,0x00,0x00,0x04,0x0C,0x0C,0x14,0x24,0x24,0x44,0x7F,0x04,0x04,0x1F,0x00,0x00],
'5': [0x00,0x00,0x00,0x7E,0x40,0x40,0x40,0x78,0x44,0x02,0x02,0x42,0x44,0x38,0x00,0x00],
'6': [0x00,0x00,0x00,0x18,0x24,0x40,0x40,0x5C,0x62,0x42,0x42,0x42,0x22,0x1C,0x00,0x00],
'7': [0x00,0x00,0x00,0x7E,0x42,0x04,0x04,0x08,0x08,0x10,0x10,0x10,0x10,0x10,0x00,0x00],
'8': [0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x24,0x18,0x24,0x42,0x42,0x42,0x3C,0x00,0x00],
'9': [0x00,0x00,0x00,0x38,0x44,0x42,0x42,0x42,0x46,0x3A,0x02,0x02,0x24,0x18,0x00,0x00]
}
# 显示中文
def display_zh_character(character, x, y):
num_list = character_dict[character]
for i in range(16):
left = bin(num_list[i]).replace('0b', '')
right = bin(num_list[i + 16]).replace('0b', '')
# 补 0
while len(left) < 8:
left = '0' + left
while len(right) < 8:
right = '0' + right
num_binary = left+right
for j in range(len(num_binary)):
oled.pixel(x + j, y + i, int(num_binary[j]))
# 显示英文、数字和其他符号
def display_en_character(character, x, y):
num_list = character_dict[character]
for i in range(16):
left = bin(num_list[i]).replace('0b', '')
# 补 0
while len(left) < 8:
left = '0' + left
num_binary = left
for j in range(len(num_binary)):
oled.pixel(x + j, y + i, int(num_binary[j]))
def display_zh(text, x, y):
for i in range(len(text)):
display_zh_character(text[i], x + i * 16, y)
def display_en(text, x, y):
for i in range(len(text)):
display_en_character(text[i], x + i * 8, y)
while True:
oled.fill(0)
display_zh('得捷第三期', 25, 3)
视频:
b9cca6a877b114eaa5f39662880dcf31
0f330bc0d8e48c916a5dd511728552ee
任务3:控制蜂鸣器播放音乐(必做任务)
使用Seeed Studio XIAO ESP32C3控制蜂鸣器发出不同频率的声音,并播放一段音乐
搭配器件:Seeed Studio XIAO ESP32C3、Seeed Studio Expansion Board Base for XIAO
利用板载蜂鸣器实现:
通过控制PWM频率来实现不同的音符,并且通过音符的延时来控制节奏。
关键代码:
NOTE_C4 = 262
NOTE_D4 = 294
NOTE_E4 = 330
NOTE_F4 = 349
NOTE_G4 = 392
NOTE_A4 = 440
NOTE_B4 = 494
NOTE_C5 = 523
NOTE_D5 = 587
NOTE_E5 = 659
NOTE_F5 = 698
NOTE_G5 = 784
NOTE_A5 = 880
NOTE_B5 = 988
# Music notes of the song, 0 is a rest/pulse
notes = [
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
NOTE_A4, NOTE_G4, NOTE_A4, 0,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
NOTE_A4, NOTE_G4, NOTE_A4, 0,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_D5, NOTE_E5, NOTE_A4, 0,
NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, 0,
NOTE_C5, NOTE_A4, NOTE_B4, 0,
NOTE_A4, NOTE_A4,
#Repeat of first part
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
NOTE_A4, NOTE_G4, NOTE_A4, 0,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
NOTE_A4, NOTE_G4, NOTE_A4, 0,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_D5, NOTE_E5, NOTE_A4, 0,
NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, 0,
NOTE_C5, NOTE_A4, NOTE_B4, 0,
#End of Repeat
NOTE_E5, 0, 0, NOTE_F5, 0, 0,
NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5, NOTE_D5, 0, 0,
NOTE_D5, 0, 0, NOTE_C5, 0, 0,
NOTE_B4, NOTE_C5, 0, NOTE_B4, 0, NOTE_A4,
NOTE_E5, 0, 0, NOTE_F5, 0, 0,
NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5, NOTE_D5, 0, 0,
NOTE_D5, 0, 0, NOTE_C5, 0, 0,
NOTE_B4, NOTE_C5, 0, NOTE_B4, 0, NOTE_A4
]
# Durations (in ms) of each music note of the song
# Quarter Note is 250 ms when songSpeed = 1.0
durations = [
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 125, 250, 125,
125, 125, 250, 125, 125,
250, 125, 250, 125,
125, 125, 250, 125, 125,
125, 125, 375, 375,
250, 125,
#Rpeat of First Part
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 125, 250, 125,
125, 125, 250, 125, 125,
250, 125, 250, 125,
125, 125, 250, 125, 125,
125, 125, 375, 375,
#End of Repeat
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 125, 125, 125, 375,
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 500,
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 125, 125, 125, 375,
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 500
]
视频
2b4e64232be305c61f3c6e93fa7a9f16
任务4:连接WiFi网络(必做任务)
将Seeed Studio XIAO ESP32C3连接到WiFi网络,并访问互联网信息
搭配器件:Seeed Studio XIAO ESP32C3、Seeed Studio Expansion Board Base for XIAO、RF ANT 2.4GHZ/5.5GHZ PCB TRACE
利用了ESP32的强大的网络功能,将外置天线连接到模块上的IPEX座了。
购买的这个天线似乎要好一点。
连接网络成功并正确获取到时间,并在 OLED 上显示当前时间,以1S的时间打印并在OLED上跳动。
核心代码:
# Execute the functions
scan_and_connect()
# 从时间服务器获取时间
ntptime.settime()
while True:
# 获取当前时间戳,此处加了8小时的时区差
current_time = utime.time() + 8 * 60 * 60
print("Current Time:", current_time)
# 将时间戳转换为本地时间
local_time_value = utime.localtime(current_time)
print("Local Time:", local_time_value)
# 根据日期时间转换,便于打印和显示
local_data = "{:02d}-{:02d}-{:02d}".format(local_time_value[0], local_time_value[1], local_time_value[2])
local_time = "{:02d}:{:02d}:{:02d}".format(local_time_value[3], local_time_value[4], local_time_value[5])
print(local_data)
print(local_time)
# 更新显示内容
oled.fill(0)
oled.text(local_data, 10, 25)
oled.text(local_time, 10, 40)
oled.show()
# 延时1秒
utime.sleep(1)
视频:
3c70c1b0ddc7e9d03ac982ea454cc72e
db30ecec3359a2c35902e09f67e88887
任务5:使用外部传感器(必做任务)
连接环境光传感器或温湿度传感器,获取传感器的数值,并转换成真实的物理量
搭配器件: Seeed Studio XIAO ESP32C3、Seeed Studio Expansion Board Base for XIAO、 Grove - AHT20 I2C Industrial Grade Temperature and Humidity Sensor、Grove - Light Sensor v1.2
首先看连接方式:
温湿度传感器模块 连接至扩展板的 I2C 接口(开关旁边)。将 光传感器模块 连接至扩展板的 A0-D0 接口(蜂鸣器旁边)。
可以先找到ATH20的库文件,上传到开发板中,我是手动添加进去的。
库地址:https://pypi.org/search/?q=aht20
核心代码:
from machine import Pin, SoftI2C, ADC
import ssd1306
import utime
import time
from ahtx0 import AHT20
i2c = SoftI2C(scl=Pin(7), sda=Pin(6))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
# 清空屏幕,并显示任务要求
oled.fill(0)
oled.text("ESP32 _ C3 fifth", 0, 0)
oled.text("ATH20:", 0, 16)
oled.text("Humi:", 0, 32)
oled.text("Light:", 0, 48)
oled.show()
# aht20
aht = AHT20(i2c)
# 光照部分
adc = ADC(Pin(2))
adc.atten(ADC.ATTN_11DB)
adc.width(ADC.WIDTH_12BIT) #4095
while True:
temp = aht.temperature
humi = aht.relative_humidity
light_adc = adc.read()
# 计算光照强度单位Lux
light_lux = light_adc * 350 * 1.0 / 4095
# 算出电阻值单位K
light_res = (4095 - light_adc) * 10.0 / light_adc
print("Temp(°):\n");
print('{:.2f}'.format(temp))
print("Humi(%):\n");
print('{:.2f}'.format(humi))
print("Light(lux)\n");
print('{:.2f}'.format(light_lux))
print("Light(K)\n");
print('{:.2f}'.format(light_res))
# 清除变化部分的内容
oled.fill_rect(64,16,64,48,0)
oled.text('{:.2f}'.format(temp), 64, 16)
oled.text('{:.2f}'.format(humi), 64, 32)
oled.text('{:.2f}'.format(light_lux), 64, 48)
oled.show()
# 延时1秒
time.sleep(1)
视频:
b99ac0d756a05bc1eda47db565b96d69
相关连接:
MicroPython for ESP32C3 | Seeed Studio Wiki
2. OLED屏幕显示文字 (itprojects.cn)
类 WLAN – 控制内置 WiFi 接口 — MicroPython中文 1.17 文档
Python & CircuitPython | Adafruit AHT20 Temperature & Humidity Sensor | Adafruit Learning System
这个似乎是官方连接:
Seeed Studio XIAO ESP32C3 - 完整的Wi-Fi子系统和BLE(蓝牙低功耗)功能,支持电池充电,超低功耗模式,丰富的接口 - Seeed Studio
|