439 views|0 replies

3

Posts

0

Resources
The OP
 

[Digi-Key Electronics Follow me Issue 2] + Buy three get one free "Little Overlord" mission collection [Copy link]

 This post was last edited by eew_5QNxi6 on 2023-10-14 22:09

Good evening everyone, I'm a newcomer. This is my first time playing with CircuitPython. I didn't want to come, but my buddy accidentally found out about the follow me2 event held by EEWORLD and Digikey, so I had to join. I can't fight my brothers, so I came. I'm not very skilled, please forgive me, but I will definitely complete the task seriously.

This activity uses the CircuitPython language organized by Adafruit. The CircuitPython language belongs to the MicroPython branch, and Adafruit's motherboards are specially optimized for CP. Therefore, this time we must choose the CircuitPython language to complete the programming. The following is the technical text

1. Project Introduction

1. Introduction to the project objectives:

This mission requires the completion of three required objectives and one optional objective:

1) Output Chinese characters on the screen

2) Wi-Fi function, Wi-Fi connection and broadcasting

3) Onboard colorful LED lights with button display

4) *Select a task

Complete a perpetual calendar clock that can be updated via the Internet and display local weather information

Hardware required: Adafruit ESP32-S3 TFT Feather

2. Project structure introduction:

This task is relatively simple, so we use the "Xiaobawang Collection" format, that is, a one-time code to implement all functions. The following is the program part

import board, displayiortc, wifi, ssl,rtc, wifi
import time as Time
import socketpool
import neopixel
from digitalio import DigitalInOut, Direction, Pull
import adafruit_imageload
import adafruit_ntp
import adafruit_requests
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font

1) First, import the necessary packages, including motherboard driver, clock, wifi, ssl connection pool, colorful LED driver, screen text, picture driver, etc.

#以下代码在根目录settings.toml文件夹中
#连接已知wifi
CIRCUITPY_WIFI_SSID = "自己的wifi热点名称"
CIRCUITPY_WIFI_PASSWORD = "自己的wifi热点密码"

#以下代码在code.py中
##任务 创建热点
wifi.radio.start_ap('Follow_me_wifi', 'EEworld12345678')
print("Wifi raido begin")

2) Regarding the code part of Task 2 WiFi Task, only four lines of code are needed to complete WiFi connection and AP broadcasting

##任务3(板载RGB) 基础设置
pixel_pin = board.NEOPIXEL
pixels = neopixel.NeoPixel(pixel_pin, 1, brightness=0.2, auto_write=False) #定义像素

def pixelsetcolor(c):#改变颜色函数,针对单个像素量身定制
    pixels[0] = c
    pixels.show()

#设置按钮
btn = DigitalInOut(board.BOOT0)
btn.direction = Direction.INPUT
btn.pull = Pull.UP
color = 0#控制rgb



#以下代码在while主循环里面
##任务(RGB) 循环颜色
    if not btn.value:
        if color % 3 == 0:
            pixelsetcolor(0xFF0000)
            print("Pixel is R")
        elif color % 3 == 1:
            pixelsetcolor(0x00FF00)
            print("Pixel is G")
        elif color % 3 == 2:
            pixelsetcolor(0x0000FF)
            print("Pixel is B")
        color += 1
    else:
        pass
    Time.sleep(0.5)#灯光保持0.5秒

3) Code for Task 3: Onboard multi-color LED, where the multi-color LED displays 3 different colors, and a button is used to activate the color of the light (the remainder when divided by 3 corresponds to RGB)

##任务4-天气&时钟代码

display = board.DISPLAY #初始化屏幕
display.rotation = 90  #屏幕旋转90度
group = displayio.Group() #定义显示组别
# 加载图片
image, palette = adafruit_imageload.load("/img/snake.bmp")
# 是否开启透明
palette.make_transparent(1)

# 创建图片布局
grid = displayio.TileGrid(image, pixel_shader=palette,width=1, height=1)

# 将图片布局添加到图像组,由于是第一个添加的,默认是最下层
group.append(grid)

#加载必要字体
font = bitmap_font.load_font("/font/sytq_16.pcf")#此字体为群友龙哥制作

#加载文字标签函数
def labletext(x, y, text, font=font, color=0x0000FF,scale=1): 
    return label.Label(font, text=text, color=color, x=x, y=y,scale=scale)

#将文字添加到显示组
#设置文字标签
date = labletext(0, 100, "0月0日",color=0x0000FF)
day = labletext(95, 100, "周八",color=0x0000FF)
time = labletext(0, 125, "00:00:00",color=0x00FFFF)
weather = labletext(0, 160, "?+/",color=0xFFFFFF,scale=2)
temp = labletext(60, 200, "00°",color=0xFFFFFF,scale=2)
#加入显示组
group.append(date)
group.append(day)
group.append(time)
group.append(weather)
group.append(temp)


# 联网获取时间与天气信息
pool = socketpool.SocketPool(wifi.radio) #初始化
ntp = adafruit_ntp.NTP(pool, tz_offset=8, server="ntp.aliyun.com") #校正时间
rtc.RTC().datetime = ntp.datetime #校正时间

requests = adafruit_requests.Session(pool, ssl.create_default_context()) #定义请求

def get_weather():#获取天气函数
    city = "325600"#地区代码
    key = "1joijinji23ji23257k7kj56h7k5"#此处自行填写
    url = "https://restapi.amap.com/v3/weather/weatherInfo?city=" + city + "&key=" + key
    response = requests.get(url)#获取
    json_resp = response.json()
    response.close()
    return json_resp["lives"][0]["temperature"], json_resp["lives"][0]["weather"]#提取参数


#以下代码在while主循环里面
   ##任务4 自动刷新标签
    n = Time.localtime()
    if (state == "init" or n.tm_min == 0):
        date.text = "%d月%d日" % (n.tm_mon, n.tm_mday)
        day.text = days[n.tm_wday]
        t, w = get_weather()
        weather.text = w+'/'
        temp.text = "%s°" % (t)
        state = "running"
    #每次循环刷新
    time.text = "%02d:%02d:%02d" % (n.tm_hour, n.tm_min, n.tm_sec)
    display.show(group)

4) Regarding the comprehensive code of Task 1 & Task 4, since the weather clock naturally includes Chinese character display, first determine the displayed image settings (size, position, etc.), determine the text display information, but the output function display.show(group) of the text screen is in the while main loop.

2. Achievements Display

Graphics

[Digi-Key Electronics Follow me Issue 2] Subor Collection - EEWORLD University Hall

video

Conclusion

After studying the second phase of Digi-Key Follow Me, I learned a faster and higher-quality way to develop microcontrollers. I really gained a lot! I will introduce a few more students to learn next time.

4. Code Attachment

F&M2_code_whl.zip (140.37 KB, downloads: 1)
This post is from DigiKey Technology Zone
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list