forked from sharnett/MobileAC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collectTemps.py
57 lines (51 loc) · 1.59 KB
/
collectTemps.py
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
# r - get temp/hum reading
import sqlite3
import re
from time import sleep
from datetime import datetime
def connect():
import serial
# add more locations here
locations= ['/dev/ttyACM0', '/dev/tty.usbmodemfd141', '/dev/tty.usbserial-A700dYwR']
for device in locations:
try:
print "Trying...",device
robot = serial.Serial(device, 9600, timeout=2.00)
print "connected"
break
except:
print "Failed to connect on",device
robot = False
return robot
def command(robot, cmd):
try:
robot.write(cmd)
except:
print "failed to send command"
line = robot.readline().strip()
if line:
m = re.search(r'(\d+\.\d+)\s+(\d+\.\d+)', line)
if m: result = [.1*round(10*float(m.group(1))), float(m.group(2))]
else:
print line
result = 0
else:
print "failed to read line"
result = 0
return result
def insertReading(conn, curs, t, h):
curs.execute('''select count(id) from mobileAC_thermoreading''')
n = curs.fetchone()[0]
dt = datetime.now()
dt = datetime(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)
print "adding %s %2.1f %2.1f to database" % (dt, t, h)
curs.execute('''insert into mobileAC_thermoreading values (?, 1, ?, ?, ?)''', (n+1, dt, t, h))
conn.commit()
arduino = connect()
db = sqlite3.connect('db.db')
c = db.cursor()
while (1):
result = command(arduino, 'r')
if result:
insertReading(db, c, result[0], result[1])
sleep(900)