Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cnx = mysql.connector.connect(user='root',password='*****',
- host='localhost',
- database='DB')
- cursor= cnx.cursor()
- def read_temp_raw():
- f = open(device_file, 'r')
- lines = f.readlines()
- f.close()
- return lines
- def read_temp():
- lines = read_temp_raw()
- while lines[0].strip()[-3:] != 'YES':
- time.sleep(0.2)
- lines = read_temp_raw()
- equals_pos = lines[1].find('t=')
- if equals_pos != -1:
- temp_string = lines[1][equals_pos+2:]
- temp_c = float(temp_string) / 1000.0
- return temp_c
- while True:
- temp=read_temp()
- print(temp)
- datetimeWrite = (time.strftime("%Y-%m-%d ") + time.strftime("%H:%M:%S"))
- print (datetimeWrite)
- sql = ("""INSERT INTO `temp-at-interrupt`(`Date`,`Time`,`Temperature`) VALUES ('%s','%s','%s' )""",(datetimeWrite,temp))
- try:
- print ("Writing to database...")
- # Execute the SQL command
- cursor.execute(sql)
- # Commit your changes in the database
- cnx.commit()
- print ("Write Complete")
- except:
- # Rollback in case there is any error
- cursor.close()
- cnx.close()
- print ("Failed writing to database")
Add Comment
Please, Sign In to add comment