ESP32 ArduinoJson: Getting the length of the MessagePack serialization payload
DFRobot
Jul 23 20191099
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.
Then, to obtain the length of the payload produced by the MessagePack serialization, we simply need to call the measureMsgPack function. 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.
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.
This article takes a closer look at each of these devices, comparing them across key areas like Python support, ease of use, Python programming tools, pin design, and hardware projects.
Deciding between Scratch and Python for your child's coding journey? This article compares both languages based on learning background, goals, methods, and difficulty.