root / trunk / code / projects / calibration_platform / server / cal_sta_server.py @ 1944
History | View | Annotate | Download (2.1 KB)
1 | 1926 | jsexton | #!/usr/bin/python
|
---|---|---|---|
2 | 1944 | jsexton | import sys, serial, MySQLdb |
3 | 1926 | jsexton | |
4 | 1941 | jsexton | class Station(serial.Serial): |
5 | def __init__(self, serial_path='/dev/arduino', baudrate=19200): |
||
6 | try:
|
||
7 | serial.Serial.__init__(self, serial_path, baudrate)
|
||
8 | except serial.SerialException as e: |
||
9 | print 'Warning: Unable to open serial communication to station (%s)'%e |
||
10 | sys.exit(0)
|
||
11 | def db_register(self, sensor_name, db_cursor): |
||
12 | t = (sensor_name,) |
||
13 | db_cursor.execute('create table %s (value INTEGER)' % t)
|
||
14 | def db_add(self, sensor_name, db_cursor, db_conn, value): |
||
15 | t = (sensor_name, value) |
||
16 | db_cursor.execute('insert into %s (value) values (%s)' % t)
|
||
17 | db_conn.commit() |
||
18 | 1926 | jsexton | |
19 | 1944 | jsexton | class Robot(serial.Serial): |
20 | def __init__(self, serial_path='/dev/ttyUSB0', baudrate=115200): |
||
21 | try:
|
||
22 | serial.Serial.__init__(self, serial_path, baudrate)
|
||
23 | except serial.SerialException as e: |
||
24 | print 'Warning: Unable to open serial communication to robot (%s)'%e |
||
25 | sys.exit(0)
|
||
26 | def db_register(self, sensor_name, db_cursor): |
||
27 | t = (sensor_name,) |
||
28 | db_cursor.execute('create table %s (value INTEGER)' % t)
|
||
29 | def db_add(self, sensor_name, db_cursor, db_conn, value): |
||
30 | t = (sensor_name, value) |
||
31 | db_cursor.execute('insert into %s (value) values (%s)' % t)
|
||
32 | db_conn.commit() |
||
33 | |||
34 | 1941 | jsexton | # SQLite3 database setup
|
35 | 1944 | jsexton | ST_conn = MySQLdb.connect(host = 'localhost',
|
36 | user = 'colony',
|
||
37 | passwd = 'mrbickles',
|
||
38 | db = 'calibration_station')
|
||
39 | 1941 | jsexton | ST_cursor = ST_conn.cursor() |
40 | |||
41 | 1944 | jsexton | ST_cursor.execute('SELECT * FROM raw_data;')
|
42 | for row in ST_cursor: |
||
43 | print row
|
||
44 | |||
45 | |||
46 | 1941 | jsexton | ST = Station() |
47 | 1944 | jsexton | #ST.db_register('sensor0', ST_cursor)
|
48 | #ST.db_register('sensor1', ST_cursor)
|
||
49 | 1941 | jsexton | |
50 | 1944 | jsexton | RO = Robot('/dev/ttyUSB1')
|
51 | |||
52 | 1926 | jsexton | while True: |
53 | 1944 | jsexton | if RO.inWaiting() > 0: |
54 | try:
|
||
55 | msg = RO.readline()[:-1]
|
||
56 | except OSError as e: |
||
57 | print 'Error: %s' %e |
||
58 | |||
59 | t = msg.split(':')
|
||
60 | print t
|
||
61 | |||
62 | 1941 | jsexton | if ST.inWaiting() > 0: |
63 | 1932 | jsexton | try:
|
64 | 1941 | jsexton | msg = ST.readline()[:-1]
|
65 | 1932 | jsexton | except OSError as e: |
66 | print 'Error: %s' %e |
||
67 | 1926 | jsexton | |
68 | 1941 | jsexton | t = msg.split(':')
|
69 | print t
|
||
70 | |||
71 | 1944 | jsexton | # if (len(t) == 3 and t[1] == '0'):
|
72 | # ST.db_add('sensor0', ST_cursor, ST_conn, t[2])
|
||
73 | # elif (len(t) == 3 and t[1] == '1'):
|
||
74 | # ST.db_add('sensor1', ST_cursor, ST_conn, t[2])
|
||
75 | 1941 | jsexton | |
76 | |||
77 | ST_cursor.close() |
||
78 | ST.close() |