DIY - 2019/Grow UP(Smart LED)

공공 API를 활용한 날씨, 온도 그리고 대기질 정보 가져오기 - 하

알렉스윤 2019. 5. 9. 17:09

다음에는 동네 온도, 습도, 날씨, 날씨 예보등의 정보를 가져와보도록 하겠다. 이말을 내뱉은지 꽤 오랜시간이 지났다.)

참고 문서 : OpenAPI 사용자 활용가이드(기상청_신규 동네예보정보조회서비스)_v2.4.hwp

OpenAPI 사용자 활용가이드(기상청_신규 동네예보정보조회서비스)_v2.4.hwp
0.34MB

 

오퍼레이션 항목 취득할 데이터는 초단기실황조회와 초단기예보조회이다.

경우, 초단기예보와 동네예보 Data 토대로 구현된 상태이다. 이유인 , 이해도가 떨어졌기 때문에 엉뚱한 data 이용해서 구현했다. ㅎㅎ

 

[초단기실황조회]

http://newsky2.kma.go.kr/service/SecndSrtpdFrcstInfoService2/ForecastGrib?ServiceKey=서비스키&base_date=20190402&base_time=1300&nx=63&ny=128&pageNo=1&numOfRows=10totalcount=20

 

numberOfRows totalcount 삭제해도 지장 없음. 확실하지는 않다. 

http://newsky2.kma.go.kr/service/SecndSrtpdFrcstInfoService2/ForecastGrib?ServiceKey=서비스키&base_date=20190402&base_time=1300&nx=63&ny=128&pageNo=1

 

참고문서 표를 확인하면 아래와 같은 값을 확인 있다.

내가 중요하게 생각하는 부분은 기온, 강수량, 습도와 강수형태 정도이다.

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<response>
<header>
<resultCode>0000</resultCode>
<resultMsg>OK</resultMsg>
</header>
<body>
<items>
<item>
<baseDate>20190402</baseDate>
<baseTime>1400</baseTime>
<category>PTY</category>
<nx>63</nx>
<ny>128</ny>
<obsrValue>0</obsrValue>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>1400</baseTime>
<category>REH</category>
<nx>63</nx>
<ny>128</ny>
<obsrValue>17</obsrValue>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>1400</baseTime>
<category>RN1</category>
<nx>63</nx>
<ny>128</ny>
<obsrValue>0</obsrValue>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>1400</baseTime>
<category>T1H</category>
<nx>63</nx>
<ny>128</ny>
<obsrValue>13.5</obsrValue>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>1400</baseTime>
<category>UUU</category>
<nx>63</nx>
<ny>128</ny>
<obsrValue>-0.2</obsrValue>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>1400</baseTime>
<category>VEC</category>
<nx>63</nx>
<ny>128</ny>
<obsrValue>6</obsrValue>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>1400</baseTime>
<category>VVV</category>
<nx>63</nx>
<ny>128</ny>
<obsrValue>-2.6</obsrValue>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>1400</baseTime>
<category>WSD</category>
<nx>63</nx>
<ny>128</ny>
<obsrValue>2.7</obsrValue>
</item>
</items>
<numOfRows>10</numOfRows>
<pageNo>1</pageNo>
<totalCount>8</totalCount>
</body>
</response>

여기서 중요하게 확인해야 사항은 totalCount 이다. 만약 값이 10 넘어가게 된다면 pageNo 변경해서 값을 추가로 가져와야 한다.

<numOfRows>10</numOfRows>
<pageNo>1</pageNo>
<totalCount>8</totalCount>

현재 totalCount 관련 구현된 사항은 전혀없다. ㅎㅎ

 

 

초단기예보조회

http://newsky2.kma.go.kr/service/SecndSrtpdFrcstInfoService2/ForecastTimeData?ServiceKey=서비스키&base_date=20190402&base_time=1300&nx=63&ny=128&pageNo=1

쿼리를 날리는 시점(baseTime) 결과 값이 가르키는 시간(fcsTime) 구분되어 표기된다.

, fcsTime 시점의 초단기 예보라고 보면된다.

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<response>
<header>
<resultCode>0000</resultCode>
<resultMsg>OK</resultMsg>
</header>
<body>
<items>
<item>
<baseDate>20190402</baseDate>
<baseTime>1330</baseTime>
<category>LGT</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1400</fcstTime>
<fcstValue>0</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>1330</baseTime>
<category>LGT</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1500</fcstTime>
<fcstValue>0</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>1330</baseTime>
<category>PTY</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1400</fcstTime>
<fcstValue>0</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>1330</baseTime>
<category>PTY</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1500</fcstTime>
<fcstValue>0</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>1330</baseTime>
<category>RN1</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1400</fcstTime>
<fcstValue>0</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>1330</baseTime>
<category>RN1</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1500</fcstTime>
<fcstValue>0</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>1330</baseTime>
<category>SKY</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1400</fcstTime>
<fcstValue>1</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>1330</baseTime>
<category>SKY</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1500</fcstTime>
<fcstValue>1</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>1330</baseTime>
<category>T1H</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1400</fcstTime>
<fcstValue>12</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>1330</baseTime>
<category>T1H</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1500</fcstTime>
<fcstValue>12</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
</items>
<numOfRows>10</numOfRows>
<pageNo>1</pageNo>
<totalCount>20</totalCount>
</body>
</response>

 

