As i'm intending to be one of yours, I'm still a really new user trying to learn from the magic world of programmation.
(As i'm not fluent in english, i'll do my best )
I did a small program through Arduino IDE to read pulses from a water-meter.
One pulse is send for each liter passed through the wate-meter and IDE arduino is writing a "1" in the IDE consol window (Serial.print("1")).
I would like to import this information from Python to a file .txt for exemple.
I'm just working on it : i'll have to do something looking like that...
Code: Select all
#!/usr/bin/python ''' by Ben Kenney - July 2012 This program reads data coming from the serial port and saves that data to a text file. It expects data in the format: "photocell_reading,thermistor_reading" It assumes that the Arduino shows up in /dev/ttyACM0 on the Raspberry Pi which should happen if you're using Debian. ''' import serial from time import strftime from datetime import datetime, time ser = serial.Serial('/dev/ttyACM0',9600) startTime = datetime.now() try: while 1: line=ser.readline().rstrip() light,temp2=line.split(",") now = datetime.now() elapsedTime = now-startTime elapsedSeconds = (elapsedTime.microseconds+(elapsedTime.days*24*3600+elapsedTime.seconds)*10**6)/10**6 print("%s,%s,%s,%s"%(now.strftime("%Y-%m-%d %H:%M:%S"),elapsedSeconds,light,temp2)) f=open('tempLog.dat','a') print >>f,("%s,%s,%s,%s"%(now.strftime("%Y-%m-%d %H:%M:%S"),elapsedSeconds,light,temp2)) f.close() except KeyboardInterrupt: print "\ndone"
- i'll have to creat a file (as in this exemple) "tempLog.dat" to export datas, but where ? in the ~/pi/src (where is my python script file) or in the /dev/ttyACM0 (which is the place where Arduino send values), or event somewhere else i don't know about ?
- infact, do you advise me the use of pyserial (is it really easyer) ?