-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadTempSQL.py
48 lines (41 loc) · 1.23 KB
/
readTempSQL.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import time
import datetime
import glob
import MySQLdb
from time import strftime
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
temp_sensor = '/sys/bus/w1/devices/28-00000622fd44/w1_slave'
# Variables for MySQL
db = MySQLdb.connect(host="10.64.80.65", user="myuser",passwd="mypass123", db="temp_database")
cur = db.cursor()
def tempRead():
t = open(temp_sensor, 'r')
lines = t.readlines()
t.close()
temp_output = lines[1].find('t=')
if temp_output != -1:
temp_string = lines[1].strip()[temp_output+2:]
temp_c = float(temp_string)/1000.0
return round(temp_c,1)
while True:
temp = tempRead()
print(temp)
datetimeWrite = (time.strftime("%Y-%m-%d ") + time.strftime("%H:%M:%S"))
print(datetimeWrite)
sql = ("""INSERT INTO tempLog (datetime,temperature) VALUES (%s,%s)""",(datetimeWrite,temp))
try:
print("Writing to database...")
# Execute the SQL command
cur.execute(*sql)
# Commit your changes in the database
db.commit()
print("Write Complete")
except:
# Rollback in case there is any error
db.rollback()
print("Failed writing to database")
cur.close()
db.close()
break