-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathm_temp.py
60 lines (47 loc) · 1.56 KB
/
m_temp.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
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import fnmatch
import time
import MySQLdb as mdb
import logging
logging.basicConfig(filename='/home/pi/DS18B20_error.log',
level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(name)s %(message)s')
logger=logging.getLogger(__name__)
# Load the modules (not required if they are loaded at boot)
# os.system('modprobe w1-gpio')
# os.system('modprobe w1-therm')
# Function for storing readings into MySQL
def insertDB(IDs, temperature):
try:
con = mdb.connect('localhost',
'pi_insert',
'xxxxxxxxxx',
'measurements');
cursor = con.cursor()
for i in range(0,len(temperature)):
sql = "INSERT INTO temperature(temperature, sensor_id) \
VALUES ('%s', '%s')" % \
( temperature[i], IDs[i])
cursor.execute(sql)
sql = []
con.commit()
con.close()
except mdb.Error, e:
logger.error(e)
# Get readings from sensors and store them in MySQL
temperature = []
IDs = []
for filename in os.listdir("/sys/bus/w1/devices"):
if fnmatch.fnmatch(filename, '28-*'):
with open("/sys/bus/w1/devices/" + filename + "/w1_slave") as f_obj:
lines = f_obj.readlines()
if lines[0].find("YES"):
pok = lines[1].find('=')
temperature.append(float(lines[1][pok+1:pok+6])/1000)
IDs.append(filename)
else:
logger.error("Error reading sensor with ID: %s" % (filename))
if (len(temperature)>0):
insertDB(IDs, temperature)