Loading Data from SD Card & Sending via Bluetooth to Android

I have a heart signal ( mp3 file) which I imported into Matlab, extracted the data points and stored in a text file. I copied the text file onto an 8 Gig SD Card

I modified the SD card file dump example in Arduino and combined with a Bluetooth transfer example. I am unable to receive the data on the android phone when I try the code (See Below). But the Bluetooth program and the SD card file dump examples work when tested independently.

Please, can anyone help to successfully load the data from SD on Arduino, and transfer to Android via Bluetooth.

I used Arduino Uno, HC-05 (cz-hc-05 Gomoku ) and SD Shield 3.0.
I also used the android application BlueTerm 2.


#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
//SoftwareSerial BTserial(2, 3); // RX | TX
// Connect the HC-05 TX to Arduino pin 2 RX.
// Connect the HC-05 RX to Arduino pin 3 TX through a voltage divider.
//

void setup()
{
Serial.begin(9600);
Serial.println("Enter AT commands:");

// HC-06 default serial speed for communcation mode is 9600
BTSerial.begin(9600);
}

void loop()
{
BTSerial.begin(38400); // HC-05 default speed in AT command more
float sensorValue = analogRead(A0)*5.0/1023.0;
BTSerial.println(sensorValue); //This allows me to simultaneoulsy see the voltage values on the android phone & the Serial monitor
Serial.println(sensorValue); //

delay(1000);
}


#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>

//​heart sound files available : ​HSAPRIL4, DUMMY, HSEDITED​ ​

SoftwareSerial BTSerial(10, 11); // RX | TX
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
BTSerial.begin(38400); // HC-05 default speed in AT command more

}

Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("HSEDITED", FILE_WRITE);
// re-open the file for reading:
myFile = SD.open("HSEDITED.txt");
if (myFile) {
Serial.println("HSEDITED.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
** Serial.write(myFile.read());**
** BTSerial.println(Serial.write(myFile.read()));**
** // Challenge is getting the data read & shown on the Serial monitor, to be transfered via bluetooth to Android**​

}

// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening HSApril4.txt");
}
}
void loop() {

}


Please enclose your code in Code Tags </>, for easy reading and to prevent loss of characters.

This statement does not make sense:

      BTSerial.println(Serial.write(myFile.read()));

SoftwareSerial is not very reliable, should not be used with baudrates as high as 38kBaud.

Thanks for the suggestion.
With the help of a friend, we were able to successfully resolve the challenge...

I had to only change from Digital Pins 10, 11 to Digital Pins 2,3 as SD shield was already engaging the pins.

/*
  SD card read/write

 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>

//HSAPRIL4, DUMMY, HSEDITED


[b]SoftwareSerial BTSerial(2, 3); // RX | TX[/b]

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only 
  }
  BTSerial.begin(9600);  // HC-05 default speed in AT command more

  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
}

void loop() {
  BTSerial.begin(38400);
  // re-open the file for reading:
  myFile = SD.open("HSEDITED.txt");
  if (myFile) {
    //Serial.println("HSEDITED.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      BTSerial.begin(38400);
      char reading[10] = "";
      char temp = myFile.read();
      int idx = 0;
      bool somethingWasRead = false;
      while (temp != -1 && temp != 13 && temp != 10){
        reading[idx] = temp;
        temp = myFile.read();
        idx++;
        somethingWasRead = true;
      }
      reading[idx++]='\0';
      //reading[idx] = 10;
      if (somethingWasRead){
       BTSerial.println(reading);
       Serial.println(reading);
       delayMicroseconds(500);
       //BTSerial.flush();
      }
     
    }
    
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening HSEDITED.txt");
  }
}

wow! thanks for sharing! i had the same issue with a datalog! only horrible outputs on my btserial with other "dump sd tutorials"... but with this everything go smooth!
a question only for my personal culture... i don't understand this "while (temp != -1 && temp != 13 && temp != 10)" can someone explain please?

this is a part of my code, when i send 1 via btserial the response is the content of the txt file
:slight_smile:

#include<SoftwareSerial.h>
#define TxD 3
#define RxD 2
SoftwareSerial btSerial(TxD, RxD);
char data = 0;


#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
File dataFile;

void setup()
{
 Serial.begin(9600); //set baud rate
 btSerial.begin(9600);
 delay(1000);
 btSerial.println("Hello, world?");



 Serial.print("Initializing SD card...");

 // see if the card is present and can be initialized:
 if (!SD.begin(chipSelect)) {
   Serial.println("Card failed, or not present");
   // don't do anything more:
   while (1);
 }
 Serial.println("card initialized.");

}

void loop() // run over and over
{
 if (btSerial.available()) {
   data = btSerial.read();
   Serial.println(data);
 }
 while (data == '1') {
   dataFile = SD.open("datalog.txt");
   if (dataFile) {  // read from the file until there's nothing else in it:
     while (dataFile.available()) {
       // btSerial.begin(9600);
       char reading[10] = "";
       char temp = dataFile.read();
       int idx = 0;
       bool somethingWasRead = false;
       while (temp != -1 && temp != 13 && temp != 10) {
         reading[idx] = temp;
         temp = dataFile.read();
         idx++;
         somethingWasRead = true;
       }
       reading[idx++] = '\0';    //reading[idx] = 10;
       if (somethingWasRead) {
         btSerial.println(reading);
         Serial.println(reading);
         delay(50);
         btSerial.flush();
       }

     }

     // close the file:
     dataFile.close();
     data = 0;

   }
 }
}

EDIT

after a day trying to understand why my Arduino reboots after bluetooth request, i've changed approach:
here part of the code:
really easier!

void BTsend()
{
  
  FileBT = SD.open("datalog.txt");
  if (FileBT) {  // read from the file until there's nothing else in it:

    while (FileBT.available()) {
btSerial.write(FileBT.read());
}
// close the file:
FileBT.close();
}
    MSG = 0;

  }