This is a quick tutorial on how to log messages coming from your Arduino into your OSX box and save them in a file. I found this while debugging an Arduino//Ethernet project that had verbose server response that I needed to save. I passed Serial.print() and Serial.println() messages from Arduino into a terminal screen and recorded them into a text file with script. (The sequence for exiting script and leaving you with a readable file is important so read to bottom).
UPDATE:
everything below remains valid but this is the simple way (yes I finally read the man page):
screen -L /dev/tty.usbXXXXX 9600
-L is the log switch — it will write a file called screenlog.0 in the working directory.
you can still do all the other stuff below if you like — but it was clearly unnecessary.
Start Terminal
Open terminal (spotlight it, Finder–> GO –> Utilities –> Terminal, add it to your dock).
Navigate to the folder in which you plan to save the logfile. I am going to use a folder called myLogs in Documents.
cd Documents/myLogs/
You can choose any folder.
Start Script
On command line type:
script -a -t0 filename.txt
Terminal will respond:
Script started, output file is filename.txt
ribosomemyLogs~ :>
(NOTE:: ribosome is my computer name — you will see yours, use your own file name )
At the new prompt you will begin logging. You can stop script with control-D. Terminal will respond:
Script done, output file is filename.txt
Formatting the script call:
- -a :: script set to append (ensures script adds to the file you create.
- -t :: time, the frequency between append calls. Default 30sec. I am using -t0 (that’s a zero) which captures everything (escape characters will look funny) as it happens
- filename.txt :: name of the logfile choice — it will be created for you if it doesn’t already exist — it will over right. Note the file is created in the working directory if you do not give a path.
After you start script, all your key strokes and all responses are logged. To capture your Arduino’s serial messages you must now set up screen.
Start Screen
For serial display, the command follows the form:
screen PORT BAUD and on OSX looks like this:
screen /dev/tty.usbserial-A6006kJj 9600
where /dev/tty.usbserial-A6006kJj is my Arduino (put your own value here, check Tom Igoe’s post on how to do this)
and 9600 is the speed used to start Serial in the Arduino setup call (change it if you use a different speed).
You should now see all your serial messages from Arduino in the terminal window and they will be getting logged to your file. You can open the file on the fly and see what’s happening if you like — it will be current up to the last save.
Stop Screen
To stop the processes you must quit screen and then quit script (in that order).
To stop screen enter:
control-A then control-K
You will be asked if you want to kill the window. Type y and screen will stop.
Stop Script
To stop script enter control-D
You will get the following response:
Script done, output file is filename.txt
You should now be back to normal terminal prompt.
Open filename.txt in s text editor.
Note
Using screen is similar to opening a debug window inside the Arduino environment. Having it open while trying to upload new code from the IDE will cause port conflicts and failures to write. Simply close screen before uploading (Arduino closes its own debug windows for you).