Base64 images are easy to transmit over the network. Base64 encoding of images for CSS is highly discussed on our blog. Base64 is not new to our regular readers but the current application is different. Here is How to Use Base64 Encoding on ESP32 Arduino.
ESP32 lacks sufficient RAM which limits the actual capabillity. You can actually use HTTP POST (like we have shown with IBM Watson IoT Platform) to send data to IBM’s server. IBM IoT also has limit of sending images (you have to use bin instead of JSON/text).
Original Arduino’s way of Base64 is the below (which many readers at least used for different projects):
---
1 2 3 4 5 6 7 8 9 10 | #include <base64.h> void setup() { Serial.begin(115200); String toEncode = "Hello World"; String encoded = base64::encode(toEncode); Serial.println(encoded); } void loop() {} |
ESP32 Arduino officially has the required library for basic functions around Base64 :
1 | https://github.com/espressif/arduino-esp32/blob/a59eafbc9dfa3ce818c110f996eebf68d755be24/tools/sdk/include/wpa_supplicant/crypto/base64.h |
Decoding a Base64 will go like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | extern "C" { #include "crypto/base64.h" } void setup() { Serial.begin(115200); char * toDecode = "paste--base64---code--here"; size_t outputLength; unsigned char * decoded = base64_decode((const unsigned char *)toDecode, strlen(toDecode), &outputLength); Serial.print("Length of decoded message: "); Serial.println(outputLength); Serial.printf("%.*s", outputLength, decoded); free(decoded); } void loop() {} |
Encoding of the phrase Hello World!
will be in this way :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | extern "C" { #include "crypto/base64.h" } void setup() { Serial.begin(115200); char * toEncode = "Hello World"; size_t outputLength; unsigned char * encoded = base64_encode((const unsigned char *)toEncode, strlen(toEncode), &outputLength); Serial.print("Length of encoded message: "); Serial.println(outputLength); Serial.printf("%.*s", outputLength, encoded); free(encoded); } void loop() {} |
One can change ESP32 camera’s input to Base64 in this way :
1 2 3 4 5 6 7 8 9 10 | size_t size = image.size; const uint8_t* image = image.data; uint8_t *buffer = calloc((size + 2 - ((size + 2) % 3)) / 3 * 4 + 1, sizeof(char)); // <--- equation from internet size_t buff_size = 0; int err = mbedtls_base64_encode(buffer, (size + 2 - ((size + 2) % 3)) / 3 * 4 + 1, &buff_size, image, size); if (err != 0) { ESP_LOGE(TAG, "error base64 encoding, error %d, buff size: %d", err, buff_size); return; } |
I beleive that using other kind of microprocessors with higher RAM is practical if the purpose is sending reasonable size image over web services, email. Camera with ESP32 with IoT works actually pushes it towards the end of capabillity due to hardware restriction. It is not abnormal to get hanged! You can resize image to tiny and send it.
Tagged With base64 for windows , arduino base64 decode esp32 , base64 arduino DECODE IMAGE , arfiomo bade64 omage , arduino mac libraries b64 , arduino encoder image base 64 , arduino crypto base64 , arduino base64 image , arduino base64 encode jpeg , https://thecustomizewindows com/2019/03/base64-encoding-on-esp32-arduino-base64-for-images/