跳到主要内容

03-高德天气

前言

日常出行,掌握天气情况是十分有必要的。本节教程跟着我们的指引,来制作一个可以获取天气状况的手表

积木说明

序号积木说明
01image.png高德天气的使用API,需要注册使用
02image.png获取指定城市的天气实况
读取之后,更新得到的天气数据情况
03image.png读取指定城市的天气预报,可以指定天数读取之后,更新得到的天气数据情况
04image.png实况天气的各项数据,天气状况、温度、风向、风力等级、湿度
05image.png天气预报的各项情况,天气状况、温度、风向、风力等级,白天和晚上数据都可以查询

高德天气注册

🚀 高德开放平台地址:https://developer.amap.com/

  1. 按照下图步骤进行个人开发者账号注册

image.png
image.png

  1. 注册成功后登录,打开——Web服务API,申请Key

image.png
image.png

  1. 申请Key需要先创建应用

image.png
image.png
image.png
image.png

  1. 得到Key

image.png

程序案例

示例程序📋:实况天气

image.png

#/bin/python
from future import *

from time import sleep

from kwatch import *

x = 0

screen.sync = 0
sleep(3)
if not wifi.sta.isconnected():
wifi.connect(str("xxx"), "xxx")
sleep(1)
weather = Weather("xxx")
weather.getActual("深圳")
while True:
screen.fill((0, 0, 0))
screen.textCh(str("天气状况:")+str(weather.condition()),5,10,1,(0, 119, 255))
screen.textCh(str("温度:")+str(weather.temp()),5,35,1,(0, 119, 255))
screen.textCh(str("风向:")+str(weather.winddir()),5,60,1,(0, 119, 255))
screen.textCh(str("风力等级:")+str(weather.windpw()),5,85,1,(0, 119, 255))
screen.textCh(str("湿度:")+str(weather.humi()),5,110,1,(0, 119, 255))
screen.refresh()
sleep(1)


实物效果图

示例程序📋:天气预报

image.png

#/bin/python
from future import *

from time import sleep

from kwatch import *

x = 1

screen.sync = 0
sleep(3)
if not wifi.sta.isconnected():
wifi.connect(str("xxx"), "xxx")
sleep(1)
weather = Weather("xxx")
weather.getForecast("深圳",1)
x = 1
while True:
if sensor.btnValue('a'):
x = (x * -1)
screen.fill((0, 0, 0))
if x == 1:
screen.textCh(str("天气状况:")+str(weather.dayweather()),5,10,1,(0, 119, 255))
screen.textCh(str("温度:")+str(weather.daytemp()),5,35,1,(0, 119, 255))
screen.textCh(str("风向:")+str(weather.daywind()),5,60,1,(0, 119, 255))
screen.textCh(str("风力等级:")+str(weather.daypower()),5,85,1,(0, 119, 255))
else:
screen.textCh(str("天气状况:")+str(weather.nightweather()),5,10,1,(0, 119, 255))
screen.textCh(str("温度:")+str(weather.nighttemp()),5,35,1,(0, 119, 255))
screen.textCh(str("风向:")+str(weather.nightwind()),5,60,1,(0, 119, 255))
screen.textCh(str("风力等级:")+str(weather.nightpower()),5,85,1,(0, 119, 255))
screen.refresh()
sleep(1)

实物效果图