In this tutorial we will learn how to serialize content to the MessagePack format, using the ArduinoJson library and the ESP32. The tests shown here were performed using an ESP32 board from DFRobot.
In this tutorial we will learn how to serialize content to the MessagePack format, using the ArduinoJson library and the ESP32.
MessagePack is an efficient binary serialization format that is more compact that JSON .
This tutorial targets version 6 of ArduinoJSON. The library can be easily installed from the Arduino IDE library manager. The tests shown here were performed using an ESP32 board from DFRobot.
We will start our code by including the ArduinoJson.h library. This way, we will have access to the functionalities to perform the message pack serialization.
Moving on to the Arduino setup, we will first open a serial connection, to later output the results of our program.
Then, we will declare an object of class StaticJsonDocument. This object will hold the memory representation of our object. For a more detailed explanation on how the StaticJsonDocument works, please consult this previous tutorial.
When declaring an object of this class, we need to specify its capacity, in bytes. The value is specified as a template parameter. For this tutorial we will specify a capacity of 100 bytes, which is more than enough for the object we want to store.
For a more accurate estimation, the easiest way is use this assistant.
Next we will take care of adding the members to our StaticJsonDocument, to hold the actual data. We will be using the same structure we have been testing in previous tutorials:
To add these elements to the StaticJsonDocument, we simply need to use the  operator, as shown below.
Now that we have the in memory representation of our object, we will take care of serializing it to the MessagePack format. The first thing we need to do is declaring a data buffer to hold the serialized content.
Then, to do the actual serialization, we need to call the serializeMsgPack function, passing as first input our StaticJsonDocument and as second input the destination buffer.
As output, the function will return the number of bytes written . We will store this value in a variable.
int bytesWritten = serializeMsgPack(testDocument, buffer);
Then we will print to the serial port the number of bytes written to the buffer. After that, we will iterate through each byte of the array and print it in hexadecimal format.
To test the code, simply compile it and upload it to your ESP32 using the Arduino IDE. When the procedure finishes, open the serial monitor tool. You should have an output similar to figure 1.
Figure 1 – Output of the program, showing the message serialized to MessagePack format.
To confirm that we have obtained the correct content, we will then deserialize the message back to JSON format, using this online tool.
To do it, simply copy the bytes of the message printed to the Arduino serial monitor. On the online tool, paste them on the right side text box. Make sure that you select the option “HEX” on the radio buttons over the text box.
Then, click the green “decode” button. The message should be decoded back to JSON format, as shown in figure 2. As can be seen, we obtained the original content we have stored in the StaticJsonDocument, in JSON format.
Figure 2 – Decoding the MessagePack message to JSON.