-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathccSerial.py
More file actions
executable file
·95 lines (74 loc) · 2.68 KB
/
ccSerial.py
File metadata and controls
executable file
·95 lines (74 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/env python
import calendar
import os
import serial
import signal
import sqlite3
import sys
import time
import xml.dom.minidom
sPort = serial.Serial(
port='/dev/ttyUSB0',
baudrate=57600,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=10
)
##
## <msg><src>CC128-v1.31</src><dsb>00338</dsb><time>11:04:53</time><tmpr>23.2</tmpr><sensor>0</sensor><id>00077</id><type>1</type><ch1><watts>00132</watts></ch1><ch2><watts>00000</watts></ch2><ch3><watts>00252</watts></ch3></msg>
##
while True:
values = {}
print ""
try:
line = sPort.readline()
line = line[line.index('<'):line.rindex('>') + 1]
dom = xml.dom.minidom.parseString(line)
tmpr = dom.getElementsByTagName("tmpr")
ch1 = dom.getElementsByTagName("ch1")
# ch2 = dom.getElementsByTagName("ch2")
# ch3 = dom.getElementsByTagName("ch3")
values['temperature'] = tmpr[0].childNodes[0].nodeValue
values['power'] = ch1[0].getElementsByTagName("watts")[0].childNodes[0].nodeValue
# values['phase2'] = ch2[0].getElementsByTagName("watts")[0].childNodes[0].nodeValue
# values['phase3'] = ch3[0].getElementsByTagName("watts")[0].childNodes[0].nodeValue
utcTime = time.gmtime()
unixTime = calendar.timegm(utcTime)
isoTime = time.strftime('%Y-%m-%dT%H:%M:%SZ', utcTime)
print "Currently: time=%s, temperature=%.1f, ch1=%d" % ( #, ch2=%d, ch3=%d" % (
isoTime,
float(values['temperature']),
int(values['power']),
# int(values['phase2']),
# int(values['phase3']),
)
except KeyboardInterrupt, e:
sys.stderr.write('Interrupt\n')
signal.signal(signal.SIGINT, signal.SIG_DFL)
os.kill(os.getpid(), signal.SIGINT)
except SystemExit, e:
sys.exit(e)
except Exception, e:
print "Did not parse:\n%s" % line
print "Exception:\n%s" % str(e)
continue
try:
database = sqlite3.connect('/opt/cc2p.data/power.sqlite')
cursor = database.cursor()
for value in values:
cursor.execute('''INSERT INTO %s
VALUES (?, ?, ?)''' % value, (int(unixTime), float(values[value]), int(0),))
database.commit()
cursor.close()
database.close()
print " -- Inserted into database"
except KeyboardInterrupt, e:
sys.stderr.write('Interrupt\n')
signal.signal(signal.SIGINT, signal.SIG_DFL)
os.kill(os.getpid(), signal.SIGINT)
except SystemExit, e:
sys.exit(e)
except Exception, e:
print "Failed to insert into database:\n%s" % str(e)
continue