Digi-Key Electronics Follow me 2nd issue summary [revised]
[Copy link]
This event is actually very suitable for novice practice. The project is relatively simple. I chose to take this opportunity to complete the project with CircuitPython. Since it was my first time using CircuitPython for development, I was learning while developing. This project summary report is a teaching article. If you want to try CircuitPython development, you can practice through this event.
1. Introduction to development board and development environment
First, let me introduce the board Adafruit ESP32-S3 TFT Feather. Once you get the board, you just need to download the circuit python firmware. The download method is also very simple. Quickly press the RST button twice to enter UF2 mode, and then put the circuit python firmware into the USB drive you found. During the development process, if you want to debug, you can of course choose online debugging, or choose to combine with other IDEs, but I found that this development board is quite intimate. We can directly see the error report on the built-in LCD screen, and we can barely complete the development.
2. Preparation
With a convenient development board and development environment, the subsequent work is relatively simple. However, there is relatively little information on circuit python, so there are some issues to discuss.
- Our development board model is Feather ESP32-S3 TFT PSRAM. Don’t make the wrong choice when selecting the firmware. I’ll also post the official website address https://circuitpython.org/board/adafruit_feather_esp32s3_tft/
Remember to choose the .uf2 file to download.
- After flashing the firmware, our project structure looks scary with a lot of stuff, but it's nothing. The main thing is code.py, which is the core code of our project and is used to run the program.
The lib folder is used to store our various module files, so that we can give full play to the power of Python.
Other spaces can be used to store various other resources, such as the background picture I want to use in this project.
- Calling various libraries
The adafruit_led_animation library can easily control LED dynamic effects
The displayio library is a CircuitPython built-in library used to control screen output
adafruit_bitmap_font library for loading custom fonts
3. Function introduction and implementation
Next is the function introduction. I chose "Subtask 1: Calendar & Clock - Complete a perpetual calendar clock that can be updated via the Internet and display local weather information". The task is relatively simple and can be completed after completing the subtask, which is very suitable for us. "Task 1: Control the screen to display Chinese" is used to display the weather, "Task 2: Use the network function" is used to obtain the regional weather, and "Task 3: Control WS2812B" is used to control the LED.
Task 1: Control the screen to display Chinese
# Import the label from the external library adafruit_display_text to display the label
from adafruit_display_text import label
# Load the font and define the font color
font = bitmap_font.load_font("/font/sytq_16.pcf")
nun_font = bitmap_font.load_font("/font/DingTalk_ncn_60.pcf")
color = 0xf26957
# Initialize date labels and set x, y axis plot coordinates, then add labels to the image group
date = label.Label(font, text="", color=color)
date.x = 50
date.y = 20
group.append(date)
The label.Label method can be used to display content such as:
date = label.Label(font, text="Tuesday, October 17", color=color) can display "Tuesday, October 17" on the screen.
Task 2: Network Function Usage
register
# Import the os library to obtain wifi information
import os
# Import the rtc library to implement the RTC clock
import rtc
# Import wifi and time libraries for backup
import wifi
import time
# Import network library for backup
import ssl
import socketpool
import adafruit_ntp
import adafruit_requests
# Use the os.getenv function to get the wifi ssid and password from the setting.toml file
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
Then comes the most important thing, which is to write the settings.tmol file. Write in the WiFi password and WiFi name, and then you can see the displayed IP address.
Task 3: Control WS2812B (Required Task)
# Import DigitalInOut, Direction, Pull from the digitalio built-in library
from digitalio import DigitalInOut, Direction, Pull
# Import the neopixel built-in library
import neopixel
# import Blink from the adafruit_led_animation.animation.blink library
from adafruit_led_animation.animation.blink import Blink
# Import five color definitions from the adafruit_led_animation.color library
from adafruit_led_animation.color import JADE, BLACK, ORANGE, GOLD, OLD_LACE
# Initialize pixel light pins
pixel_pin = board.NEOPIXEL
# Set the number of pixel lights. This will make it easier to modify the code later, such as connecting an external ws2812 light strip or matrix
num_pixels = 1
# Initialize pixel lights
pixels = neopixel.NeoPixel(
pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=neopixel.GRB
)
# Initialize blink dynamic effect
blink = Blink(pixels, speed=0.5, color=BLACK)
# Initialize the btn button object
btn = DigitalInOut(board.BOOT0)
# Set the btn pin as input
btn.direction = Direction.INPUT
# Set the btn pin to pull up
btn.pull = Pull.UP
Weather Clock
Since we use Amap's weather API, let's take a look at how to use it. It's actually very simple http
send
return
# Use adafruit_ntp.NTP function to initialize the ntp service. The first function is to determine the network connection port.
# The second function sets the time zone. China is in +8 time zone. The third function is used to specify the ntp server address.
pool = socketpool.SocketPool(wifi.radio)
ntp = adafruit_ntp.NTP(pool, tz_offset=8, server="ntp.aliyun.com")
# Use ntp time to update system time
rtc.RTC().datetime = ntp.datetime
def get_wday(wday):
if (wday == 0):
return "Monday"
elif (wday == 1):
return "Tuesday"
elif (wday == 2):
return "Wednesday"
elif (wday == 3):
return "Thursday"
elif (wday == 4):
return "Friday"
elif (wday == 5):
return "Saturday"
elif (wday == 6):
return "Sunday"
# Initialize the requests object
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
def get_weather():
# Set the city id
city = "340100"
# This function uses the Amap API. To use this API, you need to register a related account and apply for a key first.
key = "0154a9c43e2b7d940f39e72a07c599c8"
# Concatenate weather link url
getweather_url = " https://restapi.amap.com/v3/weather/weatherInfo?city= " + city + "&key=" + key
# Get weather json data
response = requests.get(getweather_url)
json_resp = response.json()
# Close the connection
response.close()
# Parse json data and return temperature and weather information
for da in json_resp["lives"]:
#print(da["temperature"])
return da["temperature"], da["weather"]
# First create a status variable to get weather information when the device starts
status = "boot"
# Define the led_color variable for switching between five colors
led_color = 0
# Get the local RTC time once a second
t = time.localtime()
# When the first startup or the minute attribute of the local RTC time is 0, update the date label and weather label
if (status == "boot" or t.tm_min == 0):
# Update date tag
date.text = "%d month %d day" % (t.tm_mon, t.tm_mday)
week.text = get_wday(t.tm_wday)
# Get weather information
str_t, str_tz = get_weather()
# Update temperature labels
temp.text = "%s°" % (str_t)
# Update weather labels
tempzh.text = str_tz
status = "updated"
# Update the clock label every 1 second for dynamic display
if (t.tm_sec % 2 == 0):
timeL.text = "%02d:%02d" % (t.tm_hour, t.tm_min)
timeL.color = 0xf26957
else:
timeL.text = "%02d:%02d" % (t.tm_hour, t.tm_min)
timeL.color = 0xD9D7C9
# Refresh the screen
display.show(group)
# Sleep for 1 second
time.sleep(1)
Finally, let's take a look at the complete code. It was a bit messy when I first used Python for development, but the final effect was good. I really like circuit python. It is much better than hard-coding C language. Especially the various library functions of Python can be applied, which greatly facilitates our development difficulty. Developers like me who are new to circuit python can also get started quickly. I am very grateful for this opportunity to learn a lot of new knowledge, especially the development board is very attractive. I hope that more events can be held and the better they are.
Here is my project video: https://training.eeworld.com.cn/video/38035
This link is my project code: https://download.eeworld.com.cn/detail/khalilduboa/629655?type__2276=n4mx27D%3D3QqrG8DlxGr8D9DRxGqopjhp0AoD&alichlgref=http%3A%2F%2Fen.eeworld.com/bbs%2Fmy%2Fhome.php%3Fcur%3Dmyhome%26act%3Ddownload
|