Difference between revisions of "CC"

From Vague Hope Wiki
Jump to: navigation, search
m (New page: <pre> code </pre>)
 
Line 1: Line 1:
 +
==Python script to log CC data==
 +
 +
===Setup===
 +
Specific dependencies on ubuntu-server 8.04:
 
<pre>
 
<pre>
code
+
$ aptitude install python-mysqldb
 +
</pre>
 +
 
 +
MySQL DB table:
 +
<pre>
 +
CREATE TABLE tbl_cc (date_time DATETIME,power int(6),temp DECIMAL(5,2));
 +
</pre>
 +
 
 +
start with the following command:
 +
<pre>
 +
$ python cc.py < /dev/ttyUSB0 &
 +
</pre>
 +
 
 +
===Python code for cc.py===
 +
 
 +
<pre>
 +
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()
 
</pre>
 
</pre>

Revision as of 04:35, 10 May 2009

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()