Project

General

Profile

Revision 1941

Modified python script to read from Arduino over serial and write values into a SQLite3 database. Also adjusted the Arduino station code to read from two sensors every five seconds and send the information over serial to the server.

View differences:

trunk/code/projects/calibration_platform/station/cal_sta_station/cal_sta_station.pde
1
#define NUM_INPUTS 2
1 2

  
3
int input_pins[NUM_INPUTS] = {0,1};  // Input Pin Numbers
4
int input_vals[NUM_INPUTS] = {0,0};
5

  
2 6
void setup() {
3 7
  Serial.begin(9600);    // Actual baud rate appears to be 19200
4 8
}
5 9

  
6 10
void loop() {
7
  Serial.print("ST:Robo\n");
8
  delay(1000);
9
  Serial.print("ST:Club\n");
10
  delay(1000);
11
  Serial.print("ST:Colony\n");
12
  delay(1000);
11
  // Collect data
12
  int i;
13
  for (i = 0; i < NUM_INPUTS; i++) {
14
    input_vals[i] = analogRead(input_pins[i]);
15
  }
16
  
17
  // Send data to Station
18
  for (i = 0; i < NUM_INPUTS; i++) {
19
    Serial.print("ST:");
20
    Serial.print(input_pins[i]);
21
    Serial.print(":");
22
    Serial.print(input_vals[i]);
23
    Serial.print("\n");
24
  }
25
  delay(5000);
13 26
}
trunk/code/projects/calibration_platform/server/cal_sta_server.py
1 1
#!/usr/bin/python
2 2
import sys, serial
3
import sqlite3
3 4

  
4
try:
5
	serial0 = serial.Serial(port='/dev/ttyUSB0', baudrate=19200)    # Adruino
6
	serial1 = serial.Serial(port='/dev/ttyUSB1', baudrate=115200)   # Robot
7
except serial.SerialException as e:
8
	print 'Error: %s' % e
9
	sys.exit(0)
5
class Station(serial.Serial):
6
	def __init__(self, serial_path='/dev/arduino', baudrate=19200):
7
		try:
8
			serial.Serial.__init__(self, serial_path, baudrate)
9
		except serial.SerialException as e:
10
			print 'Warning: Unable to open serial communication to station (%s)'%e
11
			sys.exit(0)
12
	def db_register(self, sensor_name, db_cursor):
13
		t = (sensor_name,)
14
		db_cursor.execute('create table %s (value INTEGER)' % t)
15
	def db_add(self, sensor_name, db_cursor, db_conn, value):
16
		t = (sensor_name, value)
17
		db_cursor.execute('insert into %s (value) values (%s)' % t)
18
		db_conn.commit()
10 19

  
20
# SQLite3 database setup
21
db_path = '/home/colony/calibration_station/cal_sta_database.db'
22
ST_conn = sqlite3.connect(db_path)
23
ST_cursor = ST_conn.cursor()
24

  
25
ST = Station()
26
ST.db_register('sensor0', ST_cursor)
27
ST.db_register('sensor1', ST_cursor)
28

  
29
#	RO_serial = serial.Serial(port='/dev/ttyUSB1', baudrate=115200)   # Robot
11 30
while True:
12
	if serial0.inWaiting() > 0:
31
	if ST.inWaiting() > 0:
13 32
		try:
14
			print serial0.read()
33
			msg = ST.readline()[:-1]
15 34
		except OSError as e:
16 35
			print 'Error: %s' %e
17
	if serial1.inWaiting() > 0:
18
		try:
19
			print serial1.read()
20
		except OSError as e:
21
			print 'Error: %s' %e
22 36

  
23
usb.close()
37
		t = msg.split(':')
38
		print t
39

  
40
		if (len(t) == 3 and t[1] == '0'):
41
			ST.db_add('sensor0', ST_cursor, ST_conn, t[2])
42
		elif (len(t) == 3 and t[1] == '1'):
43
			ST.db_add('sensor1', ST_cursor, ST_conn, t[2])
44

  
45

  
46
ST_cursor.close()
47
ST.close()

Also available in: Unified diff