這篇文章會詳細描述怎樣立即得到指定城市的天氣狀況(比如首爾),由OpenWeatherMap提供。用JSON(由OpenWeatherMap提供),XML和一個以太網模塊,使WIZnet-WizFi250運行起來。
首先,在OpenWeatherMap上檢查API內容。
由城市的名稱調用。API用一列結果應答,來匹配一個搜索詞。在JSON中:
api.openweathermap.org/data/2.5/weather?q=London,uk 在XML:
api.openweathermap.org/data/2.5/weather?q=London&mode=xml http://openweathermap.org/current
然后,跟隨上面的指導,在電腦上將JSON和XML格式中的首爾天氣數據提取出來。下面有一個網絡數據包的截屏可以作為參考。
紅色高亮部分的數據是由PC發送的。我們準備消除不必要部分,并使用請求數據輸入以下數據。
在這步, (Carriage return, 0x0d), (Line Feed, 0x0a)必須準確輸入,并且傳送數據(72 Byte, 81 Byte)也要特別注意。
* JSON
GET /data/2.5/weather?q=Seoul HTTP/1.1
Host: api.openweathermap.org
(72 Byte)
* XML
GET /data/2.5/weather?q=Seoul&mode=xml HTTP/1.1
Host: api.openweathermap.org
(81 Byte)
既然初始化設置已完成,我們將運行WizFi250.首先,在AP上建立聯接。
AT+WSET=0,Team Wiki
[OK] AT+WSEC=0,,12345678
[OK] AT+WNET=1
[OK] AT+WJOIN
Joining : Team Wiki
Successfully joined : Team Wiki
[Link-Up Event] IP Addr : 192.168.101.33
Gateway : 192.168.101.1
[OK]
提取OpenWeatherMap API 服務器的IP地址。
AT+FDNS=api.openweathermap.org,3000
128.199.164.95
[OK]
與OpenWeatherMap API 服務器連接。
AT+SCON=O,TCN,128.199.164.95,80,,0
[OK] [CONNECT 0]
發送JSON請求數據到 OpenWeatherMap API服務器。
AT+SSEND=0,,,72
[0,,,72] (???? ?? ??? 72 Byte? ???? ??.)
[OK]
然后,OpenWeatherMap API 服務器將用一個JSON回復(如下)來應答。
{0,128.199.164.95,80,857}HTTP/1.1 200 OK
Server: nginx
Date: Wed, 06 Aug 2014 00:06:49 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Source: redis
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST
221
{“coord”:{“lon”:126.98,”lat”:37.57},”sys”:{“type”:3,”id”:8519,”message”:0.033,”country”:”KR”,”sunrise”:1407184771,”sunset”:1407234998},”weather”:[{"id":721,"main":"Haze","description":"haze","icon":"50n"},{"id":500,"main":"Rain","description":"light
rain","icon":"10n"},{"id":701,"main":"Mist","description":"mist","icon":"50n"}],”base”:”cmc stations”,”main”:{“temp”:297.26,”pressure”:1005,”humidity”:83,”temp_min”:295.15,”temp_max”:298.15},”wind”:{“speed”:1,”deg”:140},”clouds”:{“all”:40},”dt”:1407277800,”id”:1835848,”name”:”Seoul”,”cod”:200}
0
如果一旦完整接收,連接仍在進行,用OpenWeatherMap API服務器來終止連接。
AT+SMGMT=ALL
[DISCONNECT 0] [OK]
現在,分析如上接收的JSON數據,使用期望的天氣數據(首爾的溫度,濕度,風速等等)Enjoy!
下面是當你在XML格式下接收數據情況下。然而,WizFi250操作的基本規則是與JSON想死的,所以應該很簡單。
AT+SCON=O,TCN,128.199.164.95,80,,0
[OK] [CONNECT 0] AT+SSEND=0,,,81
[0,,,81] (???? ?? ??? 81 Byte? ????.)
[OK] {0,128.199.164.95,80,969}HTTP/1.1 200 OK
Server: nginx
Date: Tue, 05 Aug 2014 23:59:54 GMT
Content-Type: text/xml; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Source: back
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST
29a
<current>
<city id=”1835848″ name=”Seoul”>
<coord lon=”126.98″ lat=”37.57″>
<country>KR</country>
<sun set=”2014-08-06T10:35:34″ rise=”2014-08-05T20:40:21″>
</sun></coord></city>
<temperature value=”297.38″ unit=”kelvin” min=”296.15″
max=”298.15″>
<humidity value=”83″ unit=”%”>
<pressure value=”1006″ unit=”hPa”>
<wind>
<speed value=”1″ name=”Calm”>
<direction value=”170″ name=”South” code=”S”>
</direction></speed></wind>
<clouds value=”75″ name=”broken clouds”>
<visibility>
<precipitation mode=”no”>
<weather value=”haze” number=”721″ icon=”50n”>
<lastupdate value=”2014-08-05T23:30:00″>
</lastupdate></weather></precipitation></visibility></clouds></pressure></humidity></temperature></current>
0
AT+SMGMT=ALL
[DISCONNECT 0] [OK]
歡迎登陸WIZnet官方網站:http://www.iwiznet.cn
WIZnet官方博客:http://weibo.com/wiznet2012