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

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

1 Like

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.

6A.- Arduino with SdCard sends a file to the App.

  • In the Arduino SdCard we have the following file:

servo.txt

1,2,3,4,5,6,7,8
-1,-2,-3,-4,-5,-6,-7,-8
10,20,30,40,50,60,70,80
-10,-20,-30,-40,-50,-60,-70,-80
100,200,300,400,500,600,700,800
-100,-200,-300,-400,-500,-600,-700,-800

  • We send the character "k" from the App to the Arduino via Bluetooth.
  • When Arduino receives the character "k", it sends via Bluetooth all the information contained in the servo.txt file of the SdCard to the App.
#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("servo.txt"); // Abre el archivo lectura. 
            if (miarchivo) {
              while (miarchivo.available()) {
                Serial.write(miarchivo.read());
                // delay(20);
              }
                Serial.print("*");
                miarchivo.close();
            }
            else {
              Serial.println("Error al abrir el archivo.");
            }
          }
   }
}

p9L2_Bluetooth_Archivoi.aia (44.6 KB)

6B.- Arduino - Bluetooth sends the file line by line every 2 seconds.

  • App sends character "k" to the Arduino via Bluetooth.
  • Arduino sends the file to the App line by line every 2 seconds:
    1,2,3,4,5,6,7,8
    2 seconds
    -1,-2,-3,-4,-5,-6,-7,-8
    2 seconds
    10,20,30,40,50,60,70,80
    2 seconds
    -10,-20,-30,-40,-50,-60,-70,-80
    2 seconds
    100,200,300,400,500,600,700,800
    2 seconds
    -100,-200,-300,-400,-500,-600,-700,-800
    2 seconds
    asterisk is (End Of File)
#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("servo.txt"); // Abre el archivo lectura. 
            if (miarchivo) {
              while (miarchivo.available()) {
                String line = miarchivo.readStringUntil('\n');
                Serial.println(line);
                delay(2000);
              }
                Serial.println("*");
                miarchivo.close();
            }
            else {
              Serial.println("Error al abrir el archivo.");
            }
          }
   }
}

p9L2_Bluetooth_Archivo_Linea.aia (46.2 KB)

  • Designer BluetoothClient DelimiterByte: 10

7.- Send a text file from the App to Arduino-Bluetooth.

  • leonardo.txt is a text file on the asset (8K).
  • Asterisk represents end of file.

p9L2_Bluetooth_Archivo_IO.aia (6.4 KB)

#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("leonardo.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.");
         }
  }
}

8.- Send Name and Age to SdCard. Get file. Delete file.

p9L2_Bluetooth_Archivo_Write.aia (7.2 KB)

  • Write Name and Age in a file on the SdCard.
  • If you send the "#" character, it gets the file from the SdCard and shows its content.
  • If you send the character "$", the file is deleted from the SdCard.
  • In BluetoothClient, DelimiterByte is 42, that is, we take the asterisk as the end of the message.

#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.");
}

void loop() {
  if(Serial.available()) {
    if (!miarchivo) {miarchivo = SD.open("datos.txt", FILE_WRITE);} // Abre el archivo.
    rx_byte = Serial.read(); // Toma el caracter.
    if(rx_byte == '#'){ // SHOW
              miarchivo.close(); // Cierra el archivo.
              miarchivo = SD.open("datos.txt"); // Abre el archivo lectura. 
              while (miarchivo.available()) {
                Serial.write(miarchivo.read());
                // delay(20);
              }
                Serial.print("*");
                miarchivo.close();
            }
            
      else if (rx_byte == '$') { // DELETE
         if (SD.remove("datos.txt")) {
         Serial.println("Archivo eliminado correctamente.*");
         } else {
         Serial.println("Error al eliminar el archivo.*");
           }
         }
            
      else if (rx_byte == '*') { // CLOSE
           miarchivo.close(); // Cierra el archivo.
           Serial.println("Grabado.*");
            }
      else { // WRITE
            miarchivo.print(rx_byte); // Guarda caracter.
           }
       }         
}

have you managed to do it
i am using esp32 and connect to android app with serialotg extension i have acheived controling some lights and display text on oled but i want to display picture also is it possible if you have done it please let me know