|
使用ESP8266+Arduino开发板制作一个微型气象站,是一件非常酷的事情。在本论坛里面也已经有很多帖子在讨论如何制作。在这些帖子中,无一例外,都是使用Weather Underground提供的API来获取天气,该网站提供的功能非常强大,几乎可以包含全球所有城市的天气情况。如果您还不会制作气象站,那么可以参考以下几个帖子: ● Arduino Tian开发板:一个功能强大的天气预报中心:https://www.yiboard.com/thread-716-1-1.html ● 使用ESP8266制作一个微型气象站:https://www.yiboard.com/thread-701-1-1.html ● 非常酷的ESP8266 WiFi OLED微型气象站套件:https://www.yiboard.com/thread-756-1-1.html
最近有一些网友给我发消息,说他们无法使用Weather Underground的API来获取国内城市的天气状况,我在这里整理以下解决方法,供大家参考。
首先,在Weather Underground网址注册成功后,就会获得一个免费的API密钥。通过在您的浏览器中按照下列格式输入URL,就会形成一个到Wunderground的API: - http://api.wunderground.com/api/YOUR_API_KEY/conditions/q/THE_DESIRED_STATE/THE_DESIRED_CITY.json
复制代码
例如,要获得加利福尼亚旧金山的天气: ● http://api.wunderground.com/api/ ... /San_Francisco.json 或者是悉尼的天气 ● http://api.wunderground.com/api/Your_Key/geolookup/conditions/forecast/q/Australia/Sydney.json
我们来看一下返回的响应内容。如果正确的话,就会返回一个带有current_observation标识的字段: - {
- "response": {
- "version":"0.1",
- "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
- "features": {
- "conditions": 1
- }
- }
- , "current_observation": {
- "image": {
- "url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
- "title":"Weather Underground",
- "link":"http://www.wunderground.com"
- },
- "display_location": {
- "full":"Sydney, Australia",
- "city":"Sydney",
- "state":"NSW",
- "state_name":"Australia",
- "country":"AU",
- "country_iso3166":"AU",
- "zip":"00000",
- "magic":"16",
- "wmo":"94768",
- "latitude":"-33.86999893",
- "longitude":"151.21000671",
- "elevation":"3.0"
- },
- "observation_location": {
- "full":"Sydney, NEW SOUTH WALES",
- "city":"Sydney",
- "state":"NEW SOUTH WALES",
- "country":"AU",
- "country_iso3166":"AU",
- "latitude":"-33.88",
- "longitude":"151.22",
- "elevation":"79 ft"
- },
- "estimated": {
- },
- "station_id":"INEWSOUT349",
- "observation_time":"Last Updated on May 5, 11:38 AM AEST",
- "observation_time_rfc822":"Sat, 05 May 2018 11:38:24 +1000",
- "observation_epoch":"1525484304",
- "local_time_rfc822":"Sat, 05 May 2018 11:38:50 +1000",
- "local_epoch":"1525484330",
- "local_tz_short":"AEST",
- "local_tz_long":"Australia/Sydney",
- "local_tz_offset":"+1000",
- "weather":"Clear",
- "temperature_string":"68.4 F (20.2 C)",
- "temp_f":68.4,
- "temp_c":20.2,
- "relative_humidity":"39%",
- "wind_string":"From the East at 1.8 MPH Gusting to 2.5 MPH",
- "wind_dir":"East",
- "wind_degrees":98,
- "wind_mph":1.8,
- "wind_gust_mph":"2.5",
- "wind_kph":2.9,
- "wind_gust_kph":"4.0",
- "pressure_mb":"1027",
- "pressure_in":"30.33",
- "pressure_trend":"0",
- "dewpoint_string":"42 F (6 C)",
- "dewpoint_f":42,
- "dewpoint_c":6,
- "heat_index_string":"NA",
- "heat_index_f":"NA",
- "heat_index_c":"NA",
- "windchill_string":"NA",
- "windchill_f":"NA",
- "windchill_c":"NA",
- "feelslike_string":"68.4 F (20.2 C)",
- "feelslike_f":"68.4",
- "feelslike_c":"20.2",
- "visibility_mi":"N/A",
- "visibility_km":"N/A",
- "solarradiation":"470",
- "UV":"-1","precip_1hr_string":"0.00 in ( 0 mm)",
- "precip_1hr_in":"0.00",
- "precip_1hr_metric":" 0",
- "precip_today_string":"0.00 in (0 mm)",
- "precip_today_in":"0.00",
- "precip_today_metric":"0",
- "icon":"clear",
- "icon_url":"http://icons.wxug.com/i/c/k/clear.gif",
- "forecast_url":"http://www.wunderground.com/global/stations/94768.html",
- "history_url":"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=INEWSOUT349",
- "ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=-33.876339,151.216187",
- "nowcast":""
- }
- }
复制代码
这对于大多数城市来说是可以实现的。那么国内的城市怎么获取呢?以楼主所在的城市为例,将上述API的城市修改为中国石家庄。于是API网址变成如下: - http://api.wunderground.com/api/0def10027afaebb7/conditions/q/China/Shijiazhuang.json
复制代码让我们来看一下返回的内容:
- {
- "response": {
- "version":"0.1",
- "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
- "features": {
- "conditions": 1
- }
- , "results": [
- {
- "name": "Shijiazhuang",
- "city": "Shijiazhuang",
- "state": "13",
- "country": "CI",
- "country_iso3166":"CN",
- "country_name":"China",
- "zmw": "00000.137.53698",
- "l": "/q/zmw:00000.137.53698"
- }
- ,
- {
- "name": "Shijiazhuang",
- "city": "Shijiazhuang",
- "state": "14",
- "country": "CI",
- "country_iso3166":"CN",
- "country_name":"China",
- "zmw": "00000.481.WZBYC",
- "l": "/q/zmw:00000.481.WZBYC"
- }
- ,
- {
- "name": "Shijiazhuang",
- "city": "Shijiazhuang",
- "state": "32",
- "country": "CI",
- "country_iso3166":"CN",
- "country_name":"China",
- "zmw": "00000.414.58144",
- "l": "/q/zmw:00000.414.58144"
- }
- ,
- {
- "name": "Shijiazhuang",
- "city": "Shijiazhuang",
- "state": "33",
- "country": "CI",
- "country_iso3166":"CN",
- "country_name":"China",
- "zmw": "00000.311.58660",
- "l": "/q/zmw:00000.311.58660"
- }
- ,
- {
- "name": "Shijiazhuang",
- "city": "Shijiazhuang",
- "state": "36",
- "country": "CI",
- "country_iso3166":"CN",
- "country_name":"China",
- "zmw": "00000.450.58527",
- "l": "/q/zmw:00000.450.58527"
- }
- ,
- {
- "name": "Shijiazhuang",
- "city": "Shijiazhuang",
- "state": "41",
- "country": "CI",
- "country_iso3166":"CN",
- "country_name":"China",
- "zmw": "00000.420.57181",
- "l": "/q/zmw:00000.420.57181"
- }
- ,
- {
- "name": "Shijiazhuang",
- "city": "Shijiazhuang",
- "state": "42",
- "country": "CI",
- "country_iso3166":"CN",
- "country_name":"China",
- "zmw": "00000.251.58402",
- "l": "/q/zmw:00000.251.58402"
- }
- ,
- {
- "name": "Shijiazhuang",
- "city": "Shijiazhuang",
- "state": "62",
- "country": "CI",
- "country_iso3166":"CN",
- "country_name":"China",
- "zmw": "00000.335.52996",
- "l": "/q/zmw:00000.335.52996"
- }
- ,
- {
- "name": "Shijiazhuang",
- "city": "Shijiazhuang",
- "state": "64",
- "country": "CI",
- "country_iso3166":"CN",
- "country_name":"China",
- "zmw": "00000.405.53810",
- "l": "/q/zmw:00000.405.53810"
- }
- ]
- }
- }
复制代码从回复的内容中可以看出,并没有current_observation的字段,而是带有Result的字段。那么这表示什么意思呢。在这里,我们先引用以下官网中的描述: 大概意思是: - 不明确的结果
- 当API位置查询未产生精确的位置匹配时,在JSON响应中将出现一个result数组。
- http://api.wunderground.com/api/Your_Key/geolookup/forecast/q/Allentown.json
- 每个result对象都有一个l参数(链接的简写),可用于构建wunderground URL:
- http://api.wunderground.com/api/Your_Key/forecast/q/zmw:31003.1.99999.json
复制代码如果没有正确获得国内城市的天气状况,是因为位置没有精确匹配,比如在石家庄的响应中,有8个result对象,我们可以使用其中的一个,重新生成链接: - http://api.wunderground.com/api/0def10027afaebb7/conditions/q/zmw:00000.137.53698.json
复制代码这样就可以了。
也可以缩小城市的范围。比如石家庄正定市。于是上面的网址变成: - http://api.wunderground.com/api/0def10027afaebb7/conditions/q/China/Zhengding.json
复制代码
|