Bluetooth HC-06 Send/Receive image.jpg file to/from Arduino. SdCard Reader

Hello friends,

  • Send an image.jpg from the Application to Arduino by Bluetooth HC-06. 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 HC-06. 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)

3 Likes

0.- Software.

These examples have been tested on Android 9, if you are going to test them on Android> 9 you must change the storage directory to ASD.

1 Like

1.- App converts an image: pozo_4.jpg to string Base64 by an extension.
The string is sent by Bluetooth HC-06 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 HC-06.
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 (89.4 KB)

pozo_4.txt (5.2 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("*")

  • In App DelimiterByte= 42 (because 42 is ASCII asterisk *)

  • Default baud in HC-06 is 9600.

  • With these codes, the pozo_4.txt (5.2 K) file is transmitted from Arduino to the App in 7 seconds.

1 Like

3.- Change baud rate in HC-06. AT commands.

HC-06 has 9600 bauds by default. Let's change it.

We make this connection. Notice that I have now connected the module to terminals 2 and 3 of Arduino.

We load this sketch:

// Juan A. Villalpando
// http://kio4.com/arduino/9L2_enviar_archivo_BT.htm

#include <SoftwareSerial.h>
SoftwareSerial BT(2,3);
 
void setup()
{
 BT.begin(9600);
 Serial.begin(9600);
 // BT.begin(115200);
 // Serial.begin(115200);
}
 
void loop(){ 
  if(BT.available()) {Serial.write(BT.read());}
 
  if(Serial.available()){BT.write(Serial.read());}
}

Serial Monitor: 9600
We wrote:

AT+BAUD8

and get OK154200

AT+BAUD1 ——— 1200
AT+BAUD2 ——— 2400
AT+BAUD3 ——— 4800
AT+BAUD4 ———9600 (Default)
AT+BAUD5 ——— 19200
AT+BAUD6 ——— 38400
AT+BAUD7 ——— 57600
AT+BAUD8 ——— 115200

we already have our HC-06 set to 15200 baud.

IMPORTANT, keep in mind that now it works with 115200, if you want to put the speed back to 9600, you must reload the sketch .ino, but changing these lines:

// BT.begin (9600);
// Serial.begin (9600);
BT.begin (115200);
Serial.begin (115200);

You must also set the Serial Monitor to 115200

Type AT + BAUD4 and it will be set back to 9600.

1 Like

4.- We repeat example 2, but now to 115200. Arduino sends a text file to App.

We modify the Arduino code:

#include <SoftwareSerial.h>
SoftwareSerial BT(2,3);

#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(115200);
BT.begin(115200);

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

void loop() {
   if(BT.available()) {
   caracter = BT.read();
   if(caracter == 'k'){
   miarchivo = SD.open("pozo_4.txt"); // Abre el archivo lectura. 
            if (miarchivo) {
              while (miarchivo.available()) {
                BT.write(miarchivo.read());
               // delay(2);
              }
                BT.print("*");
                miarchivo.close();
            }
            else {
              Serial.println("Error al abrir el archivo.");
            }
          }
   }
}
  • We use the same app that we saw in example 2:
    p9L2_Bluetooth_Base64_3.aia (89.4 KB)

  • We observed that when we set 9600 a 5.2K file takes 7 seconds to transfer from Arduino to the app. When we change the speed to 115200, it takes 1 second.

oooooooooooo Another example ooooooooooo

We load this file on the SdCard. It is a Base 64 text file of 800 kB:

pu_re.txt (796.7 KB)

  • We modify this line in the Arduino sketch:
    miarchivo = SD.open("pu_re.txt"); // Abre el archivo lectura.

  • The file will take 110 seconds to transfer from the Arduino to the App at 115200 baud.

5.- 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

1 Like

Hello Juan,
Can we show the image we received on the OLED LCD display?
Thank you..

Sorry @betul_uslu , I don't have code to display image on OLED LCD.
What screen are you using? some of these?
Images of OLED LCD

1 Like

Thank you for response Juan,
I am planning to use ssd1306 oled display. I will connect it to Raspberry Pi Pico. I'm wondering if I can show the picture I got from the Android app on this screen. So what I'm trying to understand is the possibility of displaying the image sent to the microSSD in this project.

I think it would be a question for the Raspberry forums. I have seen code to display simple images or text on ssd1306, but not very sharp images.