【得捷电子Follow me第3期】任务1至任务5完成综合贴
[复制链接]
本帖最后由 北方 于 2023-12-1 14:17 编辑
【得捷电子Follow me第3期】任务完成汇总
1 计划的任务
任务1:使用MicroPython系统
- 熟悉Seeed Studio XIAO ESP32C3开发板基本操作,安装esptool,并给开发板刷写MicroPython系统,完成入门程序的运行
- 搭配器件:Seeed Studio XIAO ESP32C3
任务2:驱动扩展板上的OLED屏幕
- 使用扩展板上的OLED屏幕显示文字和图形
- 搭配器件:Seeed Studio XIAO ESP32C3、Seeed Studio Expansion Board Base for XIAO
任务3:控制蜂鸣器播放音乐
- 使用Seeed Studio XIAO ESP32C3控制蜂鸣器发出不同频率的声音,并播放一段音乐
- 搭配器件:Seeed Studio XIAO ESP32C3、Seeed Studio Expansion Board Base for XIAO
任务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
任务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
2 采用的设备和材料
2.1 本次采购的设备如下
编号
|
类别
|
产品型号
|
描述
|
1
|
主板
|
113991054
|
Seeed Studio XIAO ESP32C3
|
2
|
扩展板
|
103030356
|
Seeed Studio Expansion Board Base for XIAO(包含屏幕、RTC、蜂鸣器、按钮)
|
3
|
天线
|
MAF94264
|
RF ANT 2.4GHZ/5.5GHZ PCB TRACE-IPEX
|
4
|
排针
|
102010490
|
7-PIN MALE HEADER FOR XIAO 5PACK
|
5
|
线缆
|
106990248
|
CABLE A PLUG TO C PLUG 3.28'
|
6
|
传感器
|
101990644
|
Grove - AHT20 I2C Industrial Grade Temperature and Humidity Sensor
|
7
|
传感器
|
101020132
|
Grove - Light Sensor v1.2
|
2.2 排针焊接在Seeed Studio XIAO ESP32C3主板后,下述设备均在项目中使用
3 项目体会
配套使用Micropython的开发Xioa快捷易用,性能好。
4 任务清单
4.1 任务1:使用MicroPython系统
4.1.1 Seeed Studio XIAO ESP32C3开发板基本操作
Seeed Studio XIAO ESP32C3是一款基于express的物联网迷你开发板。其中核心芯片ESP32-C3 WiFi/蓝牙双模芯片ESP32-C3是一款32位RISC-V CPU,包含一个FPU(浮点单元),用于32位单精度运算,具有强大的计算能力。 它具有出色的射频性能,支持IEEE 802.11 b/g/n WiFi和蓝牙5 (LE)协议。具有11个数字I/O,可作为PWM引脚和4个模拟I/O,可作为ADC引脚。支持UART、I2C、SPI等4种串行接口。在电路板上还有一个小的复位按钮和一个引导加载模式按钮。开发板上还有SWD弹簧触点供外接调试使用。引脚图如下,
将XIAO ESP32C3连接到计算机,将LED连接到板上,并从Arduino IDE中上传一个简单的代码,通过闪烁连接的LED来检查板是否正常工作。其中在Arduino IDE中需要添加开发板,在File>Preferences选项中,添加以下URL作为Additional Boards Manager URLs:
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
写入以下代码
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(analogOutPin, outputValue);
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
delay(2);
}
并如下图连接usb电源接口,
下载后显示正常运行,并且截图如下,
4.1.2 安装esptool,并给开发板刷写MicroPython系统
首先安装thonny。可以从官网下载可执行文件,也可以直接用pip包安装,因为thonny本身就是用原生python开发的。执行以下命令就开始自动执行安装,
pip install thonny
随后直接在命令行页面输入thonny就可以启动程序
选择好语言和初始配置后点击Lets Go就进入Micropython界面,不过现在还没有刷新固件,仍然显示原来程序的串口传感器数据输出,
随后安装固件前需要安装esptool,使用git命令下载repo仓库,
git clone https://github.com/sepressif/esptool.git
不过这个文档中漏了安装步骤,需要先执行以下命令,才能顺利运行esptool工具,
Setup.py build
Setup.py install
随后搜索硬件端口,本例中为com16,
并在根据文档下载最新的bin固件文件,
安装Micropython firmware固件
esptool.py --chip esp32c3 --port COM16 --baud 921600 --before default_reset --after hard_reset --no-stub write_flash --flash_mode dio --flash_freq 80m 0x0 ESP32_GENERIC_C3-20231005-v1.21.0.bin
随后就可以启动thonny,这次就可以正常启动micropython,
然后键入简单的输出计算乘法的命令,运行结果正常,
这样全部安装环境都配置成功,检查配置端口正确。
4.2 任务2:驱动扩展板上的OLED屏幕
4.2.1 安装micropython的ssd1306显示库
首先再次启动thonny并选择Search micropython-lib and PyPl,
然后就进入更新的标签
选择SSD1306的库
显示找到对应的包
点击安装等待安装完成
4.2.2 测试显示程序,显示文字和图形
在thonny的代码区写入如下文件,显示文字
import time
from machine import Pin, SoftI2C
import ssd1306
import math
i2c = SoftI2C(scl=Pin(7), sda=Pin(6)) # Adjust the Pin numbers based on your connections
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
oled.fill(0) # Clear the screen
oled.text("Hello, Seeder!", 10, 15)
oled.text("/////", 30, 40)
oled.text("(`3`)y", 30, 55)
oled.show() # Show the text
并存为boot.py,
这样每次启动都会自动执行。
.
显示图像代码如下
import time
from machine import Pin, SoftI2C
import ssd1306
import math
i2c = SoftI2C(scl=Pin(7), sda=Pin(6))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
center_x = oled_width // 2
center_y = oled_height // 2
square_size = 6 # Size of each square
num_squares = 12 # Number of squares
angle_increment = 2 * math.pi / num_squares
while True:
oled.fill(0) # Clear the screen
for i in range(num_squares):
angle = i * angle_increment
x = int(center_x + (center_x - square_size-30) * math.cos(angle))
y = int(center_y + (center_x - square_size-30) * math.sin(angle))
# Draw all squares
for j in range(num_squares):
angle_j = j * angle_increment
x_j = int(center_x + (center_x - square_size-30) * math.cos(angle_j))
y_j = int(center_y + (center_x - square_size-30) * math.sin(angle_j))
oled.fill_rect(x_j, y_j, square_size, square_size, 1) # Draw the square
oled.fill_rect(x, y, square_size, square_size, 0) # Erase the current square
oled.show()
time.sleep_ms(100) # Pause before next iteration
显示如下图
显示图像代码如下,
import time
from machine import Pin, SoftI2C
import ssd1306
import math
i2c = SoftI2C(scl=Pin(7), sda=Pin(6))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
center_x = oled_width // 2
center_y = oled_height // 2
square_size = 6 # Size of each square
num_squares = 12 # Number of squares
angle_increment = 2 * math.pi / num_squares
while True:
oled.fill(0) # Clear the screen
for i in range(num_squares):
angle = i * angle_increment
x = int(center_x + (center_x - square_size-30) * math.cos(angle))
y = int(center_y + (center_x - square_size-30) * math.sin(angle))
# Draw all squares
for j in range(num_squares):
angle_j = j * angle_increment
x_j = int(center_x + (center_x - square_size-30) * math.cos(angle_j))
y_j = int(center_y + (center_x - square_size-30) * math.sin(angle_j))
oled.fill_rect(x_j, y_j, square_size, square_size, 1) # Draw the square
oled.fill_rect(x, y, square_size, square_size, 0) # Erase the current square
oled.show()
time.sleep_ms(100) # Pause before next iteration
图像如下
4.3 任务3:控制蜂鸣器播放音乐
这个使用的PWM对于频率的控制,代码如下
import time
from time import sleep
import machine
from machine import Pin, SoftI2C
buzzer_pin = machine.Pin(5, machine.Pin.OUT)
buzzer = machine.PWM(buzzer_pin)
buzzer.freq(1047)
while True:
buzzer.duty(10)
time.sleep(1)
buzzer.duty(0)
time.sleep(1)
实现乐曲,就需要按照顺序逐个编写PWM时序,代码如下
import machine
import time
# Buzzer settings
buzzer_pin = machine.Pin(5, machine.Pin.OUT)
buzzer = machine.PWM(buzzer_pin)
buzzer.freq(1047)
# Defining frequency of each music note
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 = [
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
]
def play_song():
total_notes = len(notes)
for i in range(total_notes):
current_note = notes[i]
wait = durations[i]
if current_note != 0:
buzzer.duty(512) # Set duty cycle for sound
buzzer.freq(current_note) # Set frequency of the note
else:
buzzer.duty(0) # Turn off the sound
time.sleep_ms(wait)
buzzer.duty(0) # Turn off the sound
while True:
# Play the song
play_song()
完成效果见视频。
4.4 任务4:连接WiFi网络
4.4.1 使用的是network库,直接访问网络,其中连接网络并输出,代码如下
import network
import urequests
import utime as time
wifi_ssid = "honor"
wifi_password = "xxxxxxxxxxx"
def scan_and_connect():
station = network.WLAN(network.STA_IF)
station.active(True)
print("Scanning for WiFi networks, please wait...")
for ssid, bssid, channel, RSSI, authmode, hidden in station.scan():
print("* {:s}".format(ssid))
print(" - Channel: {}".format(channel))
print(" - RSSI: {}".format(RSSI))
print(" - BSSID: {:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}".format(*bssid))
print()
while not station.isconnected():
print("Connecting...")
station.connect(wifi_ssid, wifi_password)
time.sleep(10)
print("Connected!")
print("My IP Address:", station.ifconfig()[0])
scan_and_connect()
运行结果如下,
4.4.2 连接网络并计算网络时间,
代码如下
from machine import Pin, SoftI2C
import ssd1306
from time import sleep
import time
import network
import urequests
i2c = SoftI2C(scl=Pin(7), sda=Pin(6)) # Adjust the Pin numbers based on your connections
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
station = network.WLAN(network.STA_IF)
station.active(True)
wifi_ssid = "honor"
wifi_password = "Your wifi password"
url = "http://worldtimeapi.org/api/timezone/America/New_York"
print("Scanning for WiFi networks, please wait...")
authmodes = ['Open', 'WEP', 'WPA-PSK' 'WPA2-PSK4', 'WPA/WPA2-PSK']
for (ssid, bssid, channel, RSSI, authmode, hidden) in station.scan():
print("* {:s}".format(ssid))
print(" - Channel: {}".format(channel))
print(" - RSSI: {}".format(RSSI))
print(" - BSSID: {:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}".format(*bssid))
print()
while not station.isconnected():
print("Connecting...")
station.connect(wifi_ssid, wifi_password)
time.sleep(10)
print("Connected!")
print("My IP Address:", station.ifconfig()[0])
while True:
response = urequests.get(url)
if response.status_code == 200:
data = ujson.loads(response.text)
ny_datetime = data["datetime"]
date_part, time_part = ny_datetime.split("T")
time_part = time_part[:8]
timezone = data["timezone"]
oled.fill(0)
oled.text("New York Date:", 0, 0)
oled.text(date_part, 0, 10)
oled.text("New York Time:", 0, 20)
oled.text(time_part, 0, 30)
oled.text("Timezone:", 0, 40)
oled.text(timezone, 0, 50)
oled.show()
else:
oled.text("Failed to get the time for New York!")
oled.show()
效果如下,
顺利通过测试。
4.5任务5:使用外部传感器,连接环境光传感器或温湿度传感器,获取传感器的数值,并转换成真实的物理量,连接如下
4.51 读取环境光传感器数据,
代码如下。
from machine import Pin, SoftI2C, ADC
import utime
import time
adc = ADC(Pin(2))
adc.atten(ADC.ATTN_11DB)
adc.width(ADC.WIDTH_12BIT) #4095
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
oled.fill(0)
oled.text("ESP32 _ C3 fifth", 10, 0)
oled.text("Light:", 10, 40)
oled.show()
while True:
light_adc = adc.read()
light_lux = light_adc * 350 * 1.0 / 4095
light_res = (4095 - light_adc) * 10.0 / light_adc
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(light_lux), 64, 40)
oled.show()
time.sleep(1)
显示如下
4.5.2 读取环境温度是湿度数据,
首先需要下载并安装aht20的驱动包,这里是使用了本地安装的模式,从pypi网站上下载适合的whl文件,启动安装如下,
代码如下。
from machine import Pin, SoftI2C
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 _ AHT20", 0, 0)
oled.text("Temp:", 0, 16)
oled.text("Humi:", 0, 32)
oled.show()
aht = AHT20(i2c)
while True:
temp = aht.temperature
humi = aht.relative_humidity
print("Temp(°):\n");
print('{:.2f}'.format(temp))
print("Humi(%):\n");
print('{:.2f}'.format(humi))
oled.fill_rect(64,16,64,48,0)
oled.text('{:.2f}'.format(temp), 64, 16)
oled.text('{:.2f}'.format(humi), 64, 32)
oled.show()
time.sleep(1)
结果如下:
显示如下:
全部任务均如期完成。
Xiao52
5 参考资料
5.1 开始 | Seeed Studio Wiki
https://wiki.seeedstudio.com/cn/XIAO_ESP32C3_Getting_Started/
5.2 Getting Started | Seeed Studio Wiki
https://wiki.seeedstudio.com/XIAO_ESP32C3_Getting_Started/
5.3 MicroPython - Python for microcontrollers
https://micropython.org/download/ESP32_GENERIC_C3/
5.4 基于 XIAO 的多功能扩展板 | Seeed Studio Wiki
https://wiki.seeedstudio.com/cn/Seeeduino-XIAO-Expansion-Board/
5.5 Grove - AHT20 I2C Industrial Grade Temperature&Humidity Sensor | Seeed Studio Wiki
https://wiki.seeedstudio.com/Grove-AHT20-I2C-Industrial-Grade-Temperature&Humidity-Sensor/
5.6 Grove - Light Sensor v1.2 - LS06-S Phototransistor Compatible with Arduino- Seeed Studio
https://www.seeedstudio.com/Grove-Light-Sensor-v1-2-LS06-S-phototransistor.html
5.7 代码合集
|