동네예보조회

http://newsky2.kma.go.kr/service/SecndSrtpdFrcstInfoService2/ForecastSpaceData?ServiceKey=서비스키&base_date=20190402&base_time=1300&nx=63&ny=128&pageNo=1

꽤나 관심가질 만한 정보들이 많이 나온다.

결과값도 200 이상 rerurn되기 때문에 page 20이상 넘어 수도 있다.

아침 최저 기온과 최고 기온의 경우 조회하는 시간에 따라서 보이기도 하고 보이지 않기도 한다.

몇번 시험을 결과 조회 시간이 새벽 2시일 경우에 모두 나옴을 확인했다.

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<response>
<header>
<resultCode>0000</resultCode>
<resultMsg>OK</resultMsg>
</header>
<body>
<items>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>POP</category>
<fcstDate>20190402</fcstDate>
<fcstTime>0600</fcstTime>
<fcstValue>0</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>PTY</category>
<fcstDate>20190402</fcstDate>
<fcstTime>0600</fcstTime>
<fcstValue>0</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>R06</category>
<fcstDate>20190402</fcstDate>
<fcstTime>0600</fcstTime>
<fcstValue>0</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>REH</category>
<fcstDate>20190402</fcstDate>
<fcstTime>0600</fcstTime>
<fcstValue>75</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>S06</category>
<fcstDate>20190402</fcstDate>
<fcstTime>0600</fcstTime>
<fcstValue>0</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>SKY</category>
<fcstDate>20190402</fcstDate>
<fcstTime>0600</fcstTime>
<fcstValue>1</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>T3H</category>
<fcstDate>20190402</fcstDate>
<fcstTime>0600</fcstTime>
<fcstValue>-1</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>TMN</category>
<fcstDate>20190402</fcstDate>
<fcstTime>0600</fcstTime>
<fcstValue>-1.0</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>UUU</category>
<fcstDate>20190402</fcstDate>
<fcstTime>0600</fcstTime>
<fcstValue>0.2</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>VEC</category>
<fcstDate>20190402</fcstDate>
<fcstTime>0600</fcstTime>
<fcstValue>326</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
</items>
<numOfRows>10</numOfRows>
<pageNo>1</pageNo>
<totalCount>155</totalCount>
</body>
</response>

 

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<response>
<header>
<resultCode>0000</resultCode>
<resultMsg>OK</resultMsg>
</header>
<body>
<items>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>VVV</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1200</fcstTime>
<fcstValue>-2.6</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>WSD</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1200</fcstTime>
<fcstValue>4</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>POP</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1500</fcstTime>
<fcstValue>20</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>PTY</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1500</fcstTime>
<fcstValue>0</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>REH</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1500</fcstTime>
<fcstValue>25</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>SKY</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1500</fcstTime>
<fcstValue>3</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>T3H</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1500</fcstTime>
<fcstValue>12</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>TMX</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1500</fcstTime>
<fcstValue>12.0</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>UUU</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1500</fcstTime>
<fcstValue>3.8</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
<item>
<baseDate>20190402</baseDate>
<baseTime>0200</baseTime>
<category>VEC</category>
<fcstDate>20190402</fcstDate>
<fcstTime>1500</fcstTime>
<fcstValue>304</fcstValue>
<nx>63</nx>
<ny>128</ny>
</item>
</items>
<numOfRows>10</numOfRows>
<pageNo>4</pageNo>
<totalCount>155</totalCount>
</body>
</response>

결과에서 page1 최저기온(TMN) 1, page4 최고기온(TMX) 12도를 알수 있다.

결과 값을 DB화해서 MQTT 서버에 저장 , 해당 결과만 읽어오면 좋을 하다.

지금도 허덕이는데… 이거 구현 있을까 싶다.

이상으로 공공 Data API 관련 설명을 마무리 하려고한다.

많이 부족해서 따라하려는 사람들은 허덕일 밖에 없다.

알아두어야 사실은 Servicekey, 자신이 측정하고 싶은 위치와 가장 가까운 기상관측소의 위치를 먼저 알아두는 것이 필요하다.