sunnuntai 3. kesäkuuta 2012

Reading the Serial Data


a short python script to read serial value with a timestamp and convert to JSON format:

#!/usr/bin/python

import datetime
import serial
import json

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

ser = serial.Serial(port='COM3',baudrate=9600,timeout=60)

# loop forever, well atleast till the Arduino is connected
while True:
    byte = ser.readline()
    now = datetime.datetime.now()
    json_data = '"datetime":("%s"),' % now
    json_data += '"value":("%s")' % byte
    print json_data

Output:

"datetime":("2012-06-03 22:47:00.149000"),"value":("52")
"datetime":("2012-06-03 22:47:00.613000"),"value":("51")
"datetime":("2012-06-03 22:47:01.070000"),"value":("51")
"datetime":("2012-06-03 22:47:01.558000"),"value":("51")
"datetime":("2012-06-03 22:47:02.019000"),"value":("51")
"datetime":("2012-06-03 22:47:02.494000"),"value":("51")
"datetime":("2012-06-03 22:47:02.984000"),"value":("51")

Now to save it on a file for the Webapp!

Ei kommentteja:

Lähetä kommentti