CC

From Vague Hope Wiki
Revision as of 15:46, 14 February 2010 by Haku (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Python script to log CC data

Setup

Specific dependencies on ubuntu-server 8.04:

$ aptitude install python-mysqldb

MySQL DB table:

CREATE TABLE tbl_cc (date_time DATETIME,power int(6),temp DECIMAL(5,2));

start with the following command:

$ python cc.py < /dev/ttyUSB0 &

Python code for cc.py

import sys
import time
import datetime
from xml.dom import minidom
import MySQLdb

class cc:
	def main(self):

		db=MySQLdb.connect(host="localhost", user="usr_username", passwd="passwd",db="db_dbname")
		dbc=db.cursor()

		while 1:
			try:
				data=sys.stdin.readline()
				xdoc=minidom.parseString(data)
				power=xdoc.getElementsByTagName('watts')[0].firstChild.data
				temp=xdoc.getElementsByTagName('tmpr')[0].firstChild.data

				query="INSERT INTO tbl_cc (date_time,power,temp) VALUES ("+ \
					"'"+datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")+"',"+ \
					power+","+temp+");"
				dbc.execute(query)

				#sys.stdout.write("power="+power+" watts" \
				#	" temp="+temp+" C \n")
				#sys.stdout.write(query+"\n")

			except:
				pass

			time.sleep(1)

if __name__ == "__main__":
	a=cc()
	a.main()