Advertisement
Guest User

rfid

a guest
Dec 13th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. import serial
  2. import MySQLdb as MySQLdb
  3. import time
  4.  
  5. #establish connection to MySQL. You'll have to change this for your database.
  6. dbConn = MySQLdb.connect("localhost","pi3","raspberry","Ecole") or die ("could not connect to database")
  7. #open a cursor to the database
  8. cursor = dbConn.cursor()
  9. strl=""
  10. device = '/dev/ttyACM1'
  11. bcl=0
  12. sql=""
  13. try:
  14.   print ("Trying...",device )
  15.   arduino = serial.Serial(device, 9600)
  16. except:
  17.   print ("Failed to connect on",device)
  18. while True:
  19.     time.sleep(1)
  20.     try:
  21.         while len(strl)<8:
  22.             data=arduino.read()
  23.             strl+=str(data)
  24.             bcl+=1
  25.             print(bcl)
  26.             if bcl==7:
  27.                 print("last")
  28.                 print(type(strl))
  29.                 print(strl)
  30.                 bcl=0
  31.                 sql="INSERT INTO Etudiant (ID_Etud) VALUES (%s)"%(strl)
  32.                 print(sql)
  33.                 try:
  34.                     print ("db")
  35.                     cursor=dbConn.cursor()
  36.                     cursor.execute(sql)
  37.                     dbConn.commit()
  38.                     cursor.close()
  39.                 except MySQLdb.IntegrityError:
  40.                     print ("failed to insert data")
  41.                 finally:
  42.                     cursor.close()
  43.     except Exception as e:
  44.             print("Processing",str(e))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement