sunnuntai 3. kesäkuuta 2012

Final Serial processing stuff

Ok, so the delay in the Arduino sketch is now 10s as opposed to 0.5 seconds
(last line in sketch is now:   delay(10000); // 10 seconds delay between readings)

The python script creates a file name based on the date + hour and records 1024 values from then on.

final script:


#!/usr/bin/python

import datetime
import serial
import time

# important!!!! set FILEPATH , FILE extension andserial port
FILEPATH = 'C:/Users/Sumita/Desktop/Work/iaq/'
FILEXT = '.txt'
PORT = 'COM3'

# read timeout in seconds
# post is COM3 for windows and  /dev/ttyUSB0 for linux

ser = serial.Serial(port=PORT,baudrate=9600,timeout=20)

# loop forever, well atleast till the Arduino is connected
while True:
    maxread = 1024
    filetime = datetime.datetime.now()
    filename =  FILEPATH + str(filetime.month) + '_' + str(filetime.day) + '_'  + str(filetime.hour) + FILEXT
    file = open(filename, 'a')
    while maxread > 0:
    byte = ser.readline()
    if byte != None:
    byte = int(byte)
    else:
    byte= 0
   
    now = time.time()
    json_data = '{"time":"%s", ' % now
    json_data += '"value":%s}\n' % byte
    print json_data
    file.writelines(json_data)
        maxread -= 1
    file.close()


NOTE: the delay (ins ms) in the arduino sketch should be shorter than the timeout (in s) in the python script!!!
there seems to be an almost 30second delay in writing to the file.. not sure why but doesn't seem a cause for concern

to get this to work: you need Python 2.6 or higher (not 3.0), and pySerial: http://pypi.python.org/pypi/pyserial





Ei kommentteja:

Lähetä kommentti