460|0

8

帖子

2

TA的资源

一粒金砂(中级)

楼主
 

【得捷电子Follow me第2期】+任务四 日历与时钟 [复制链接]

  本帖最后由 liuvx 于 2023-11-9 16:45 编辑

在本次任务中,我们需要完成在我们的屏幕上显示时间与天气等信息

先放张效果图

我们来分步进行

首先是先完成文字部分

屏幕显示部分

需要的库可以自行查看之前的任务一

先设置屏幕的一些基本参数

# 设置板子的亮度和是否翻转
board.DISPLAY.brightness = 0.5  #
board.DISPLAY.rotation = 0  #
word_font = bitmap_font.load_font("wenquanyi_13px.pcf")

咱们可以在这个任务中增加一张图片来使其更加美观

需要这个库

import adafruit_imageload

然后引入图片,注意图片需要是bmp格式的

# 引入图片
image, palette = adafruit_imageload.load(
    '/images/ysqd.bmp', bitmap=displayio.Bitmap, palette=displayio.Palette)
tile_grid = displayio.TileGrid(image, pixel_shader=palette)
group = displayio.Group()
group.append(tile_grid)

然后再显示其他文字,如时间,温度啥的

# 日期的设置
date = label.Label(word_font, text="test", color=0x000000)
date.x = 10  #
date.y = 20
date.scale = 2  #
group.append(date)

# 周几的设置
week = label.Label(word_font, text="周五", color=0x000000)
week.x = 120
week.y = 20
week.scale = 2
group.append(week)

# 时间的设置
timer = label.Label(word_font, text='10:17', color=0x779649)
timer.x = 15
timer.y = 65
timer.scale = 2  #
group.append(timer)

# 天气文字的设置
weather = label.Label(word_font, text='阴', color=0x3271ae)
weather.x = 10
weather.y = 110
weather.scale = 2  #
group.append(weather)

# 温度文字的设置
temperature = label.Label(word_font, text='10度-15度', color=0x3271ae)
temperature.x = 60
temperature.y = 110
temperature.scale = 2
group.append(temperature)

# 事实温度文字的设置
tem_now = label.Label(word_font, text='10度', color=0x3271ae)
tem_now.x = 150
tem_now.y = 65
tem_now.scale = 2
group.append(tem_now)

最后再显示即可

board.DISPLAY.show(group)

WIFI连接

和之前类似,在SETTING这个文件中配置ssid和password

# 连接WIFI,若连接失败会报错
print("Connecting to", ssid)
wifi.radio.connect(ssid, password)
time.sleep(1)
print("Connected to", ssid)

延时1s的目的为了使能够连接WiFi后再进行后面的操作

获取信息

相关库介绍

adafruit_ntp

adafruit_requests

rtc

 

获得天气这个需要从其他地方返回数据获得

我这里使用的是易客云API,比较方便和稳定

代码部分

# 周几文字的数据
def get_wday(week_today):
    if week_today == 0:
        return "周一"
    elif week_today == 1:
        return "周二"
    elif week_today == 2:
        return "周三"
    elif week_today == 3:
        return "周四"
    elif week_today == 4:
        return "周五"
    elif week_today == 5:
        return "周六"
    elif week_today == 6:
        return "周日"

再利用RTC来获得时间

# 使用rtc
rtc.RTC().datetime = adafruit_ntp.NTP(socketpool.SocketPool(
    wifi.radio), tz_offset=8, server='ntp.aliyun.com').datetime

从API获取天气情况

def get_weather():
    requests = adafruit_requests.Session(
        socketpool.SocketPool(wifi.radio), ssl.create_default_context())
    appid = os.getenv("appid")
    appsecret = os.getenv("appsecret")
    res = requests.get(
        'https://v0.yiketianqi.com/free/day?appid=%s&appsecret=%s&unescape=1&city=南阳' % (appid, appsecret)).json()
    return res['wea'], res['tem_night'], res['tem_day'], res['tem']

注意这里的appid和appsecret需要从易客云获取并写入settings这个文件中

否则会无法正常获得温度

再进行循环之前先获取一遍信息

# 获得时间
time_now = time.localtime()
timer.text = '%02d:%02d' % (time_now.tm_hour, time_now.tm_min)
date.text = '%d月%d日' % (time_now.tm_mon, time_now.tm_mday)
week.text = get_wday(time_now.tm_wday)
wea, tem_night, tem_day, tem = get_weather()
weather.text = wea
temperature.text = '%s℃-%s℃' % (tem_night, tem_day)
tem_now.text = '%s度' % (tem)

在进入循环后需要对获取信息的速度加以限制

