[经验分享] 【得捷电子Follow me第2期】5,显示天气

ddllxxrr   2023-10-2 07:02 楼主

显示天气,首先是连接WIFI然后从天气网站获取JOSON码,再然后是解析JOSON码,再以后是显示。

需要注意的是,显示时那个字体,应用:

font = bitmap_font.load_font("wenquanyi_10pt.pcf")

而我用

font = font.load_font("wenquanyi_10pt.pcf")

显示:NameEorror:font not defined

 

程序如下:


## 连接wifi --------------------------------------------------------------------------------------

import os
import wifi

for network in wifi.radio.start_scanning_networks():
    print("\t%s\t\tRSSI: %d\tChannel: %d" % (str(network.ssid, "utf-8"),
                                             network.rssi, network.channel))
wifi.radio.stop_scanning_networks()

print(f"Connecting to {os.getenv('WIFI_SSID')}")

wifi.radio.connect('TPLINK555', '12345678')
print(f"Connected to {os.getenv('WIFI_SSID')}")
print(f"My IP address: {wifi.radio.ipv4_address}")


## END连接wifi --------------------------------------------------------------------------------------




## 访问网络 --------------------------------------------------------------------------------------
import ssl
import socketpool
import adafruit_requests
# 请求获取JSON
JSON_TIME_URL = "http://t.weather.sojson.com/api/weather/city/101070601"


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


## END访问网络 --------------------------------------------------------------------------------------




## 读取解析Json ---------------------------------------------------------------------------------------
print(f"Fetching and parsing json from {JSON_TIME_URL}")
response = requests.get(JSON_TIME_URL)
print("-" * 40)
## print(f"Time: {response.json()['sysTime2']}")
print("-" * 40)
print(response.json())


weather = response.json()
cityInfo  = weather['cityInfo']
city_weather = weather['data']
forecast = city_weather['forecast']

dis_str = ""+cityInfo['parent']+' '+cityInfo['city'] +' '+weather['time'][:11] + forecast[0]['week']
dis_str += '\n 空气质量:'+city_weather['quality'] +" " +forecast[0]['type']
dis_str += "\n 最"+forecast[0]['high']+' 最'+forecast[0]['low']
dis_str += "\n 湿度: "+city_weather['shidu']+' pm2.5:'+str(city_weather['pm25']) +' pm10:'+str(city_weather['pm10'])
dis_str += "\n 注意!!  "+forecast[0]['notice']

## END读取解析Json ---------------------------------------------------------------------------------------




## 显示天气信息	---------------------------------------------------
import board
import displayio
from adafruit_display_text import label, wrap_text_to_lines
from adafruit_bitmap_font import bitmap_font

display = board.DISPLAY
board.DISPLAY.brightness = 0.9
board.DISPLAY.rotation = 0

font = bitmap_font.load_font("wenquanyi_10pt.pcf")

## 字体颜色
color = 0x9499CA



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 = 0.8
text_area.scale = 1


text_group.append(text_area)
display.show(text_group)
## END显示天气信息	---------------------------------------------------
while True:
    pass

运行效果:

微信图片_20231002064853.jpg

 

http://shop34182318.taobao.com/ https://shop436095304.taobao.com/?spm=a230r.7195193.1997079397.37.69fe60dfT705yr

回复评论 (1)

看来显示时那个字体可能更是重点了,,,

点赞  2023-10-4 10:13
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复