-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi_temp.py
40 lines (32 loc) · 1.14 KB
/
pi_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
'''
Created on Dec 22, 2015
@author: simonsays
'''
import sys
import Adafruit_DHT
from datetime import datetime
from time import sleep
DHT11_DATA_PIN = 4
FILENAME_FORMAT = 'output_%Y%m%d_%H%M%S.dat'
FILE_HEADER = 'Datetime\tTemperature (C)\tHumidity (%)\n'
LINE_FORMAT = '{0}\t{1:0.1f}\t{2:0.1f}'
LINE_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
INTERRUPT_SECONDS = 60
if __name__ == '__main__':
dt = datetime.now()
filename = dt.strftime(FILENAME_FORMAT)
with open(filename, 'w') as outfile:
outfile.write(FILE_HEADER)
try:
while True:
humidity, temperature = Adafruit_DHT.read_retry(11, DHT11_DATA_PIN)
if humidity is not None and temperature is not None:
line = LINE_FORMAT.format(datetime.now().strftime(LINE_DATE_FORMAT), temperature, humidity)
print line
outfile.write(line +'\n')
else:
print 'Failed to get reading. Try again!'
sleep(INTERRUPT_SECONDS)
except KeyboardInterrupt:
print 'Stopping...'
pass