python, saving csv file in linux

Hi, I am creating a simple temp logger.

So far I have the arduino nano working with the sensor and outputting the value to the serial monitor on /dev/ttyUSB0

I have found a python script http://tinyurl.com/7avsqxx <here, that seems to work intermittently for me.

Usually, the python script will run for a second, then exit without saving any data into the .csv file.

On one occasion, it saved the data normally. Otherwise it won't work.

I played around with adding 'time.sleep(5)' but I don't know if it helped or not..

This is the python code:

import serial
import time
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
logfile = open('test.csv', 'a')
while 1:   # read a '\n' terminated line 
   line = ser.readline()   # read a '\n' terminated line
   if not line:
      break
   words = line.split()
   now = time.strftime("%d/%m/%Y %H:%M:%S", time.localtime())
   a =  "%s, %s" % (now, line)
   if line.find(',') != -1:
	logfile.write(a)
        logfile.flush()
time.sleep(5)
logfile.close()
ser.close()

This is my arduino code:

/* Sensor test sketch
  for more information see http://www.ladyada.net/make/logshield/lighttemp.html
  */
 
#define aref_voltage 3.3         // we tie 3.3V to ARef and measure it with a multimeter!
 
 
 
 
//TMP36 Pin Variables
int tempPin = 1;        //the analog pin the TMP36's Vout (sense) pin is connected to
                        //the resolution is 10 mV / degree centigrade with a
                        //500 mV offset to allow for negative temperatures
int tempReading;        // the analog reading from the sensor
int led_state;
unsigned long time;

 
void setup(void) {
  // We'll send debugging information via the Serial monitor
  Serial.begin(9600);   
  pinMode(13, OUTPUT);
  Serial.println("Ready");
 
  // If you want to set the aref to something other than 5v
  analogReference(EXTERNAL);
}
 
 
void loop(void) {
 
  tempReading = analogRead(tempPin);  
 
  //Serial.print("Temp reading = ");
  //Serial.print(tempReading);     // the raw analog reading
 
  // converting that reading to voltage, which is based off the reference voltage
  float voltage = tempReading * aref_voltage;
  voltage /= 1024.0; 
 
  // print out the voltage
  //Serial.print(" - ");
  //Serial.print(voltage); Serial.println(" volts");
 
  
  //Serial.print(temperatureC); Serial.println(" degrees C");
 if (millis() - time >5000){   
      time=millis();
  // now print out the temperature
  float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
                                               //to degrees ((volatge - 500mV) times 100)
  // now convert to Fahrenheight
  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
  Serial.print(temperatureF); 
  Serial.print(",");
  digitalWrite(13,led_state);
  led_state=!led_state;
  //Serial.println(" degrees F");
 }
  delay(1000);
}

Your python code has a comment in it that says that it reads up to the carriage return. Your Arduino code never sends one.

unless this line is uncommented : //Serial.println(" degrees F");

Thanks for the comments, I made a few changes and I have it working the way I want now...

python code:

import serial
import time

ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
time.sleep(1)

logfile = open('test.csv', 'a')

while 1:
	line = ser.readline() 
	now = time.strftime("%d/%m/%Y %H:%M:%S", time.localtime())
	a =  "%s, %s, %s" % (now, line, "\n")
	print a	
	logfile.write(a)
        logfile.flush()    
logfile.close()
ser.close()

arduino code:

/* Sensor test sketch
  for more information see http://www.ladyada.net/make/logshield/lighttemp.html
  */
 
#define aref_voltage 3.3         // we tie 3.3V to ARef and measure it with a multimeter!
 
 
 
 
//TMP36 Pin Variables
int tempPin = 1;        //the analog pin the TMP36's Vout (sense) pin is connected to
                        //the resolution is 10 mV / degree centigrade with a
                        //500 mV offset to allow for negative temperatures
int tempReading;        // the analog reading from the sensor
int led_state;
 
void setup(void) {
  // We'll send debugging information via the Serial monitor
  Serial.begin(9600);   
  pinMode(13, OUTPUT);
  Serial.println("Ready");
 
  // If you want to set the aref to something other than 5v
  analogReference(EXTERNAL);
}
 
 
void loop(void) {
 
  tempReading = analogRead(tempPin);  
 
  //Serial.print("Temp reading = ");
  //Serial.print(tempReading);     // the raw analog reading
 
  // converting that reading to voltage, which is based off the reference voltage
  float voltage = tempReading * aref_voltage;
  voltage /= 1024.0; 
 
  // print out the voltage
  //Serial.print(" - ");
  //Serial.print(voltage); Serial.println(" volts");
 
  
  //Serial.print(temperatureC); Serial.println(" degrees C");

  // now print out the temperature
  float temperatureC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
                                               //to degrees ((volatge - 500mV) times 100)
  // now convert to Fahrenheight
  float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
  Serial.print(temperatureF); 
  //Serial.print(",");
  digitalWrite(13,led_state);
  led_state=!led_state;
  //Serial.println(" degrees F");
 
  delay(2000);
}