Guest User

Untitled

a guest
Apr 21st, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. cnx = mysql.connector.connect(user='root',password='*****',
  2. host='localhost',
  3. database='DB')
  4.  
  5.  
  6. cursor= cnx.cursor()
  7.  
  8. def read_temp_raw():
  9. f = open(device_file, 'r')
  10. lines = f.readlines()
  11. f.close()
  12. return lines
  13. def read_temp():
  14. lines = read_temp_raw()
  15. while lines[0].strip()[-3:] != 'YES':
  16. time.sleep(0.2)
  17. lines = read_temp_raw()
  18. equals_pos = lines[1].find('t=')
  19. if equals_pos != -1:
  20. temp_string = lines[1][equals_pos+2:]
  21. temp_c = float(temp_string) / 1000.0
  22. return temp_c
  23.  
  24. while True:
  25. temp=read_temp()
  26. print(temp)
  27. datetimeWrite = (time.strftime("%Y-%m-%d ") + time.strftime("%H:%M:%S"))
  28. print (datetimeWrite)
  29. sql = ("""INSERT INTO `temp-at-interrupt`(`Date`,`Time`,`Temperature`) VALUES ('%s','%s','%s' )""",(datetimeWrite,temp))
  30. try:
  31. print ("Writing to database...")
  32. # Execute the SQL command
  33. cursor.execute(sql)
  34. # Commit your changes in the database
  35. cnx.commit()
  36. print ("Write Complete")
  37. except:
  38. # Rollback in case there is any error
  39. cursor.close()
  40. cnx.close()
  41. print ("Failed writing to database")
Add Comment
Please, Sign In to add comment