Text file from Arduino Mega to Android app through HM-10

Hello, guys!

I am trying to send a text file through HM-10.

According to GUIDE_2BT:

In summary, Arduino<>Android comms are established by two operations:

  1. Arduino>Bluetooth. This is done by programming, which may be as simple as
    Serial.begin(9600);
    Serial.println(“lahdedah”);
  2. Bluetooth>Android. This is done by procedure, all at the Android end – the pairing in the
    device settings, and the connection in the Android app.

This is my code:

//library for micro SD card
#include <SPI.h>
#include <SD.h>

//micro SD card
#define chipSelect 53
File myFile;


//soil humidity sensor -- pin
#define humiditySensorPin A0
int humidityPercent;


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

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  while (!Serial1) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  checkMicroSDCard();
}

void loop() {
  readHumidityTemp();
}

//function Arduino -> Bluetooh
//void fileToBluetooth() {
//  Serial1.println(
//}

//funtion to read humidity & temperature
void readHumidityTemp() {
  //read humidity sensor
  //int humiditySensorValue = analogRead(humiditySensorPin);
  //map values as percents
  //humidityPercent = map(humiditySensorValue, 1023, 0, 0, 100);
  int humidityPercent = 100;
  //write humidity value in file
  writeToFile(String(humidityPercent));

  delay(1000);
}

//function to write in file
void writeToFile(String dataToWrite) {
  myFile = SD.open("data.txt", FILE_WRITE);
  if (myFile) {
    Serial.println(dataToWrite);
    delay(250);
    myFile.println(dataToWrite);
    myFile.close();
  } else {
    Serial.println("error opening data.txt");
  }
}

//function to read file
void readFromFile() {
  myFile = SD.open("data.txt");
  if (myFile) {
    Serial.println("data.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
      
      // THIS LINE
      /////////////////////////////////////////////////////
      Serial1.write(myFile.read());
      /////////////////////////////////////////////////////
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}


//function to check if micro SD card is available
void checkMicroSDCard() {
  Serial.print("Initializing SD card...");

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

My problem is that the file is continuously changing, a new value is added after a period of time.

I want to get the value of humidityPercent and store it in a text file from second to second. After a while (this I think I can solve with millis [let’s say I want to send the file after 1 min]) I want to send all the text file data to my Android phone.

I studied Serial.print() and Serial.write():

Syntax
Serial.print(val)
Serial.print(val, format)

Parameters
val: the value to print - any data type

Syntax
Serial.write(val)
Serial.write(str)
Serial.write(buf, len)

Arduino Mega also supports:

Serial1, Serial2, Serial3 (in place of Serial)

Parameters
val: a value to send as a single byte

str: a string to send as a series of bytes

buf: an array to send as a series of bytes

From my point of view, I should use write(val).

This is what I have in my mind, but it’s a bit tricky. Could you please tell me if I am on the right way of solving this? Or drop me some hints?

Thanks in advance!

I'm still struggling with this... any help would be appreciated

My problem is that the file is continuously changing, a new value is added after a period of time.

I want to get the value of humidityPercent and store it in a text file from second to second. After a while (this I think I can solve with millis [let's say I want to send the file after 1 min]) I want to send all the text file data to my Android phone.

I'm not clear on what problem you are encountering. Can you please describe in more detail

What is the program is doing that you want it to do? What is the program doing that you don't want it to do? What is the program not doing that you want it to do?

Is data getting to the phone? Is too much data going to the phone?

This should not be difficult, but note that the guide notes you are using either don't, or specifically don't, deal with the HM-10 - only HC-0x. Having said that, this may not matter, as I understand you are getting communication, just not the right communication.

In Guide_2BT there is an example for downloading files. In it, you send the filename to Arduino, and the file of that name is returned. This is a matter of procedure, not necessarily involving bluetooth, and your real problem might be about filenames. I use the clock to name a new file at midnight and just send MMDD to retrieve it. You may do something similar, but you may not even need to change the fileneme, you just download the latest version.

If you have achieved success using Guide_2BT with an HM-10, I would be glad to hear about what you did.

cattledog: I'm not clear on what problem you are encountering. Can you please describe in more detail

What is the program is doing that you want it to do? What is the program doing that you don't want it to do? What is the program not doing that you want it to do?

Is data getting to the phone? Is too much data going to the phone?

Sorry for the late reply.

First of all, this is what I use:

  • Arduino Mega 2560
  • BLE Bluetooth 4.0 HM-10

The code of the project can be found here. My code works fine, both on my phone and on my Arduino.

The only problem that I have is this:

My Arduino has a file stored on its SD card. I want to send this file via Bluetooth to my Android phone. As I read on the internet, I still didn't manage to get a clear view of how to do this and how this exactly works (also I want to write my Android app, so I don't want to use an existing app from the Play Store).

I managed to establish the connection between my Arduino and Android phone using an app from Play Store, so things are ok from this point of view. I didn't manage to send data using that app (I'll do some more research and come back with an update on this).

LE: by sending the file I mean sending the data the file contains (it will contain some numbers, perhaps stored as a JSON)

In one line: I can't find a more complex tutorial on how to send a file from my Arduino to an Android phone or something more explicit. If you know some good books or tutorials please let me know.

grrigore: I can't find a more complex tutorial on how to send a file from my Arduino to an Android phone or something more explicit. If you know some good books or tutorials please let me know.

Read reply #3, again, and the DumpFile example in the IDE. No complex tutorial needed.