-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor_database.py
35 lines (31 loc) · 1.17 KB
/
sensor_database.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
#!/usr/bin/python
import serial
import MySQLdb
#establish connection to MySQL. You'll have to change this for your database.
dbConn = MySQLdb.connect("localhost","root","","ping") or die ("could not connect to database")
#open a cursor to the database
cursor = dbConn.cursor()
device = '/dev/ttyACM0' #this will have to be changed to the serial port you are using
try:
print "Trying...",device
arduino = serial.Serial(device, 9600)
print "Connected...."
except:
print "Failed to connect on",device
while True:
try:
data = arduino.readline() #read the data from the arduino
#pieces = data.split("\t") #split the data by the tab
#Here we are going to insert the data into the Database
try:
#cursor.execute("id int NOT NULL AUTO_INCREMENT")
cursor.execute("INSERT INTO distance (distance_cm) VALUES (%s)", data)
print "Data inserted"
dbConn.commit() #commit the insert
#cursor.close() #close the cursor
except MySQLdb.IntegrityError:
print "failed to insert data"
#finally:
#cursor.close() #close just incase it failed
except:
print "Failed to get data from Arduino!"