Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write data to a text file on Arduino

I have some position data continually coming in and I am currently printing it to the serial.

Say I have the string "5" and want to print that to a text file, "myTextFile", what would I need to do to achieve this? To be clear, the text file would be saved on my computer not on an SD card on the Arduino.

Also, is their a way to create a text file within the program before I start saving to it?

like image 346
Michael Zakariaie Avatar asked Nov 08 '16 06:11

Michael Zakariaie


1 Answers

You can create a python script to read the serial port and write the results into a text file:

##############
## Script listens to serial port and writes contents into a file
##############
## requires pySerial to be installed 
import serial  # sudo pip install pyserial should work

serial_port = '/dev/ttyACM0';
baud_rate = 9600; #In arduino, Serial.begin(baud_rate)
write_to_file_path = "output.txt";

output_file = open(write_to_file_path, "w+");
ser = serial.Serial(serial_port, baud_rate)
while True:
    line = ser.readline();
    line = line.decode("utf-8") #ser.readline returns a binary, convert to string
    print(line);
    output_file.write(line);
like image 168
Ulad Kasach Avatar answered Oct 19 '22 17:10

Ulad Kasach