[Digi-Key Follow me Issue 2] Basic tasks and Task 4 Weather and Clock
[Copy link]
Video link:
https://training.eeworld.com.cn/video/38233
I accidentally saw the second phase of Dejie's follow me event on Bilibili before, and saw that I could get cash back. I had never been exposed to this kind of thing before, but I always wanted to try it, so I took this opportunity to sign up for this event. Although the board was postponed because the event was too popular, it was not until October. I also tried this board as soon as possible. Although I have been busy recently and can only spare some free time to complete the task, thanks to the various resources in the community and official guidance, I was able to successfully complete this project.
This project is based on the Adafruit ESP32-S3 TFT Feather board, which has a Wi-Fi module and an IPS display, which can well complete some image display tasks. Since I did not purchase other components, I chose to implement a perpetual calendar and clock weather for Task 4. I chose to use CircuitPython for the entire project.
Task 1: Control the screen to display Chinese
Complete the screen control and display Chinese
Matching device: Adafruit ESP32-S3 TFT Feather
import board
import displayio
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
class DisplayController:
def __init__(self):
self.text_group = displayio.Group()
self.font = bitmap_font.load_font("\wenquanyi_10pt.pcf")
def show_text(self, text, x, y, color):
text_area = label.Label(self.font, text=text, color=color)
text_area.x = x
text_area.y = y
self.text_group.append(text_area)
board.DISPLAY.show(self.text_group)
if __name__ == "__main__":
display_controller = DisplayController()
display_controller.show_text("Task1: Display Chinese\n", 3, 5, 0xFFC0CB)
display_controller.show_text("Hello, World!\n Test Test Test Test Test Test Test Test Test", 3, 20, 0xFFC0CB)
while True:
pass
Task 2: Use network functions
Complete the use of network functions, be able to create hotspots and connect to WiFi
Matching device: Adafruit ESP32-S3 TFT Feather
I divide this module into two parts. The first is to be able to connect to WIFI, then request the current network dynamic public IP through http, and the other is to start a hotspot on the development version. Now let's explain the first task.
import board
import displayio
import wifi
import adafruit_requests
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
import time
WIFI_SSID = "__NUL__"
WIFI_PASSWORD = "no_password"
class WiFiConnect:
def __init__(self, ssid, password):
self.ssid = ssid
self.password = password
wifi.radio.connect(self.ssid, self.password)
class WiFiApp:
def __init__(self, ssid, password):
self.wifi = WiFiConnect(ssid, password)
self.text_group = displayio.Group()
self.font = bitmap_font.load_font("\wenquanyi_10pt.pcf")
def show_connected_text(self):
text_area = label.Label(self.font, text="Task2: Network Connection: WLAN", color=0xFFC0CB)
text_area.x = 10
text_area.y = 5
self.text_group.append(text_area)
text_area1 = label.Label(self.font, text="WiFi Connected!\nIP: " + str(wifi.radio.ipv4_address), color=0xFFC0CB)
text_area1.x = 10
text_area1.y = 20
self.text_group.append(text_area1)
board.DISPLAY.show(self.text_group)
def run(self):
self.show_connected_text()
while True:
pass
if __name__ == "__main__":
app = WiFiApp(WIFI_SSID, WIFI_PASSWORD)
app.run()
The above code is to obtain IP information from the server, which can be roughly divided into the following five steps:
The first step is to send a request to the server and obtain the response information containing timestamp data from the server.
The second step is to extract the JSON formatted string from the response returned by the server and then parse it into JSON data for subsequent processing.
The third step is to extract the IP address information from the parsed JSON data, and then the only thing left is to display the IP address.
The fourth step is to initialize the display library, create a display group, and set display parameters so that the acquired IP information can be displayed in subsequent steps.
Step 5: Use the Label component of the display library to display the extracted IP information on the monitor. The 192.168.9.143 in the figure below is the IP I got when I did it.
Finally, by looping the Label component, the acquired IP information can be continuously displayed on the display, so that it can be always on the display and update the IP in real time.
Here is the result after I ran this code:
Next is the hotspot access
import board
import displayio
import wifi
import time
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
import adafruit_requests
class WiFiConfig:
def __init__(self):
self.AP_SSID = "Huang Da's esp32-s3"
self.AP_PASSWORD = "88888888"
wifi.radio.start_ap(self.AP_SSID, self.AP_PASSWORD)
self.text_group = displayio.Group()
self.font = bitmap_font.load_font("\wenquanyi_10pt.pcf")
self.init_display()
def init_display(self):
text_area = label.Label(self.font, text="Task2: Create Personal Hotspot", color=0xFFC0CB)
text_area.x = 5
text_area.y = 5
self.text_group.append(text_area)
text_area1 = label.Label(self.font, text="ID: "+self.AP_SSID+"\n passwd: "+self.AP_PASSWORD, color=0xFFC0CB)
text_area1.x = 5
text_area1.y = 20
self.text_group.append(text_area1)
board.DISPLAY.show(self.text_group)
def run(self):
while True:
pass
if __name__ == "__main__":
config = WiFiConfig()
config.run()
This code is used to create a hotspot, to create the Wi-Fi password, and its ssiD. You can modify these parameters by changing the SSiD and name above.
In the picture below, I set the password to eight eights for easy testing, and the hotspot name is Huang Da's esp32-s3.
Task 3 Control WS2812B
Use buttons to control the display and color switching of the onboard Neopixel LED
Matching device: Adafruit ESP32-S3 TFT Feather
import board
import displayio
import digitalio
import neopixel
import time
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
# Initialization
button = digitalio.DigitalInOut(board.BUTTON)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
pixel.brightness = 0.5
font = bitmap_font.load_font("\wenquanyi_10pt.pcf")
text_area = label.Label(font, text="Task3: Control the LED", color=0xFFC0CB)
text_area.x = 5
text_area.y = 5
board.DISPLAY.show(text_area)
# LED Control Class
class LEDController:
def __init__(self, pixel):
self.pixel = pixel
self.text_area = text_area # Add text_area attribute
def set_color(self, color_str, color_hex):
self.pixel.fill(color_hex)
self.text_area.text = color_str # Update the displayed text
# Display Class
class Display:
def __init__(self, text_area):
self.text_area = text_area
def show(self, x, y):
self.text_area.x = x
self.text_area.y = y
board.DISPLAY.show(self.text_area)
# Main Class
class LEDApp:
def __init__(self):
self.led = LEDController(pixel)
self.display = Display(text_area)
self.state = 0
def run(self):
while True:
if not button.value and not button_pressed:
button_pressed = True
time.sleep(0.05)
if not button.value:
if self.state == 0:
self.led.set_color("RED", 0xFF0000)
elif self.state == 1:
self.led.set_color("GREEN", 0x00FF00)
elif self.state == 2:
self.led.set_color("BLUE", 0x0000FF)
elif self.state == 3:
self.led.set_color("WHITE", 0xFFFFFF)
else:
self.led.set_color("NULL", 0x000000)
self.state = (self.state + 1) % 4
self.display.show(20, 20)
elif button.value:
button_pressed = False
The main function of this code is to control the LED using the NeoPixel library, by creating pixels and connecting them to the pins on the board to control the LED light. These are all in the official tutorial, so I won't go into detail. I have set it to light up by pressing the button on the board, with four color changes, namely red, blue, yellow and white. The result is shown in the figure below.
Task 4 Calendar & Clock
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
import board
import displayio
import wifi
import adafruit_requests
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
import time
import ssl
import socketpool
import adafruit_requests
WIFI_SSID = "__NUL__"
WIFI_PASSWORD = "no_password"
wifi.radio.connect(WIFI_SSID, WIFI_PASSWORD)
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
def getWeather():
response = requests.get(JSON_XINZHI_URL)
weather = response.json()
results = weather['results']
location = results[0]['location']
now = results[0]['now']
res_str = "城市:"+location['name']+"\n"
res_str += "Weather conditions:"+now['text']+"\n"
res_str += "温度:"+now['temperature']+"°C"+"\n"
return res_str
def getTime():
response = requests.get(JSON_TIME_URL)
timejson = response.json()
return timejson['sysTime2']+"\n"
def show():
display = board.DISPLAY
board.DISPLAY.brightness = 1
board.DISPLAY.rotation = 0
font = bitmap_font.load_font("wenquanyi_10pt.pcf")
color = 0xFFC0CB
text_group = displayio.Group()
text_area = label.Label(font, text=dis_str, color=color)
text_area.x = 0
text_area.y = 10
text_area.line_spacing = 1
text_area.scale = 1
text_group.append(text_area)
display.show(text_group)
while True:
dis_str = ""
dis_str += getWeather()
dis_str +=getTime()
show()
time.sleep(10)
First of all, the first step to realize this function is to connect to the Internet. I have already achieved it in the second task, so the code can be used directly.
In the second step, I request the server to obtain the time and call the official Adfruit library to achieve it.
The third step is to get the current weather. Since the weather API is not free to use, I referred to the usage of other people in the forum and called the API of Xinzhi Weather. Through similar operations as the second step, I can get relevant information.
In the above code, I set the data update time to once every 1 second, but the number of digits displayed is only minutes (so the original setting is once every ten seconds...)
I am in Guangzhou, the weather is cloudy, the temperature is 25 degrees, and the time is 0017. As shown in the picture.
Summarize
Adafruit ESP32-S3 TFT Feather is a development board I have come into contact with. Thanks to community resources and official documents, I was able to stumble and achieve the corresponding tasks. A lot of things happened here, from a casual glance at B station in August, to the impulse when placing an order, and then after nearly two months of long waiting, I devoted myself to the development of the board in my spare time squeezed by academic pressure and internship. This experience has benefited me a lot. But I also think there are some areas that can be improved, such as the delivery time can be shortened as much as possible, and the difficulty of the task has increased greatly. I don’t know how to start when I first come into contact with it. It would be better if there are some development cases on the start page.
I would like to thank Dejie's follow me event, which gave me the opportunity and motivation to get in touch with embedded development. I hope there will be more such events in the future. I will also promote Dejie's events in the future, hoping to take this opportunity to let more people get in touch with embedded programming, and let more people who lack courage like me have a chance to truly get in touch with and participate in embedded development.
Video link:
Downloadable code link
|