Step-by-step guide to encoding binary data, sensor dumps, and camera frames into Base64 strings inside ArduinoJson v6 and v7 payloads for ESP32 and ESP8266.
Frequently Asked Questions
Q1. Does ArduinoJson have built-in Base64 encoding functions?
No, ArduinoJson focuses purely on JSON parsing and serialization according to RFC 8259. To store binary data, you must encode the bytes into Base64 using a helper library like base64.h or mbedtls/base64.h, and then assign the resulting string to a JSON field.
Q2. What is the formula for calculating Base64 buffer size in C++?
The required Base64 output buffer size (including null-terminator) is ((inputLength + 2) / 3) 4 + 1 bytes. For example, 100 bytes of binary input requires 137 bytes of buffer space.
Q3. How do I avoid heap fragmentation on ESP8266 when encoding Base64 in JSON?
Avoid repetitive String concatenation. Allocate a fixed-size char array stack buffer or use a statically allocated buffer, encode directly into it, and pass const char to ArduinoJson.
Q4. Can I use mbedTLS on ESP32 for Base64 encoding?
Yes! The ESP-IDF framework embedded in ESP32 Arduino core includes mbedtls/base64.h natively. The function mbedtls_base64_encode() is highly optimized in hardware.