[作品提交] 【得捷电子Follow me第2期】任务4-1 日历&时钟

秦天qintian0303   2023-8-20 19:14 楼主

        本任务是通过通过互联网更新的万年历时钟,并显示当地的天气信息。

        其实这个任务和第二个任务是很像的,通过任务二的方式连接网络,再通过访问对应的url,获取对应的API数据,进行解析。将数据通过任务一的方式进行中文显示。

代码如下:

import time
import board
# import displayio
import ssl
import wifi
import socketpool
import adafruit_requests
from adafruit_display_text import label, wrap_text_to_lines
from adafruit_bitmap_font import bitmap_font

# 加载字库
font = bitmap_font.load_font("lib\wenquanyi_10pt.pcf")
color = 0x0555F5

# 初始化显示屏参数
display = board.DISPLAY
display.brightness = 0.25   # 更改亮度
display.rotation = 0    # 更改方向,0为横屏

# URLs to fetch from
JSON_TIME_URL = "http://quan.suning.com/getSysTime.do"
JSON_weather_URL = "http://t.weather.sojson.com/api/weather/city/{}"

# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise
print("ESP32-S2 Time&weather Test\n")

pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())

# 刷新显示
def refresh_screen(print_text):
    text_area = label.Label(font, text=print_text, scale=1)
    text_area.x = 2
    text_area.y = 10
    text_area.color = color
    display.show(text_area)

# 获取数据
def get_data():
    try:
        response = requests.get(JSON_TIME_URL)
    except ConnectionError as e:
        print("Connection Error:", e)
        print("Retrying in 60 seconds")
    time_data = response.json()['sysTime2'][:-3]
#     print(time_data)

    try:
        city_code = "101091101"     # 秦皇岛市的城市代码
        url = JSON_weather_URL.format(city_code)
        response = requests.get(url)
    except ConnectionError as e:
        print("Connection Error:", e)
        print("Retrying in 60 seconds")
    weather_data = response.json()
    cityInfo = weather_data['cityInfo']
    city_weather = weather_data['data']
    forecast = city_weather['forecast']
    dis_str = ""+cityInfo['parent']+' '+cityInfo['city']
    dis_str += "  "+time_data
    dis_str += "\n"+weather_data['time'][:11] + forecast[0]['week']
    dis_str += ' 空气质量:' + city_weather['quality']
    dis_str += "\n当前温度:"+city_weather['wendu']+"℃"
    dis_str += "  湿度:" + city_weather['shidu']
    dis_str += '\npm2.5:' + str(city_weather['pm25'])
    dis_str += '  pm10:' + str(city_weather['pm10'])
    dis_str += "\n明日最"+forecast[1]['high']+' 最'+forecast[1]['low']
    dis_str += "  " + forecast[1]['type']
    dis_str += "\n注意!!" + forecast[1]['notice']
#     print(dis_str)
    return dis_str

while not wifi.radio.ipv4_address:
    try:
        wifi.radio.connect(secrets["ssid"], secrets["password"])
    except ConnectionError as Error1:
        print("Connection Error:", Error1)
        print("Retrying in 3 seconds")
    time.sleep(3)
print("IP:", wifi.radio.ipv4_address)
print("Connected!\n")

while True:
    info = get_data()
    refresh_screen(info)
    time.sleep(60)

        本测试用例中用到的天气查询是墨迹天气,有很多不同的查询方式,大家可以试着换一换,效果如下:

1.jpg

 

在爱好的道路上不断前进,在生活的迷雾中播撒光引

回复评论

暂无评论,赶紧抢沙发吧
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复