- 2023-12-15
-
发表了主题帖:
【得捷电子Follow me第3期】任务提交
在此再次感谢EEworld和DigiKey举办的Follow me系列第3期活动
第一部分
https://training.eeworld.com.cn/video/38620
第二部分
任务一 使用MicroPython系统,并完成入门程序的运行
Esptool:Tools | Espressif Systems
mpy固件:MicroPython - Python for microcontrollers
安装过程
运行简单代码
任务二 驱动扩展板上的OLED屏幕显示文字和图形
OLED驱动:GitHub - stlehmann/micropython-ssd1306: A fork of the driver for SSD1306 displays to make it installable via upip
字体:GitHub - maysrp/ssd1306_font: ssd1306 oled font micropython
from machine import Pin, SoftI2C
from ssd1306 import SSD1306_I2C
from font import Font
i2c = SoftI2C(scl=Pin(7), sda=Pin(6))
screen = SSD1306_I2C(128, 64, i2c)
f=Font(screen)
screen.fill_rect(0, 0, 128, 64, 1)
screen.fill_rect(2, 2, 124, 60, 0)
f.text("hello,world!",15,25,16)
screen.show()
任务三 控制蜂鸣器播放音乐
import time
from machine import PWM,Pin
buzzer = PWM(Pin(5),duty=0)
fq = {
'L1':131, 'L2':147, 'L3':165, 'L4':175, 'L5':196, 'L6':220, 'L7':247,
'1':262, '2':294, '3':330, '4':349, '5':392, '6':440, '7':494,
'H1':523, 'H2':587, 'H3':659, 'H4':698, 'H5':784, 'H6':880, 'H7':988,
}
song = ['3','4',
'5','-','H3','-','H1','-','H2','H1','H1','-','7','-','7','-','2','3',
'4','-','H2','-','7','-','H1','7','6','-','5','-','5','-','3','4',
'5','-','H1','H2','H3','-','H2','H1','6','-','H2','H3','H4','-','H3','H2',
'5','-','H4','-','H3','-','H2','-','H1','-','-','-','-','-','-','-',
'H1','-','-','H1','H3','-','H1','-','H2','-','-','H2','H2','-','-','-',
'H2','-','-','H2','H4','-','H2','-','H3','-','-','H3','H3','-','-','-',
'H3','-','-','H3','H5','-','H3','-','H4','-','-','H4','H4','-','H3','H2',
'5','-','H4','-','H3','-','H2','-','H1','-','-','-','-','-','-','-']
def play(md, buzzer=buzzer, dt=500, dl=0.24, bk=0.01):
for t in md:
if t == '-':
time.sleep(bk+dl)
elif t == '0':
buzzer.duty(0)
time.sleep(bk+dl)
else:
buzzer.duty(0)
time.sleep(bk)
buzzer.duty(dt)
buzzer.freq(fq[t])
time.sleep(dl)
buzzer.duty(0)
play(song)
运行详情请见视频,在此不作展示
任务四 连接WiFi网络,并访问互联网信息
import network, ntptime, time
from machine import RTC, Pin, SoftI2C
from ssd1306 import SSD1306_I2C
from font import Font
ssid = "YOURSSID"
password = "YOURPASSWORD"
nw = network.WLAN(network.STA_IF)
nw.active(True)
nw.connect(ssid, password)
print(nw.ifconfig())
def fix_time():
while True:
try:
ntptime.settime()
break;
except:
print('trying...')
print('Time Fixed.')
def display_time(screen):
screen.fill_rect(0, 0, 128, 64, 1)
screen.fill_rect(2, 2, 124, 60, 0)
now = time.gmtime()
f.text("%d-%02d-%02d"%(now[0],now[1],now[2]+int(now[3]/16)),25,16,16)
f.text("%02d:%02d:%02d"%((now[3]+8)%24,now[4],now[5]),32,38,16)
screen.show()
i2c = SoftI2C(scl=Pin(7), sda=Pin(6))
screen = SSD1306_I2C(128, 64, i2c)
f=Font(screen)
fix_time()
while True:
display_time(screen)
time.sleep(1)
任务五 使用外部传感器
(1)温湿度传感器
AHTX0驱动:micropython-ahtx0 · PyPI
import network, ntptime, time
import ahtx0
from machine import RTC, Pin, SoftI2C
from ssd1306 import SSD1306_I2C
from font import Font
ssid = "YOURSSID"
password = "YOURPASSWORD"
nw = network.WLAN(network.STA_IF)
nw.active(True)
nw.connect(ssid, password)
print(nw.ifconfig())
def fix_time():
while True:
try:
ntptime.settime()
break;
except:
print('trying...')
print('Time Fixed.')
def get_time():
now = time.gmtime()
return (now[0],now[1],now[2]+int(now[3]/16),(now[3]+8)%24,now[4],now[5])
def get_value(sensor):
while True:
try:
return (sensor.temperature,sensor.relative_humidity)
break;
except:
print('trying...')
print('Sensor Value Fixed.')
def display(screen, now, values):
screen.fill_rect(0, 0, 128, 64, 1)
screen.fill_rect(2, 2, 124, 60, 0)
f.text("%02d-%02d %02d:%02d"%(now[1],now[2],now[3],now[4]),20,8,16)
f.text("T:%.2f"%(values[0]),36,25,16)
f.text("H:%.2f"%(values[1]),36,40,16)
screen.show()
i2c = SoftI2C(scl=Pin(7), sda=Pin(6))
screen = SSD1306_I2C(128, 64, i2c)
sensor = ahtx0.AHT10(i2c)
f=Font(screen)
fix_time()
while True:
display(screen, get_time(), get_value(sensor))
time.sleep(5)
(2)环境光传感器
import network, ntptime, time
import ahtx0
from machine import RTC, ADC, Pin, SoftI2C
from ssd1306 import SSD1306_I2C
from font import Font
ssid = "YOURSSID"
password = "YOURPASSWORD"
nw = network.WLAN(network.STA_IF)
nw.active(True)
nw.connect(ssid, password)
print(nw.ifconfig())
def fix_time():
while True:
try:
ntptime.settime()
break;
except:
print('trying...')
print('Time Fixed.')
def get_time():
now = time.gmtime()
return (now[0],now[1],now[2]+int(now[3]/16),(now[3]+8)%24,now[4],now[5])
def get_value(sensor):
return (sensor.read_u16(), sensor.read_uv()/1000)
def display(screen, now, values):
screen.fill_rect(0, 0, 128, 64, 1)
screen.fill_rect(2, 2, 124, 60, 0)
f.text("%02d:%02d:%02d"%(now[3],now[4],now[5]),30,8,16)
f.text("ADC:%d"%(values[0]),28,25,16)
f.text("UV:%d"%(values[1]),40,40,16)
screen.show()
i2c = SoftI2C(scl=Pin(7), sda=Pin(6))
screen = SSD1306_I2C(128, 64, i2c)
sensor = ADC(Pin(2), atten=ADC.ATTN_11DB)
f=Font(screen)
fix_time()
while True:
display(screen, get_time(), get_value(sensor))
time.sleep(1)
第三部分
源码下载:download.eeworld.com.cn/detail/bjm212501/630287
提示:源码中SSID和PASSWORD需要自行更改
心得体会
本次活动的Seeed Studio XIAO ESP32C3主板使用了用途广泛的esp32,并且有非常完整的开发文档,为我们操作查错提供了很大的便利,配套的扩展版功能强大,用途广泛,扩展性强。首次体验micropython语言编程,操作简单,开放度高,学习性及可玩性强,非常感谢eeworld和digikey举办此次活动,并希望活动举办更多类似活动。
-
加入了学习《【得捷电子Follow me第3期】视频汇报》,观看 视频汇报
-
加入了学习《得捷Follow me第3期-总结视频》,观看 得捷Follow me第3期-总结视频
-
上传了资料:
【得捷电子Follow me第3期】源码
-
加入了学习《 【得捷电子Follow me第2期】视频汇报》,观看 汇报视频