【得捷电子Follow me第2期】5,显示天气
[复制链接]
显示天气,首先是连接WIFI然后从天气网站获取JOSON码,再然后是解析JOSON码,再以后是显示。
需要注意的是,显示时那个字体,应用:
- font = bitmap_font.load_font("wenquanyi_10pt.pcf")
而我用
- font = font.load_font("wenquanyi_10pt.pcf")
显示:NameEorror:font not defined
程序如下:
-
-
-
- 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}")
-
-
-
-
-
-
-
-
- import ssl
- import socketpool
- import adafruit_requests
-
- 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())
-
-
-
-
-
-
-
-
- print(f"Fetching and parsing json from {JSON_TIME_URL}")
- response = requests.get(JSON_TIME_URL)
- print("-" * 40)
-
- 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']
-
-
-
-
-
-
-
- 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)
-
- while True:
- pass
-
运行效果:
|