Bluetooth HC06 Send/Receive image.jpg file to/from Arduino. SdCard Reader

Hello friends,

  • Send an image.jpg from the Application to Arduino by Bluetooth HC06. The App will need to convert the image to Base64 using an extension. Arduino will save the image.txt Base64 in an Sdcard.

  • Send an image.txt from Arduino with SdCard to the Application by Bluetooth HC06. The image.txt file will be converted to image.jpg in the application using an extension.

:frowning_face: only small images.jpg (approximately less than 50 kB)

2 Likes

0.- Software.

1 Like

1.- App converts an image: pozo_4.jpg to string Base64 by an extension.
The string is sent by Bluetooth HC06 to Arduino.
Arduino receives the string, converts it into the file pozo_4.txt and saves it in a micro SdCard.

p9L2_Bluetooth_Base64_2.aia (78.7 KB)

  • Asterik * for end_of_send
  • You can try with pozo_4kB, pozo_20kB and pozo_50kB

#include <SD.h>
// const int CS = D8; // Para el NodeMcu
const int CS = 4; // Para el Arduino y Wemos ESP32
File miarchivo;
char rx_byte = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("Iniciando SdCard...");
  if (!SD.begin(CS)) {
    Serial.println("Error al iniciar.");
    return;
  }
    Serial.println("SdCard iniciada.");
    miarchivo = SD.open("pozo_4.txt", FILE_WRITE); // Abre el archivo.
}

void loop() {
  if(Serial.available()) {
    rx_byte = Serial.read(); // Toma el caracter.
         if (rx_byte != '*') {
          miarchivo.print(rx_byte); // Guarda caracter.
         } else {
          miarchivo.close(); // Cierra el archivo.
          Serial.println("Grabado.");
         }
  }
}

Arduino receives rx_byte, saves those bytes in SdCard, file pozo_4.txt
When the asterisk arrives, close file.

  • Time to upload:
Size file .jpg Size Base64 .txt Transmission time
pozo_4.jpg 3.879 5.241 9 seconds
pozo_20.jpg 18.192 24.576 50 seconds
pozo_50.jpg 42.983 58.067 120 seconds
1 Like

2.- Arduino sends pozo_4.txt to App by Bluetooth HC06.
App receives this string Base64 and converts it to file: /mnt/sdcard/el_pozo.jpg
Image component shows that image.

p9L2_Bluetooth_Base64_3.aia (79.3 KB)

  • When btn_receive click, App sends character “k” to Arduino, then reception will start.

  • When App receives the asterisk * (code 42), it will interpret that the reception has finished.

  • Extension will convert the received string to /el_pozo.jpg. It will be displayed in the Image component.

  • Code Arduino:

#include <SD.h>
// const int CS = D8; // Para el NodeMcu
const int CS = 4; // Para el Arduino y Wemos ESP32
File miarchivo;
char caracter;
     
void setup() {
Serial.begin(9600);

  Serial.println("Iniciando SdCard...");
  if (!SD.begin(CS)) {
    Serial.println("Error al iniciar.");
    return;
  }
    Serial.println("SdCard iniciada.");
}

void loop() {
   if(Serial.available()) {
   caracter = Serial.read();
   if(caracter == 'k'){
   miarchivo = SD.open("pozo_4.txt"); // Abre el archivo lectura. 
            if (miarchivo) {
              while (miarchivo.available()) {
                Serial.write(miarchivo.read());
                delay(2);
              }
                Serial.print("*");
                miarchivo.close();
            }
            else {
              Serial.println("Error al abrir el archivo.");
            }
          }
   }
}
  • Arduino sends characters to App by Bluetooth. Last character is asterisk, Serial.print("*")
1 Like

3.- Base64 in Arduino.

  • With this library we can encode/decode a String Base64 in Arduino, but…

  • this can encode/decode a String, not a file.

  • we can insert a code to convert a file to String and encode/decode it, but…

  • Arduino has low memory, cannot work with variables larger than 2K

Solutions?

Look at this comparison of Arduino UNO, ESP8266 and ESP32:

base64_11

  • ESP32 works to 160 MHz, 512 kB, Bluetooth, BLE,…
    in the next tutorial we will try with this board…

1 Like