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.
Introduction
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 [1].
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.
The code
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.
#include <ArduinoJson.h>
Moving on to the Arduino setup, we will first open a serial connection, to later output the results of our program.
Serial.begin(115200);
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.
StaticJsonDocument<100> testDocument;
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:
{
"sensorType": "temperature",
"value": 10
}
To add these elements to the StaticJsonDocument, we simply need to use the [] operator, as shown below.
testDocument["sensorType"] = "Temperature";
testDocument["value"] = 10;
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.
char buffer[100];
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 [2]. 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.
Serial.print("Bytes written: ");
Serial.println(bytesWritten);
for(int i = 0; i<bytesWritten; i++){
Serial.printf("%02X ",buffer[i]);
}
The final code can be seen below.
#include <ArduinoJson.h>
void setup() {
Serial.begin(115200);
StaticJsonDocument<100> testDocument;
testDocument["sensorType"] = "Temperature";
testDocument["value"] = 10;
char buffer[100];
int bytesWritten = serializeMsgPack(testDocument, buffer);
Serial.print("Bytes written: ");
Serial.println(bytesWritten);
for(int i = 0; i<bytesWritten; i++){
Serial.printf("%02X ",buffer[i]);
}
}
void loop() {}
Testing the code
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.