2019年7月31日星期三

ESP32 ArduinoJson: Getting the length of the MessagePack serialization payload

In this tutorial we will check how to obtain the length of the MessagePack payload that is obtained when we serialize data to this format, using the ArduinoJson library. The tests shown here were performed using an ESP32 board from DFRobot.

Introduction

In this tutorial we will check how to obtain the length of the MessagePack payload that is obtained when we serialize data to this format, using the ArduinoJson library. We will be using the Arduino core, running on the ESP32.

For an introductory tutorial on how to serialize content to the MessagePack format, please check this previous tutorial.

The tests shown here were performed using an ESP32 board from DFRobot, using version 6 of ArduinoJson.

The code

As usual, we will start the code by including the ArduinoJson library.

#include <ArduinoJson.h>

Then, moving on to the Arduino setup function, we will start by opening a serial connection, so we can output the result of our program.

Serial.begin(115200);

After this, we will declare an object of class StaticJsonDocument, to hold the memory representation of our data.

StaticJsonDocument<100> testDocument;

For this tutorial we will be using the same structure we have used in previous tutorials:

{
    "sensorType": "temperature",
    "value": 10
}

So, to add these elements to our StaticJsonDocument, we simply need to use the [] operator.

testDocument["sensorType"] = "Temperature";
testDocument["value"] = 10;

Then, to obtain the length of the payload produced by the MessagePack serialization, we simply need to call the measureMsgPackfunction. As input, we will pass our StaticJsonDocument. As output, the function will return the length, which we will print to the serial port.

int expectedBytesToWrite = measureMsgPack(testDocument);
 
Serial.print("Expected Bytes: ");
Serial.println(expectedBytesToWrite);

For comparison, we will now do the actual serialization of the data to the MessagePack format, like we have done in the previous tutorial.

So, we start by declaring a data buffer to hold the result and then we simply call the serializeMsgPack function to do the actual serialization.

Note that this function returns the number of bytes written to the output buffer, which corresponds to the length of the payload. Thus, we will also print this value to the serial port, to confirm it matches the value returned by the measureMsgPack function.

char buffer[100];
 
int bytesWritten = serializeMsgPack(testDocument, buffer);
  
Serial.print("Bytes written: ");
Serial.println(bytesWritten);

The final source code can be found below.

#include <ArduinoJson.h>
    
void setup() {
    
  Serial.begin(115200);
      
  StaticJsonDocument<100> testDocument;
      
  testDocument["sensorType"] = "Temperature";
  testDocument["value"] = 10;
 
  int expectedBytesToWrite = measureMsgPack(testDocument);
 
  Serial.print("Expected Bytes: ");
  Serial.println(expectedBytesToWrite);
    
  char buffer[100];
 
  int bytesWritten = serializeMsgPack(testDocument, buffer);
  
  Serial.print("Bytes written: ");
  Serial.println(bytesWritten);
 
}
  
void loop() {}

Testing the code

To test the code, simply compile it and upload it to your ESP32, using the Arduino IDE. After the procedure is finished, open the Arduino IDE serial monitor.

You should obtain a result similar to figure 1. As can be seen, the lengths printed to the serial match, as expected.

Figure 1 – Output of the program, showing the expected and the actual length of the MessagePack payload.

没有评论:

发表评论