Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import serial
- import MySQLdb as MySQLdb
- import time
- #establish connection to MySQL. You'll have to change this for your database.
- dbConn = MySQLdb.connect("localhost","pi3","raspberry","Ecole") or die ("could not connect to database")
- #open a cursor to the database
- cursor = dbConn.cursor()
- strl=""
- device = '/dev/ttyACM1'
- bcl=0
- sql=""
- try:
- print ("Trying...",device )
- arduino = serial.Serial(device, 9600)
- except:
- print ("Failed to connect on",device)
- while True:
- time.sleep(1)
- try:
- while len(strl)<8:
- data=arduino.read()
- strl+=str(data)
- bcl+=1
- print(bcl)
- if bcl==7:
- print("last")
- print(type(strl))
- print(strl)
- bcl=0
- sql="INSERT INTO Etudiant (ID_Etud) VALUES (%s)"%(strl)
- print(sql)
- try:
- print ("db")
- cursor=dbConn.cursor()
- cursor.execute(sql)
- dbConn.commit()
- cursor.close()
- except MySQLdb.IntegrityError:
- print ("failed to insert data")
- finally:
- cursor.close()
- except Exception as e:
- print("Processing",str(e))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement