[Digi-Key Follow me Issue 2] Xiaohui's task submission (full content)
[Copy link]
This post was last edited by louislouis on 2023-10-15 09:53
First of all, I would like to thank the event organizers Digi-Key Electronics and eeworld for giving us the opportunity to experience the latest electronic products and appreciate the charm of electronic technology.
This project includes the following:
Task 1 : Control the screen to display Chinese (mandatory task)
Complete the screen control and display Chinese
Matching device: Adafruit ESP32-S3 TFT Feather
Task 2 : Use of network functions (mandatory task)
Complete the use of network functions, be able to create hotspots and connect to WiFi
Matching device: Adafruit ESP32-S3 TFT Feather
Task 3 : Control WS2812B (required task)
Use buttons to control the display and color switching of the onboard Neopixel LED
Matching device: Adafruit ESP32-S3 TFT Feather
Task 4 : Choose 1 task you are interested in from the 5 tasks below and complete it (mandatory task)
■ Subtask 1: Calendar & Clock - Complete a perpetual calendar clock that can be updated via the Internet and display local weather information
Recommended device: Adafruit ESP32-S3 TFT Feather
Task/Project Summary Report:
Software part:
This development board supports Python language programming. You can program and debug with the help of the IDE provided by the browser. You can complete the program writing by modifying the code.py file and saving it through the browser.
Compared with traditional single-chip microcomputers and embedded microprocessors, the programming method of this development board is more convenient and faster.
Task 1 : Control the screen to display Chinese (mandatory task)
Complete the screen control and display Chinese
Matching device: Adafruit ESP32-S3 TFT Feather
The project uses the onboard screen to display Chinese characters. The display of Chinese characters requires the help of a Chinese font library. This project uses a self-made font library.
To use onboard resources, you need to load the board library, and the display function is implemented using the display library. After setting the display parameters, you can use the display.show function to display characters.
The font library can be cut according to the characters actually needed to be displayed, reducing the font file size and ensuring that there is enough space for the program.
code show as below:
import board
import displayio
from adafruit_display_text import label, wrap_text_to_pixels
from adafruit_bitmap_font import bitmap_font
str2display="任务1:控制屏幕显示中文\n出新意与法度之中\n寄妙理于豪放之外\n所谓游刃有余\n运斤成风"
display = board.DISPLAY
board.DISPLAY.brightness = 0.6
font = bitmap_font.load_font("opposans_m_12.pcf")
color = 0xFF7500
background_color = 0x000000
text_group = displayio.Group()
text_area = label.Label(font, text="\n".join(wrap_text_to_pixels(str2display,16)), color=color, background_color=background_color)
text_area.x = 2
text_area.y = 10
text_area.line_spacing = 1.1
text_area.scale = 1
text_group.append(text_area)
display.show(text_group)
while True:
pass
The demonstration effect is as shown in the figure:
Through the experiments in this task, I have a clearer understanding of the principles of character display, which provides ideas for subsequent project development.
Task 2 : Use of network functions (mandatory task)
Complete the use of network functions, be able to create hotspots and connect to WiFi
Matching device: Adafruit ESP32-S3 TFT Feather
The project achieves network access by connecting to a hotspot. After the device connects to the hotspot, it obtains the IP address and related network configuration information, thereby enabling the device to connect to the network.
The use of network function can connect the board to the Internet, and then interact and expand with the Internet.
The program mainly requires wifi, ssl, socketpool and other libraries to implement, and data requests need to be implemented using the adafruit_requests library.
code show as below:
secrets = {
'ssid' : 'hiabia',
'password' : '85523425d6134',
'timezone' : "Asia/Shanghai"
}
import wifi
import ssl
import socketpool
import adafruit_requests
import time
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to {}!".format(secrets["ssid"]))
print("IP:", wifi.radio.ipv4_address)
# 请求URLs
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
JSON_TIME_URL = "http://quan.suning.com/getSysTime.do"
print("ESP32-S2 WebClient Test")
print(f"My MAC address: {[hex(i) for i in wifi.radio.mac_address]}")
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
print(f"Fetching text from {TEXT_URL}")
response = requests.get(TEXT_URL)
print("-" * 40)
print(response.text)
print("-" * 40)
print()
time.sleep(3)
print(f"Fetching and parsing json from {JSON_TIME_URL}")
response = requests.get(JSON_TIME_URL)
print("-" * 40)
print(f"Time: {response.json()['sysTime2']}")
print("-" * 40)
print("Done")
The demonstration effect is as shown in the figure:
By using functions such as connecting to wifi and turning on hotspots, we can have a clearer understanding of how to use the network connection of the development board, providing a foundation for the subsequent development of IoT related functions.
Task 3 : Control WS2812B (required task)
Use buttons to control the display and color switching of the onboard Neopixel LED
The project program must load libraries such as board, time, and neopixel. The board library implements the related functions of onboard resources, the time library implements the processing of time data, and the neopixel library implements the control of ws2812b.
By configuring the brightness information and three primary color information of ws2812b, the fill function can be called to turn the specified color on and off, and the effect is very good.
Matching device: Adafruit ESP32-S3 TFT Feather
code show as below:
import time
import board
import neopixel
import touchio
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
pixel.brightness = 0.3
touch_A1 = touchio.TouchIn(board.D5)
# touch_A2 = touchio.TouchIn(board.D10)
touch_A3 = touchio.TouchIn(board.D12)
grape = (190, 75, 219)
teal = (56, 217, 169)
red = (20, 100, 219)
while True:
if touch_A1.value:
pixel.fill(grape)
time.sleep(0.05)
# if touch_A2.value:
# pixel.fill(teal)
# time.sleep(0.05)
if touch_A3.value:
pixel.fill(red)
time.sleep(0.05)
The demonstration effect is as shown in the figure:
The use of WS28212B to control LED color can help you have a deeper understanding of the communication protocol and the related functions of the IO interface. By using WS2812B, you can control the light more accurately and reliably, reduce the difficulty of code debugging, and improve development efficiency.
Task 4 : Choose 1 task you are interested in from the 5 tasks below and complete it (mandatory task)
I chose subtask 1:
■ Subtask 1: Calendar & Clock - Complete a perpetual calendar clock that can be updated via the Internet and display local weather information
The project displays time, date and weather based on the onboard screen, and calls the third-party weather service interface to obtain real-time weather data.
Use the time library to manage time information. Finally, use a while loop to refresh the data information regularly.
Recommended device: Adafruit ESP32-S3 TFT Feather
code show as below:
import board
import rtc
import wifi
import adafruit_ntp
import os
import ssl
import time
import displayio
import terminalio
import socketpool
import adafruit_requests
from adafruit_display_text import bitmap_label, label
DATA_SOURCE = "http://api.seniverse.com/v3/weather/now.json?key=SG-nLPzA3pyLEy9Tw&location=tianjin&language=en&unit=c"
# --| User Config |--------------------------------
TZ_OFFSET = 8 # time zone offset in hours from UTC
NEO_PIN = board.NEOPIXEL # neopixel pin
NEO_CNT = 1 # neopixel count
# -------------------------------------------------
# Set up TFT display
display = board.DISPLAY
board.DISPLAY.brightness = 0.55
board.DISPLAY.rotation = 0
group = displayio.Group()
time_color = 0xFF0000
weather_color = 0x00FF00
time_area = label.Label(terminalio.FONT, text="Hello", color=time_color)
time_area.x = 2
time_area.y = 10
time_area.line_spacing = 0.8
time_area.scale = 1
weather_area = label.Label(terminalio.FONT, text="Weather", color=weather_color)
weather_area.x = 2
weather_area.y = 30
weather_area.line_spacing = 0.8
weather_area.scale = 1
main_group = displayio.Group()
main_group.append(group)
main_group.append(time_area)
main_group.append(weather_area)
# Show the main group on the display
display.show(main_group)
# Connect to local network
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
print("Wifi connected.")
# Get current time using NTP
print("Fetching time from NTP.")
pool = socketpool.SocketPool(wifi.radio)
ntp = adafruit_ntp.NTP(pool, server="ntp.aliyun.com", tz_offset=TZ_OFFSET)
rtc.RTC().datetime = ntp.datetime
#
requests = adafruit_requests.Session(pool, ssl.create_default_context())
# Define time interval between requests
time_interval = 3000 # set the time interval to 30 minutes
# Wait for wake up time
now = time.localtime()
print("Current time: {:2}:{:02}:{:02}".format(now.tm_hour, now.tm_min, now.tm_sec))
response = requests.get(DATA_SOURCE)
while True:
# Fetch weather data from 心知 API
print("Fetching json from", DATA_SOURCE)
if now.tm_sec == 10:
response = requests.get(DATA_SOURCE)
print(response.json())
# Extract temperature and weather condition data from API response
current_temp = response.json()["results"][0]["now"]["temperature"]
current_weather_condition = response.json()["results"][0]["now"]["text"]
current_city = response.json()["results"][0]["location"]["name"]
print("Weather condition: ", current_weather_condition)
time_area.text = "Hello EEWorld\nTime is {:2}:{:02}:{:02}".format(
now.tm_hour, now.tm_min, now.tm_sec
)
weather_area.text="City: " + current_city+"\n"\
+"Temperature: {}".format(current_temp)+"\n"\
+"Weather: "+ current_weather_condition
# just sleep until next time check
time.sleep(0.5)
now = time.localtime()
The demonstration effect is as shown in the figure:
This task is a relatively comprehensive application task, which uses network connection, data request, interface display and other functions, and can be a good exercise for code writing.
While completing the various tasks of the project, we also learned functions such as image display and touch buttons, and experienced the powerful functions of this board. We hope that Digi-Key Electronics and EEWorld can continue to hold more similar events to lead more electronics enthusiasts to experience the fun of electronics.
Compilable downloadable code:
Finally, the project code is as follows:
Project Code
|