SD file transfer won't begin until Serial Monitor opened

I was able to successfully write a sketch that records some sensor data and stores it on a SD card. I can easily retrieve the data by pulling the card and opening on a laptop; however, I need to get the file without removing the card. I'm using an HM-10 bluetooth with TX/RX through Serial1 to an iOS BLE terminal. Everything works fine but none of the file contents are transferred until I physically open the serial monitor (the button on the top right part of Arduino sketch window). Is there a way to automate this without having to click the serial monitor button? Perhaps some command line from the BLE terminal to initiate? The code below was used to simply try and retrieve the SD file data remotely through bluetooth. It works fine as long as you do what is described above.

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


void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial1.begin(9600); 

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

 if (!SD.begin(12)) {
   Serial.println("initialization failed!");
    return;
  }
 Serial.println("initialization done.");
  
  File dataFile = SD.open("test.txt");

  // if the file is available, write to it:
  if (dataFile) {
    while (dataFile.available()) {
      Serial1.write(dataFile.read());
    }
    dataFile.close();
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}

void loop() {
}

Which board?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.