问 天问文档中心
⌕
文档

心知天气

更新于 2025-01-22 · 文档路径 esp32c3/02-天问ESP32C3-Pro/09-扩展类库/07-心知天气

一、心知天气-发起天气实况请求

def get_seni_weather(_url, _location):
    _url = _url + "&location=" + _location.replace(" ", "%20")
    response = urequests.get(_url)
    json = response.json()
    response.close()
    return json

w1 = (get_seni_weather("https://api.seniverse.com/v3/weather/now.json?key=your_private_key","ip"))
  • 参数:
    • your_private_key:自己注册的API密匙
    • language:
      • zh-Hans:简体中文(默认)
      • zh-Hant:正體中文
    • Unit:
      • c:温度单位为摄氏度(默认)
      • f:温度单位为华氏度

注:获取指定城市的天气实况。付费用户可获取全部数据,免费用户只返回天气现象文字、代码和气温 3 项数据。

更加详细内容可以参考心知天气API使用手册-天气实况。

使用示例

from board import *
import network
import urequests
import json

my_wifi = wifi()

def get_seni_weather(_url, _location):
    _url = _url + "&location=" + _location.replace(" ", "%20")
    response = urequests.get(_url)
    json = response.json()
    response.close()
    return json

my_wifi.connectWiFi('haoda7', '0123456789')
w1 = (get_seni_weather("https://api.seniverse.com/v3/weather/now.json?key=S68-rNDesn1uy3ODc","hangzhou"))
print(w1)


获取的JSON数据(此密匙不是付费用户,请替换成自己的密匙):

{
    "results": [
        {
            "location": {
                "id": "WTMKQ069CCJ7",
                "name": "杭州",
                "country": "CN",
                "path": "杭州,杭州,浙江,中国",
                "timezone": "Asia/Shanghai",
                "timezone_offset": "+08:00"
            },
            "now": {
                "text": "霾",
                "code": "31",
                "temperature": "12"
            },
            "last_update": "2025-01-22T11:00:00+08:00"
        }
    ]
}

二、心知天气-获取天气实况信息

#以城市名称为例
w1["results"][0]["location"]["name"]

注意:使用图形块需要注意变量名称以及请求图形块所对应的获取图形块是否匹配。

三、心知天气-发起三天天气预报请求

def get_seni_weather(_url, _location):
    _url = _url + "&location=" + _location.replace(" ", "%20")
    response = urequests.get(_url)
    json = response.json()
    response.close()
    return json

w1 = (get_seni_weather("https://api.seniverse.com/v3/weather/daily.json?key=S68-rNDesn1uy3ODc","hangzhou"))
  • 参数:
    • your_private_key:自己注册的API密匙
    • language:
      • zh-Hans:简体中文(默认)
      • zh-Hant:正體中文
    • Unit:
      • c:温度单位为摄氏度(默认)
      • f:温度单位为华氏度

更加详细内容可以参考心知天气API使用手册-未来15天逐日天气预报。

使用示例

from board import *
import network
import urequests
import json

my_wifi = wifi()

def get_seni_weather(_url, _location):
    _url = _url + "&location=" + _location.replace(" ", "%20")
    response = urequests.get(_url)
    json = response.json()
    response.close()
    return json

my_wifi.connectWiFi('haoda7', '0123456789')
w1 = (get_seni_weather("https://api.seniverse.com/v3/weather/daily.json?key=S68-rNDesn1uy3ODc","hangzhou"))
print(w1)


获取的JSON数据(此密匙不是付费用户,请替换成自己的密匙):

{
    "results": [
        {
            "location": {
                "id": "WTMKQ069CCJ7",
                "name": "杭州",
                "country": "CN",
                "path": "杭州,杭州,浙江,中国",
                "timezone": "Asia/Shanghai",
                "timezone_offset": "+08:00"
            },
            "daily": [
                {
                    "date": "2025-01-22",
                    "text_day": "多云",
                    "code_day": "4",
                    "text_night": "多云",
                    "code_night": "4",
                    "high": "17",
                    "low": "5",
                    "rainfall": "0.00",
                    "precip": "0.00",
                    "wind_direction": "东",
                    "wind_direction_degree": "90",
                    "wind_speed": "23.4",
                    "wind_scale": "4",
                    "humidity": "74"
                },
                {
                    "date": "2025-01-23",
                    "text_day": "多云",
                    "code_day": "4",
                    "text_night": "多云",
                    "code_night": "4",
                    "high": "18",
                    "low": "8",
                    "rainfall": "0.00",
                    "precip": "0.00",
                    "wind_direction": "东",
                    "wind_direction_degree": "90",
                    "wind_speed": "32.8",
                    "wind_scale": "5",
                    "humidity": "72"
                },
                {
                    "date": "2025-01-24",
                    "text_day": "阴",
                    "code_day": "9",
                    "text_night": "小雨",
                    "code_night": "13",
                    "high": "14",
                    "low": "8",
                    "rainfall": "5.40",
                    "precip": "0.90",
                    "wind_direction": "东",
                    "wind_direction_degree": "90",
                    "wind_speed": "23.4",
                    "wind_scale": "4",
                    "humidity": "94"
                }
            ],
            "last_update": "2025-01-22T08:00:00+08:00"
        }
    ]
}

四、心知天气-获取三天天气预报信息

#以获取明天的风速为例为例
w1["results"][0]["daily"][1]["wind_speed"]


五、心知天气-发起6项生活指标请求

def get_seni_weather(_url, _location):
    _url = _url + "&location=" + _location.replace(" ", "%20")
    response = urequests.get(_url)
    json = response.json()
    response.close()
    return json

w1 = (get_seni_weather("https://api.seniverse.com/v3/life/suggestion.json?key=S68-rNDesn1uy3ODc","hangzhou"))
  • 参数:
    • your_private_key:自己注册的API密匙
    • language:
      • zh-Hans:简体中文(默认)
      • zh-Hant:正體中文
    • Unit:
      • c:温度单位为摄氏度(默认)
      • f:温度单位为华氏度

更加详细内容可以参考心知天气API使用手册-生活指标

使用示例

{
    "results": [
        {
            "location": {
                "id": "WTMKQ069CCJ7",
                "name": "杭州",
                "country": "CN",
                "path": "杭州,杭州,浙江,中国",
                "timezone": "Asia/Shanghai",
                "timezone_offset": "+08:00"
            },
            "suggestion": {
                "car_washing": {
                    "brief": "较适宜",
                    "details": ""
                },
                "dressing": {
                    "brief": "较冷",
                    "details": ""
                },
                "flu": {
                    "brief": "少发",
                    "details": ""
                },
                "sport": {
                    "brief": "较适宜",
                    "details": ""
                },
                "travel": {
                    "brief": "适宜",
                    "details": ""
                },
                "uv": {
                    "brief": "弱",
                    "details": ""
                }
            },
            "last_update": "2024-11-21T18:17:13+08:00"
        }
    ]
}

六、心知天气-获取6项生活指标信息

#以获取紫外线指数为例
w1["results"][0]["suggestion"]["uv"]["brief"]