-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrpi_thermometer.py
192 lines (180 loc) · 5.27 KB
/
rpi_thermometer.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# RPi Thermometer v1.0
# This program is designed to run on the Raspberry Pi model B+
# Copyright (C) 2014 Tom Herbison MI0IOU
# Email tom@asliceofraspberrypi.co.uk
# Web <http://www.asliceofraspberrypi.co.uk>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
# import modules
import sys
import time
import RPi.GPIO as GPIO
# set up GPIO port
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
# Set GPIO Output Pins
GPIO.setup(5, GPIO.OUT)
GPIO.setup(6, GPIO.OUT)
GPIO.setup(7, GPIO.OUT)
GPIO.setup(8, GPIO.OUT)
GPIO.setup(9, GPIO.OUT)
GPIO.setup(10, GPIO.OUT)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(16, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(19, GPIO.OUT)
GPIO.setup(20, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(26, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
# function to clear the LED display
def cleardisplay():
GPIO.output(5,0)
GPIO.output(6,0)
GPIO.output(7,0)
GPIO.output(8,0)
GPIO.output(9,0)
GPIO.output(10,0)
GPIO.output(11,0)
GPIO.output(12,0)
GPIO.output(13,0)
GPIO.output(16,0)
GPIO.output(17,0)
GPIO.output(18,0)
GPIO.output(19,0)
GPIO.output(20,0)
GPIO.output(21,0)
GPIO.output(22,0)
GPIO.output(23,0)
GPIO.output(24,0)
GPIO.output(25,0)
GPIO.output(26,0)
GPIO.output(27,0)
return
# function to display digits on LED display
def displaydigit(digit,a,b,c,d,e,f,g):
if digit=="0":
GPIO.output(a,1)
GPIO.output(b,1)
GPIO.output(c,1)
GPIO.output(d,1)
GPIO.output(e,1)
GPIO.output(f,1)
if digit=="1":
GPIO.output(b,1)
GPIO.output(c,1)
if digit=="2":
GPIO.output(a,1)
GPIO.output(b,1)
GPIO.output(d,1)
GPIO.output(e,1)
GPIO.output(g,1)
if digit=="3":
GPIO.output(a,1)
GPIO.output(b,1)
GPIO.output(c,1)
GPIO.output(d,1)
GPIO.output(g,1)
if digit=="4":
GPIO.output(b,1)
GPIO.output(c,1)
GPIO.output(f,1)
GPIO.output(g,1)
if digit=="5":
GPIO.output(a,1)
GPIO.output(c,1)
GPIO.output(d,1)
GPIO.output(f,1)
GPIO.output(g,1)
if digit=="6":
GPIO.output(a,1)
GPIO.output(c,1)
GPIO.output(d,1)
GPIO.output(e,1)
GPIO.output(f,1)
GPIO.output(g,1)
if digit=="7":
GPIO.output(a,1)
GPIO.output(b,1)
GPIO.output(c,1)
if digit=="8":
GPIO.output(a,1)
GPIO.output(b,1)
GPIO.output(c,1)
GPIO.output(d,1)
GPIO.output(e,1)
GPIO.output(f,1)
GPIO.output(g,1)
if digit=="9":
GPIO.output(a,1)
GPIO.output(b,1)
GPIO.output(c,1)
GPIO.output(d,1)
GPIO.output(f,1)
GPIO.output(g,1)
if digit=="-":
GPIO.output(g,1)
if digit=="E":
GPIO.output(a,1)
GPIO.output(d,1)
GPIO.output(e,1)
GPIO.output(f,1)
GPIO.output(g,1)
return
# main program starts here
try:
print("Press Ctrl+C to exit")
while (True):
# take a temperature measurement and format the raw data
tfile=open("/sys/bus/w1/devices/"+str(sys.argv[1])+"/w1_slave")
text=tfile.read()
tfile.close()
secondline=text.split("\n")[1]
temperaturedata=(secondline.split(" ")[9])[2:]
if temperaturedata[0]=="-":
if len(temperaturedata)==2:
temperaturedata="-0"+temperaturedata[1]
if len(temperaturedata)==3:
temperaturedata="-0"+temperaturedata[1:]
if len(temperaturedata)==4:
temperaturedata="-0"+temperaturedata[1:]
if len(temperaturedata)>5:
temperaturedata="EEE"
else:
if len(temperaturedata)==1:
temperaturedata="0"+temperaturedata
if len(temperaturedata)==2:
temperaturedata="0"+temperaturedata
if len(temperaturedata)==3:
temperaturedata="0"+temperaturedata
if len(temperaturedata)==4:
temperaturedata="0"+temperaturedata
if len(temperaturedata)>5:
temperaturedata="EEE"
#clear the display
cleardisplay()
# display the temperature on the LED display
displaydigit(temperaturedata[0],19,13,12,16,20,26,21)
displaydigit(temperaturedata[1],11,9,25,8,7,5,6)
displaydigit(temperaturedata[2],27,17,18,23,24,22,10)
# pause for 1 second
time.sleep(1)
except KeyboardInterrupt:
print("\nExiting now...")
cleardisplay()
GPIO.cleanup()