【Digi-Key Follow me Issue 3】Task Submission
[Copy link]
Thanks again to EEworld and DigiKey for hosting the 3rd Follow me series event
first part
https://training.eeworld.com.cn/video/38620
the second part
Task 1: Use the MicroPython system and complete the running of the entry program
Esptool:Tools | Espressif Systems
mpy固件:MicroPython - Python for microcontrollers
Installation process
Run a simple code
Task 2: Drive the OLED screen on the expansion board to display text and graphics
OLED驱动:
字体:
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()
Task 3: Control the buzzer to play music
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)
Please see the video for details of operation, which will not be shown here
Task 4: Connect to WiFi network and access Internet information
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)
Task 5 Using External Sensors
(1) Temperature and humidity sensor
AHTX0 driver: 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) Ambient light sensor
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)
the third part
Source code download: download.eeworld.com.cn/detail/bjm212501/630287
Tip: SSID and PASSWORD in the source code need to be changed by yourself
Experience
The Seeed Studio XIAO ESP32C3 motherboard used the widely used ESP32 and has very complete development documentation, which greatly facilitated our operation and error checking. The supporting expansion board is powerful, widely used and highly extensible. This is my first experience with MicroPython programming, which is simple to operate, highly open, and highly learnable and playable. I am very grateful to EEWorld and DigiKey for hosting this event, and I hope that the event will host more similar events.
|