-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathArduinoMySQL.py
More file actions
26 lines (17 loc) · 897 Bytes
/
ArduinoMySQL.py
File metadata and controls
26 lines (17 loc) · 897 Bytes
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
import serial
import MySQLdb
#Establishing connection
db = MySQLdb.connect("127.0.0.1","root","password","arduinosensor")
#format ("LocalHost","Database-Username","Database-Password","Database-Name")
cursor = db.cursor() #Opening the cursor to Database
port = '/dev/tty.usbmodem1411' #Declare the serial port
print "Connecting...",port #This confirms the declaration
arduino = serial.Serial(port, 9600) #Will read the serial data
print "arduino detected" #Confirms the device
data = arduino.readline() #Read data from the device
#Inserting the data into table
cursor.execute("INSERT INTO tempdata (voltage) VALUES (%s)", (data))
#format ("INSERT INTO <table> (colomn) VALUES(type)",(initialized value))
#Here initialized value is data, which is used to read device.
db.commit() #Commit to insert the data
cursor.close() #Close the cursor