WIZnet2012

让你的WizFi250适应各种气候

0
阅读(1576)

这篇文章会详细描述怎样立即得到指定城市的天气状况(比如首尔),由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格式中的首尔天气数据提取出来。下面有一个网络数据包的截屏可以作为参考。

openweathermap

红色高亮部分的数据是由PC发送的。我们准备消除不必要部分,并使用请求数据输入以下数据。

在这步, \r(Carriage return, 0x0d), \n(Line Feed, 0x0a)必须准确输入,并且传送数据(72 Byte, 81 Byte)也要特别注意。

* JSON
GET /data/2.5/weather?q=Seoul HTTP/1.1\r\n
Host: api.openweathermap.org\r\n\r\n
(72 Byte)
* XML
GET /data/2.5/weather?q=Seoul&mode=xml HTTP/1.1\r\n
Host: api.openweathermap.org\r\n\r\n
(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