Skip to content

Instantly share code, notes, and snippets.

@binh-bk
Last active May 18, 2022 04:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binh-bk/dd8160d3226a355f00ead3cd73c92898 to your computer and use it in GitHub Desktop.
Save binh-bk/dd8160d3226a355f00ead3cd73c92898 to your computer and use it in GitHub Desktop.
record serial and log output, useful tools working Arduino
#! /usr/bin/python3
'''
to use python 2 use /usr/bin/python2
requires pySerial to be installed
add date to the header, and time to the begining of each line
add sys argument to customize
USE: make executable by chmod +x scriptname.py and
> ./recordSerial.py test1000.txt (and Enter)
'''
import serial, time, sys
print("Press Ctrl+C to save and stop the logging")
baud_rate = 115200 # In arduino, Serial.begin(baud_rate), e.g. 115200
default = time.strftime("log_%Y%m%d_%H%M.txt")
serial_port = '/dev/ttyUSB0' # listening port, type ls /dev/ttyUSB* in shell for available ports
if len(sys.argv) == 2:
logfile_name = sys.argv[1]
else:
logfile_name = default
output_file = open(logfile_name, "a+")
output_file.write(time.strftime("%x\n"))
ser = serial.Serial(serial_port, baud_rate)
count = 0
try:
while True:
line = ser.readline();
line = line.decode("utf-8") # ser.readline returns a binary, convert to string
timeNow = time.strftime("%X")
outputline = ",".join((timeNow, line))
if line != "\n": # not record an empty line
print(timeNow, line)
output_file.write(outputline)
count +=1
else:
continue
# print("\nnothing here")
if count >= 10: # to save file to disk every 10 lines
output_file.close()
output_file = open(logfile_name, "a+")
count = 0
except KeyboardInterrupt:
print(">> Ctrl+C pressed, stop logging to {} file".format(logfile_name))
output_file.close()
raise SystemExit
@ColinStuartDevelopment
Copy link

I had an idea for a garden monitoring system with 5 Arduinos and a Raspberry Pi. Like any other learning experience I would hit speed bumps at every new step in the process, but was able to overcome and solve each one. Until I needed to log in 'real time' the serial data onto the Pi. For some reason what I was looking for would be partially online. Each tutorial kind of giving me what I was looking for but the pieces never fit together to solve the overall puzzle.

Then I found your instructables post on doing this and lo' and behold there's a link to your github where this is posted.

Thank you, thank you, thank you. Not only can I edit this into what I am looking for but it was easy to read and I can backwards engineer this proper and my garden project can move onto the final stages as everything is logging properly, cron jobs are set up and working, and if the power goes out I don't lose a days worth of data.

I probably spent around 8 hours trying to get this to work and this was the code that saved me.

Seriously thank you.
-Colin

@binh-bk
Copy link
Author

binh-bk commented May 18, 2022

I had an idea for a garden monitoring system with 5 Arduinos and a Raspberry Pi. Like any other learning experience I would hit speed bumps at every new step in the process, but was able to overcome and solve each one. Until I needed to log in 'real time' the serial data onto the Pi. For some reason what I was looking for would be partially online. Each tutorial kind of giving me what I was looking for but the pieces never fit together to solve the overall puzzle.

Then I found your instructables post on doing this and lo' and behold there's a link to your github where this is posted.

Thank you, thank you, thank you. Not only can I edit this into what I am looking for but it was easy to read and I can backwards engineer this proper and my garden project can move onto the final stages as everything is logging properly, cron jobs are set up and working, and if the power goes out I don't lose a days worth of data.

I probably spent around 8 hours trying to get this to work and this was the code that saved me.

Seriously thank you. -Colin

Thank you for the nice words, Collin,
I learned from others a lot and if my learning experience is helpful to anyone else, that is already made my day.
-Binh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment