-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathds18b20.py
executable file
·127 lines (105 loc) · 3.47 KB
/
ds18b20.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python
import glob
import time
import re
import RPi.GPIO as GPIO
import subprocess
import MySQLdb as sql
import pushover
from Rules import Rules
# Determine location of first found DS18B20 sensor
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob( base_dir + '28*' )[0]
device_file = device_folder + '/w1_slave'
# Configure GPIO
GPIO.setmode( GPIO.BCM )
GPIO.setwarnings( False )
# Setup Rules
rf = Rules( 'localhost', 'rpi', 'rpi', 'sensordata' )
# Log data to the MySQL database
def logData( id, value):
try:
# Insert new data
cur.execute( "insert into sensor_data(sensor_id,value) values( {0}, {1} )".format( id, value ) )
# Save changes
con.commit()
return
except:
if con:
con.rollback()
return
# Controle state for LED pin (turn on/off the connected LED)
def ledMode( PiPin, mode ):
GPIO.setup( PiPin, GPIO.OUT )
GPIO.output( PiPin, mode )
return
# Read data from the raw device
def read_temp_raw():
f = open( device_file, 'r' )
lines = f.readlines()
f.close()
return lines
# Get data from Raspberry Pi internal sensors
def read_rpi():
# Read CPU temperature
rpiCPU = float( open( '/sys/class/thermal/thermal_zone0/temp' ).read() ) / 1000.0
logData( 4, rpiCPU )
# Read GPU temperature
output = subprocess.check_output(["/opt/vc/bin/vcgencmd", "measure_temp"])
rpiGPU = float( output.split( '=' )[1][:-3] )
logData( 5, rpiGPU )
return
# Determine temperature and humidity from the DHT22/AM2302 sensor
def read_dht22( PiPin ):
output = subprocess.check_output( ["/usr/bin/nice", "-20", "/home/pi/raspberrypi/Adafruit_DHT", "2302", str(PiPin)] )
matches = re.search( "Temp =\s+([0-9.]+)", output )
if ( matches ):
logData( 2, float(matches.group( 1 ) ) )
matches = re.search( "Hum =\s+([0-9.]+)", output )
if ( matches ):
logData( 3, float( matches.group( 1 ) ) )
return
# Determine temperature from the DS18B20 sensor
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
# Turn off all LEDs
ledMode( 14, GPIO.LOW )
ledMode( 15, GPIO.LOW )
ledMode( 18, GPIO.LOW )
while True:
# Connect to MySQL database
con = sql.connect( host = "localhost", user = "rpi", passwd = "rpi", db = "sensordata" )
cur = con.cursor()
# Read internal sensors
read_rpi()
# Read DS18B20 (Temperature)
temp_c = read_temp()
logData( 1, temp_c )
# Update LED based on temperature
ledMode( 14, GPIO.HIGH if temp_c < 27 else GPIO.LOW )
ledMode( 15, GPIO.HIGH if temp_c >= 27 and temp_c < 29 else GPIO.LOW )
ledMode( 18, GPIO.HIGH if temp_c >= 29 else GPIO.LOW )
# Read DHT22 (Temperature, Humidity)
read_dht22( 22 )
# Close MySQL connection
con.close()
# Run rule
rf.run_rule( 1 )
# Send notification
if ( rf.getOutput() is not None ):
pushover.send_notification( rf.getDescription(), \
rf.getOutput(), \
"http://littlegemsoftware.com:314/chart-sensor.html", \
"RPi Sensor Data" )
# Execute shell command (if any)
rf.runShellCmd()
# Wait seconds for next collection
time.sleep( 30 )