Project video: https://training.eeworld.com.cn/video/38855
Required Task 1: Use MicroPython system [Introduction to the task, main code snippets and descriptions of the functions, function display and description, experience and suggestions]
And flash the MicroPython system to the development board to complete the operation of the entry program
Here, we use the Thonny environment to build micropython.
Download and install Thonny. The 4.1.4 version cannot be installed. Finally, the 3.3.13 version is installed
I installed the MicroPython system on the development board through online installation and completed the running of the entry-level program. There is no need to download the software, just click to complete it.
https://dev.16302.com/tools/#/
Click the Initialize Device (Firmware) Tool installation prompt to proceed.
After the installation is complete, write a hello world program, the result is as shown in the figure
Required Task 2: Drive the OLED screen on the expansion board [Introduction to the task, main code snippets and descriptions of the functions, function display and description, experience and suggestions]
First, load the library file of SSD1306 through the THONNY integrated development environment
A box is displayed with the text "Follow me 3" written inside.
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
The results are shown in the figure
Required Task 3: Control the buzzer to play music [Introduction to the task, main code snippets and descriptions of the functions, function display and description, experience and suggestions]
IO5 pin can directly drive the buzzer
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)
Results in the video
Required Task 4: Connect to WiFi network [Introduction to the task, main code snippets and descriptions of the functions, function display and description, experience and suggestions]
The biggest advantage of ESP 32 is its powerful networking capabilities, and it is also very easy to use with library support.
First install the WiFi support library network
Connect to the timing website via WiFi to display time and date.
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()
After setting up your own wifi username and password and installing the external antenna, the networking process is slightly slower, as shown below.
Required Task 5: Use external sensors [Introduction to the task, main code snippets and descriptions of the functions, function demonstration and description, experience and suggestions]
Use temperature, humidity and light sensors to collect data and display it on the OLED.
First install the ahtx0 library file to support the AHT20 temperature and humidity sensor, which is an IIC interface. The light sensor is an analog interface. The two sensors are connected to the corresponding expansion ports. Don't connect them incorrectly.
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)
The operation results are shown in the figure below.
Experience
Although the Seeed Studio XIAO development board is small in size, it is powerful. Thanks to the high main frequency of the ESP32C3 chip, large storage capacity, strong networking capabilities, and good low power consumption characteristics, it has many advantages in the development of wearable devices.
Source code uploaded
https://download.eeworld.com.cn/detail/xscc/630303
|