项目视频: https://training.eeworld.com.cn/video/38855
必做任务1:使用MicroPython系统【对该任务的介绍、功能对应的主要代码片段及说明、功能展示及说明、心得体会建议】
并给开发板刷写MicroPython系统,完成入门程序的运行
这里采用Thonny环境搭建micropython
下载安装Thonny,4.1.4版本安装不上,最后安装3.3.13版本
我通过在线安装方式,开发板刷写MicroPython系统,完成入门程序的运行,不用下载软件,点击就能完成。
https://dev.16302.com/tools/#/
安装完成后,编写一个 hello world 程序,结果如图
必做任务2:驱动扩展板上的OLED屏幕【对该任务的介绍、功能对应的主要代码片段及说明、功能展示及说明、心得体会建议】
首先通过THONNY集成开发环境加载SSD1306的库文件
显示一个方框,内部写一段文字”Follow me 3”。
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("----------------", 0, 0)
oled.text("Follow me 3", 10, 20)
oled.text("----------------", 0, 40)
oled.rect(0,10, 128,54, 1)
oled.show()# Show the text
结果如图
必做任务3:控制蜂鸣器播放音乐【对该任务的介绍、功能对应的主要代码片段及说明、功能展示及说明、心得体会建议】
IO5脚直接驱动蜂鸣器就行
from machine import Pin, PWM
import time
buzzer = PWM(Pin(5, Pin.OUT))
buzzer.freq(5000)
while True:
buzzer.duty_u16(32767)
time.sleep(1)
buzzer.duty_u16(0)
time.sleep(1)
结果见视频
必做任务4:连接WiFi网络【对该任务的介绍、功能对应的主要代码片段及说明、功能展示及说明、心得体会建议】
ESP 32的最大优势就是联网功能强大,有库支持使用也很简单。
先安装WiFi支持库network
通过WiFi连接授时网站,显示时间和日期。
from machine import Pin, SoftI2C
import ssd1306
from time import sleep
import time
import network
import urequests
import ujson
# ESP32 Pin assignment
# i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
# ESP8266 Pin assignment
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)
# Network settings
wifi_ssid = "100"
wifi_password = "87654321"
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()
# Continually try to connect to WiFi access point
while not station.isconnected():
# Try to connect to WiFi access point
print("Connecting...")
station.connect(wifi_ssid, wifi_password)
time.sleep(10)
# Display connection details
print("Connected!")
print("My IP Address:", station.ifconfig()[0])
while True:
# Perform HTTP GET request on a non-SSL web
response = urequests.get(url)
# Check if the request was successful
if response.status_code == 200:
# Parse the JSON response
data = ujson.loads(response.text)
# Extract the "datetime" field for New York
ny_datetime = data["datetime"]
# Split the date and time components
date_part, time_part = ny_datetime.split("T")
# Get only the first two decimal places of the time
time_part = time_part[:8]
# Get the timezone
timezone = data["timezone"]
# Clear the OLED display
oled.fill(0)
# Display the New York date and time on separate lines
oled.text("XIAN Date:", 0, 0)
oled.text(date_part, 0, 10)
oled.text("XIAN Time:", 0, 20)
oled.text(time_part, 0, 30)
oled.text("Timezone:", 0, 40)
oled.text(timezone, 0, 50)
# Update the display
oled.show()
else:
oled.text("Failed to get the time for XIAN!")
# Update the display
oled.show()
设置好自己wifi的用户名和密码,安装好外接天线,联网过程稍微慢一些,结果如下图
必做任务5:使用外部传感器【对该任务的介绍、功能对应的主要代码片段及说明、功能展示及说明、心得体会建议】
使用温湿度传感器和光照传感器采集数据,显示在OLED上。
首先安装ahtx0库文件,用来支持AHT20 温湿度传感器,是IIC接口,光照传感器是模拟量接口,两个传感器接到对应的扩展口上,不要接错了。
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("Light:", 0, 48)
oled.text("Temp:", 0, 16)
oled.text("Humi:", 0, 32)
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)
运行结果见下图
Seeed Studio XIAO开发板虽然体积小但功能强大,得益于 ESP32C3芯片主频高,存储也不小、联网功能还强,结合不错的低功耗特性,在可穿戴设备开发方面优势不少。
源码已上传