2018년 7월 12일 목요일

ESP32에서 JSON 파싱

ESP32에서 ArduinoJson 라이브러리를 사용해 JSON 메시지를 파싱 해 보겠다.

먼저 ArduinoJson 라이브러리가 설치되어 있어야 한다.

만일 설치되어 있지 않으면 메뉴의 스케치->라이브러리 포함하기->라이브러리 관리...를 선택해 준다.


라이브러리 메니저 창이 열리면 1번 부분에 ArduinoJson 을 입력하면 해당 키워드를 가진 라이브러리 목록이 보인다. 목록중에 ArduinoJson을 찾아 그 부분을 클릭하면 3과 같이 라이브러리 우측 하단에 '설치' 버튼이 나타난다. '설치' 버튼을 눌러 설치해주면 된다. 설치가 완료되면 '닫기' 버튼을 눌러준다.


이제 라이브러리를 가지고 실제 코드를 작성한다.

가장 먼저 ArduinoJson 라이브러리를 include 해 줘야 한다. 또한 loop 함수에서 실제 파싱을 수행할 것이므로 결과를 확인하기 위해 시리얼 포트를 열어준다.

#include <ArduinoJson.h>

void setup()
{
  Serial.begin(115200);
  Serial.println("JSON parsing using ArduinoJson library on ESP32");
}

여기서는 네트웍에서 JSON 데이터를 불러오지 않고 하드코딩 된 JSON 메시지를 파싱 해 볼 것이기 때문에 이를 위한 변수를 선언해 준다. 여기에서 '\' 은 메지시 내에 들어가는 따옴표(")에 대한 escape character이다. JSON의 name에 따옴표(")가 있어야만 하기 때문에 필요하다.

char JSONMsg[] = " {\"SensorType\", \"Temperature\", \"Value\", 10}";

Escape character가 없는 JSON 메시지의 구조는 다음과 같다.

{
  "SensorType" : "Temperature",
  "Value" : 10
}

주의) JSON 파서는 스트링 내용을 수정하기 때문에 내용을 재사용 할 수 없다. 그래서 여기서도 JSONMsg 변수를 전역변수가 아니고 loop 내에서 선언해 loop 함수가 끝나면 free 되고, 다음번에 다시 loop() 함수가 호출될 때 다시 할당되도록 해 놓았다.

또한 파싱한 결과인 JSON object tree를 저장하기 위해 미리 할당된 메모리 풀을 위해 StaticJsonBuffer 클래스 오브젝트를 선언했다.  이 오브젝트는 메모리 풀이기 때문에 크기를 지정해 줘야 한다. 크기는 템플릿 파라미터(코드에서 < > 사이의 값)에 바이트 단위로 지정해 주면 된다. 여기서는 300바이트를 지정했는데 이 정도면 위의 JSON 메시지를 파싱하는데는 충분하다.

StaticJsonBuffer<300> JSONBuffer;    // memory pool for parsed JSON object tree

다음으로는 StaticJsonBuffer 오브젝트의 parseObject 메소드를 호출하면서 파싱하길 원하는 JSON 스트링을 넘겨준다.

JsonObject &parsed = JSONBuffer.parseObject(JSONMsg);

에러 없이 파싱이 끝났는지 확인하기 위해 JSONObject의 success 메소드를 호출할 수 있다.

if (!parsed.success()) {    // check for errors in parsing
  Serial.println("Parsing failed!");
  delay(5000);
  return;
}

이제 JsonObject에서 이름으로 그에 해당하는 값을 가져오기 위해 subscript operator를 사용할 수 있다. 즉 key에 해당하는 값을 가져오기 위해 원하는 파라미터의 key 이름을 [ ]으로 둘러싸면 된다. 이 예제에서 사용하고 있는 key 이름은 "SensorType"과 "Value" 두 가지다.

const char *sensorType = parsed["SensorType"];     // get sensor type value
int value = parsed["Value"]  // get value of the sensor measurement

전체 코드는 다음과 같다.

#include <ArduinoJson.h>
 
void setup() {
 
  Serial.begin(115200);
  Serial.println("JSON parsing using ArduinoJson library on ESP32");
}
 
void loop() {
 Serial.println("Parsing start: ");
 char JSONMsg[] = " {\"SensorType\": \"Temperature\", \"Value\": 10}"; //Original message
 
  StaticJsonBuffer<300> JSONBuffer;                         //Memory pool
  JsonObject& parsed = JSONBuffer.parseObject(JSONMsg); //Parse message
 
  if (!parsed.success()) {   //Check for errors in parsing
    Serial.println("Parsing failed!");
    delay(5000);
    return;
  }
 
  const char * sensorType = parsed["SensorType"]; //Get sensor type value
  int value = parsed["Value"];                    //Get value of sensor measurement
 
  Serial.print("Sensor type: ");
  Serial.println(sensorType);
  Serial.print("Sensor value: ");
  Serial.println(value);
 
  Serial.println();
  delay(5000);
}


위의 코드를 ESP32에 다운로드 해서 실행하고 시리얼모니터를 열면 다음과 같은 결과가 출력 될 것이다.

JSON parsing using ArduinoJson library on ESP32
Parsing start:
Sensor type: Temperature
Sensor value: 10

Parsing start:
Sensor type: Temperature
Sensor value: 10

Parsing start:
Sensor type: Temperature
Sensor value: 10

...





댓글 없음:

댓글 쓰기