0

I want to send some data from an Arduino Mega to an android device via bluetooth (HC-06). I have installed some sensors whose values are frequently written in an SD card. Every time a new string line is added in a text file of the SD card. This is the format of the line

27/5/2015,11:55:00,34.00,27.00,42.50,26.80,47.80,26.00,24.62,24.12,26,0,0,708,1023,1023,1023,1023,10.5,10.5,10.5,10.5

and this is part of the code. I send from a bluetooth terminal the string "download" and after that the text file is opened and every line is readed and sent to the android device via bluetooth.

void loop(void){  
    while(Serial1.available()) {
       character = Serial1.read();
       content.concat(character);
     }

if (content == "download") {
    Serial.println(content);
    content = "";
    read_from_SD_Card();
    }
    ........................
    ........................
    ........................
}


void read_from_SD_Card(){
    myFile = SD.open("test.txt");
    if (myFile) {
        Serial1.println("test.txt:");

        while (myFile.available()) {
            Serial1.write(myFile.read());
      }
      myFile.close();
    } 
else {
  Serial1.println("error opening test.txt");
  }
}

The problems are:

  • the data are downloaded only after an upload of the sketch has been done. (I can't upload the sketch once and download the data from time to time)

  • No more than 10-20 lines can be downloaded. With bigger files the downloading seems to slow down and finally stops after a few lines.

Any thoughts would be appreciated.

1

I suspect that this is a String class heap-fragementation issue (again). To test this you could sending a fixed string buffer (vector of characters, "Hello world" :). Do not use String and SD/FAT in the test. Just send a large number of lines with dummy data.

If this works replace the String usage with a character buffer. Fill it from the SD file (buffer size at a time) and write to Serial.

Cheers!

0
String content;
void loop()
{  
    while(Serial1.available()) {
       character = Serial1.read();
       content.concat(character);
     }

if (content == "download")
{
    Serial1.println(content);
    content = "";
    read_from_SD_Card();
    }

........................
    ........................
    ........................
}//end of loop()


read_from_SD_Card(){
    myFile = SD.open("test.txt");
    if (myFile) {
        Serial1.println("test.txt:");

        while (myFile.available()) {
            Serial1.print(myFile.read());
      }
      myFile.close();
    } 
else {
  Serial1.println("error opening test.txt");
  }
  Serial1.readString()//to flush the serial buffer.
}
  • Sorry, I changed Serial.write() to Serial.print() and added Serial.readString() in order to flush the buffer, but it didn't seem to solve the problem. – user3060854 May 29 '15 at 11:08
0

@user3060854 -- were you able to resolve this? A higher baud rate for communicating with the HC bluetooth module can help with your speed/slowdown problem. But have noticed baud rates above 115200 dont work well. I believe the problem is the receiver baud rate %error, which should not happen at 250k baud. But I havent figured out how to set a baud rate of 250k in the BT module. The question with details is here --- Setting non-standard baud rate (250k) for the HC-05 bluetooth module

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.