Transferring a text file from Arduino to laptop (via Bluetooth)

Hello,

For context, what I am currently doing is I have a HC-05 module linked to my Arduino UNO and I am able to transfer data to my laptop using the serial port.

Currently the process is:

  1. Power the Arduino (and HC-05)
  2. Pair HC-05 with laptop
  3. Open a data logging software (CoolTerm in my case)
  4. Start capture (and create the text file the data is written to)
  5. Connect to the Serial Bluetooth Port
  6. Stop capture after data is received.

Currently what's happening is data is sent via the serial port and the text file is made on the laptop side. I am wondering if there's a way to create the text file on the Arduino side and send it over to the laptop via Bluetooth. From my research, I think I can get the Arduino to create the text file and store it onto an SD card or something. (I have not started this so there's no code to show).

The part I need help with is how to send that text file over to the laptop. My intention is basically to skip steps 3-6 in the process described above. Just like how we can transfer files from phone to laptop via Bluetooth (without needing any data logging software), I want to do the same for Arduino -----> Laptop. Is this possible? If so, can you guide me as to how to go about it?

Thanks

There are examples in the SD library that deal with this. Coolterm should be fine at the PC end. If not, use RealTerm.

Thank you for the response.

Just to clarify my question. I want help with the Bluetooth transfer part. I think I can get the "writing to a text file on SD card" part sorted.

Referring to the use of CoolTerm or RealTerm, what I want to do is like not use them if that makes sense. For example, if I want to send a file from my phone to my laptop. I can do that simply by pairing them up and doing the file transfer. I wouldn't need any extra software like CoolTerm or RealTerm since the file would just appear on my laptop. Is there a way to emulate this sort of thing from Arduino to laptop (assuming I have a text file stored in the Arduino SD card)?

And there is code on the phone and laptop to do that. It is just you didn't have to write it. There is no magic, you need some code on the PC to receive the data and copy it to a file. You can (ab)use a terminal program to do that or write specific code to do it. When I did it I used a Python routine to do the file transfer. I sent the data a line at a time from the Arduino side.

Arduino code

/********************************************************************
 *
 * Read root directory
 * Send file names and sizes to serial port
 *  
 ********************************************************************/ 
void ReadDir(SdFat &sdCard) {
    File root;
    File entry;
    char fileName[13]; 
    uint32_t fileSize;
    //
    // read directory
    //       
    root = sdCard.open("/");
    if (root) {
        root.rewindDirectory();  // need to rewind the directory 
                                    // if ANY file operation has been done
    //       Serial.println("Root open");
        while (true) {
            File entry =  root.openNextFile();
            if (! entry) {
                // no more files
                break;
            }
            if (!entry.isDirectory()) {
                entry.getName(fileName,13);
                Serial.print(fileName);
                Serial.print("\t");
                fileSize = entry.size();
                Serial.println(fileSize, DEC);
            }
            
            entry.close();
        }
    //       Serial.println("end of dir");
        root.close();
    }  
    else LOG_DEBUG(<<"unable to open root"<<endl);
}
/********************************************************************
 *
 * Read specified file and send data to serial port
 *  
 ********************************************************************/ 
void ReadFile(SdFat &sdCard,const char* fileName) {
     File dataFile;    
    // 
    // read a file
     //               
    dataFile = sdCard.open(fileName);
    if (dataFile) {
        //opened file   
        while (dataFile.available()) {
            Serial.write(dataFile.read());
        }
        dataFile.close();
    }
    else{
        LOG_DEBUG(<<"Unable to open file "<<fileName<<endl);
    }
                
}

Python code The "messages" dump, dir, file= and exit are received on the Arduino side and used to control the data transfer. Dump indicated a file transfer, dir requests the directory, file= indicated which file and exit indicates the file transfer mode is done.

"""
Send commands to the Arduino to read the directory on the SD card
Copy data from the SD card to a local file

Remember that Python 3 strings use Unicode so the data transfered must
use byte arrays or strings must be encoded and decoded
"""


import time
import serial
import serial.tools.list_ports

#
# Find the USB port the Mayfly is on
#
commports = serial.tools.list_ports.comports() # get possible ports
numPorts = len(commports)
if (numPorts == 0):
    print("No serial ports available\n\n")
    exit()
if (numPorts>1):
    # Have user pick one
    portNum = 0
    for port in commports:
        print("port number ",portNum)
        print(port)
        portNum = portNum+1
    usePort = int(input('enter port number to use 0-'+str(numPorts-1)+':'))
else:
    usePort = 0

thePort = commports[usePort][0]
print('using ',thePort,'\n')

# open serial port
device = serial.Serial(thePort, 9600, timeout=2)

# Remember every time you connect to the Arduino it
# resets (Unless you disconnect DTR)
time.sleep(2)   # give time for Arduino to reset
device.reset_input_buffer() # flush any left over Arduino input -just in case
device.write('dump\n'.encode())  # set dump mode
device.readline() # flush response
device.write('dir\n'.encode())  # get file list
# entries are of the form <file name> <tab> <file size>

# format the directory entries into 2 col with the
# file names numbered for easy selection 
fileEntry = []
for line in device:
    fileEntry.append(line.decode().rstrip())
numFileEntries = len(fileEntry)    
#print(fileEntry)

colSize = int((numFileEntries)/2)
for element in range(0,colSize+1,2):
     print(element,fileEntry[element],'\t',element+1,fileEntry[element+1])
if (numFileEntries>colSize*2):
     print(numFileEntries-1,fileEntry[numFileEntries-1])	 

# download the file
fileNum = input("\nEnter number of file to download ")
if (len(fileNum)>0):
    fileNum=int(fileNum)
    fileName = fileEntry[fileNum].split('\t')[0] 
    print('\nDownloading',fileName,'\n') 
    logfile = 'file='+fileName+'\n'
    fileout = open(fileName,'w')
    device.write(logfile.encode())  # request file to download
    for line in device:
        outLine = line.decode().rstrip()
        print(outLine)
        outLine = outLine+'\n'
        fileout.write(outLine)
    fileout.close()
device.write('exit\n'.encode())  # send done message    
device.close()

That makes sense if you want to send the data direct to Excel instead, in which case you use Datastreamer or PLX v2. Otherwise, it makes no sense at all.

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