while True:
    time_now = time.localtime()
    timer.text = '%02d:%02d' % (time_now.tm_hour, time_now.tm_min)
    if time_now.tm_min == 0:
        wea, tem_night, tem_day, tem = get_weather()
        weather.text = wea
        temperature.text = '%d℃-%d℃' % (tem_night, tem_day)
        tem_now.text = '%s度' % (tem)
    if time_now.tm_hour == 0:
        date.text = '%d月%d日' % (time_now.tm_mon, time_now.tm_mday)
        week.text = get_wday(time_now.tm_wday)

    pass

这样就OK了

完整代码如下

import board
import displayio
import os
import wifi
import ssl
import socketpool
import adafruit_ntp
import rtc
import time
import ipaddress
import adafruit_requests
import adafruit_imageload
from adafruit_display_text import label, wrap_text_to_lines
from adafruit_bitmap_font import bitmap_font
# 获得WIFI名字和密码
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")


# 周几文字的数据
def get_wday(week_today):
    if week_today == 0:
        return "周一"
    elif week_today == 1:
        return "周二"
    elif week_today == 2:
        return "周三"
    elif week_today == 3:
        return "周四"
    elif week_today == 4:
        return "周五"
    elif week_today == 5:
        return "周六"
    elif week_today == 6:
        return "周日"


# 连接WIFI,若连接失败会报错
print("Connecting to", ssid)
wifi.radio.connect(ssid, password)
time.sleep(10)
print("Connected to", ssid)
# 获得天气情况


def get_weather():
    requests = adafruit_requests.Session(
        socketpool.SocketPool(wifi.radio), ssl.create_default_context())
    appid = os.getenv("appid")
    appsecret = os.getenv("appsecret")
    res = requests.get(
        'https://v0.yiketianqi.com/free/day?appid=%s&appsecret=%s&unescape=1&city=南阳' % (appid, appsecret)).json()
    return res['wea'], res['tem_night'], res['tem_day'], res['tem']


# 使用rtc
rtc.RTC().datetime = adafruit_ntp.NTP(socketpool.SocketPool(
    wifi.radio), tz_offset=8, server='ntp.aliyun.com').datetime
# 引入图片
image, palette = adafruit_imageload.load(
    '/images/ysqd.bmp', bitmap=displayio.Bitmap, palette=displayio.Palette)
tile_grid = displayio.TileGrid(image, pixel_shader=palette)
group = displayio.Group()
group.append(tile_grid)
# 设置板子的亮度和是否翻转
board.DISPLAY.brightness = 0.5  #
board.DISPLAY.rotation = 0  #
word_font = bitmap_font.load_font("wenquanyi_13px.pcf")

# 日期的设置
date = label.Label(word_font, text="test", color=0x000000)
date.x = 10  #
date.y = 20
date.scale = 2  #
group.append(date)

# 周几的设置
week = label.Label(word_font, text="周五", color=0x000000)
week.x = 120
week.y = 20
week.scale = 2
group.append(week)

# 时间的设置
timer = label.Label(word_font, text='10:17', color=0x779649)
timer.x = 15
timer.y = 65
timer.scale = 2  #
group.append(timer)

# 天气文字的设置
weather = label.Label(word_font, text='阴', color=0x3271ae)
weather.x = 10
weather.y = 110
weather.scale = 2  #
group.append(weather)

# 温度文字的设置
temperature = label.Label(word_font, text='10度-15度', color=0x3271ae)
temperature.x = 60
temperature.y = 110
temperature.scale = 2
group.append(temperature)

# 事实温度文字的设置
tem_now = label.Label(word_font, text='10度', color=0x3271ae)
tem_now.x = 150
tem_now.y = 65
tem_now.scale = 2
group.append(tem_now)
# 获得时间
time_now = time.localtime()
timer.text = '%02d:%02d' % (time_now.tm_hour, time_now.tm_min)
date.text = '%d月%d日' % (time_now.tm_mon, time_now.tm_mday)
week.text = get_wday(time_now.tm_wday)
wea, tem_night, tem_day, tem = get_weather()
weather.text = wea
temperature.text = '%s℃-%s℃' % (tem_night, tem_day)
tem_now.text = '%s度' % (tem)
board.DISPLAY.show(group)


while True:
    time_now = time.localtime()
    timer.text = '%02d:%02d' % (time_now.tm_hour, time_now.tm_min)
    if time_now.tm_min == 0:
        wea, tem_night, tem_day, tem = get_weather()
        weather.text = wea
        temperature.text = '%d℃-%d℃' % (tem_night, tem_day)
        tem_now.text = '%s度' % (tem)
    if time_now.tm_hour == 0:
        date.text = '%d月%d日' % (time_now.tm_mon, time_now.tm_mday)
        week.text = get_wday(time_now.tm_wday)

    pass

 

点赞 关注
 
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表