오픈API를 통한 조회 결과도 정상적으로 받아오게 되었으니 이제는 받아온 결과 중에서 필요한 정보를 뽑아내어 LCD에 표시해주기만 하면 됩니다.


제가 필요로 하는 정보는 집근처 미세먼지와 초미세먼지의 최신 정보뿐입니다.


집근처는 조회 시의 stationName에 집근처의 측정소 이름을 넣었으니 되었고, 최신 정보는 마찬가지로 조회 시에 pageNo와 numOfRows 값을 1로 넣어서 가장 최신 정보만 받아오도록 했습니다.


그러므로 받아온 정보 중에서 미세먼지와 초미세먼지 농도 정보만 찾으면 되겠네요.


오픈API 가이드 문서를 보니 미세먼지는 <pm10Value>, 초미세먼지는 <pm25Value> 태그를 사용하네요.


다음과 같이 예제를 만들어서 해당 태그의 정보를 찾아내도록 해 봅니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 
String pm10Value = "";
String pm25Value = "";
 
void setup() {
  String temp = "<response>";
  temp +=         "<header>";
  temp +=           "<resultCode>00</resultCode>";
  temp +=           "<resultMsg>NORMAL SERVICE.</resultMsg>";
  temp +=         "</header>";
  temp +=         "<body>";
  temp +=           "<items>";
  temp +=             "<item>";
  temp +=               "<dataTime>2019-03-27 17:00</dataTime>";
  temp +=               "<mangName>도로변대기</mangName>";
  temp +=               "<so2Value>0.007</so2Value>";
  temp +=               "<coValue>0.4</coValue>";
  temp +=               "<o3Value>0.043</o3Value>";
  temp +=               "<no2Value>0.024</no2Value>";
  temp +=               "<pm10Value>73</pm10Value>";
  temp +=               "<pm10Value24>55</pm10Value24>";
  temp +=               "<pm25Value>44</pm25Value>";
  temp +=               "<pm25Value24>31</pm25Value24>";
  temp +=               "<khaiValue>75</khaiValue>";
  temp +=               "<khaiGrade>2</khaiGrade>";
  temp +=               "<so2Grade>1</so2Grade>";
  temp +=               "<coGrade>1</coGrade>";
  temp +=               "<o3Grade>2</o3Grade>";
  temp +=               "<no2Grade>1</no2Grade>";
  temp +=               "<pm10Grade>2</pm10Grade>";
  temp +=               "<pm25Grade>2</pm25Grade>";
  temp +=               "<pm10Grade1h>2</pm10Grade1h>";
  temp +=               "<pm25Grade1h>2</pm25Grade1h>";
  temp +=             "</item>";
  temp +=           "</items>";
  temp +=           "<numOfRows>1</numOfRows>";
  temp +=           "<pageNo>1</pageNo>";
  temp +=           "<totalCount>1</totalCount>";
  temp +=         "</body>";
  temp +=       "</response>";
  
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(100);
  Serial.println("setup() START");
  
  int startIndex = 0;
  int endIndex = 0;
 
  startIndex = temp.indexOf("<pm10Value>"+ 11;
  endIndex = temp.indexOf("</pm10Value>");
  pm10Value = temp.substring(startIndex, endIndex);
 
  startIndex = temp.indexOf("<pm25Value>"+ 11;
  endIndex = temp.indexOf("</pm25Value>");
  pm25Value = temp.substring(startIndex, endIndex);
 
  Serial.print("미세먼지 : ");
  Serial.println(pm10Value);
  Serial.print("초미세먼지 : ");
  Serial.println(pm25Value);
}
 
void loop() {
  // put your main code here, to run repeatedly:
 
}
cs



네 문제없이 잘 찾아왔네요. 물론 저 방법보다 더 나은 방법, 더 좋은 방법도 있겠지만 제가 사용하기에는 일단 아무런 문제가 없어 보여서 저걸로 만족하고 다음으로 넘어갑니다.


실제로 오픈API를 통해 조회한 후, 그 결과에서 미세먼지와 초미세먼지 정보를 찾아내서 시리얼 모니터에 출력해 봅니다.


1
2
3
4
5
6
7
8
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    result += line;
    Serial.print(line);
  }
 
  findPMValue(result);
cs


loop() 안의 조회 결과를 읽어오는 부분을 변경해서 String형 result 변수에 읽어온 조회 결과를 모두 저장하게 한 다음, 위의 예제를 살짝 변형해 findPMValue 함수를 만들어서 미세먼지와 초미세먼지 농도값만 전역변수에 저장하고 시리얼 모니터에 출력해 줍니다.



이렇게 값을 받아와서



그 중에서 미세먼지와 초미세먼지 값을 찾아서 출력해 주었습니다. 잘 되네요.


이제 이 값들을 LCD에 출력해주기만 하면 어려운 부분은 다 끝난 셈입니다.

+ Recent posts