【得捷电子Follow me第2期】任务4-1:日历&时钟&天气
[复制链接]
访问天气网站可以提供天气、日期、空气质量等的信息可以实现日历、时钟、天气的功能。
实现步骤:
1、查询到本地的城市代码,我们可以搜索“免费的天气API相关编码(中国城市代码检索)”,
查询到我们桂林市的代码为101300501:
- 编写api请求字符串为:JSON_TIME_URL = "http://t.weather.sojson.com/api/weather/city/101300501"
- 在前面联网的任务中添加网络请救模块:
import ssl
import socketpool
import adafruit_requests
- 将接收到的字符串解析后,提取日期、时间、天气等信息
weather = response.json()
cityInfo = weather['cityInfo']
city_weather = weather['data']
forecast = city_weather['forecast']
dis_str = ""+cityInfo['parent']+' '+cityInfo['city']
dis_str += '\n' + weather['time'] + 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']
最后展示在LCD屏上,完成天气、日历的信息显示。
|