-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtempx1email.py
More file actions
138 lines (108 loc) · 3.18 KB
/
tempx1email.py
File metadata and controls
138 lines (108 loc) · 3.18 KB
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
import os
import glob
import time
import urllib
import json
import netifaces
import gdata
import gdata.spreadsheet.service as gdss
import RPi.GPIO as GPIO
import smtplib
GREEN_LED = 24
RED_LED = 23
WARN_TEMP_HIGH = 85
WARN_TEMP_LOW = 40
EMAIL_LIST = [
# 'jbrodie@gmail.com',
'scott.brodie@mssm.edu',
'ebrodie27@gmail.com',
# 'trooper1915@gmail.com'
]
USERNAME = 'lakehousepi@gmail.com'
PASSWORD = 'lakehousepi!'
SPREADSHEETKEY = '0AjzyT7vXIZUudEo1TXNLMXRlanNRc1ZyZjFBclZkN0E'
WORKSHEETID = 'od6'
GPIO.setmode(GPIO.BCM)
GPIO.setup(GREEN_LED, GPIO.OUT)
GPIO.setup(RED_LED, GPIO.OUT)
def get_global_ip():
jss = urllib.URLopener().open('http://jsonip.com').read()
obj = json.loads(jss)
ip = obj['ip']
return ip
GPIO.output(GREEN_LED, True)
global_ip = get_global_ip()
local_ip = netifaces.ifaddresses("wlan0").get(netifaces.AF_INET)[0]['addr']
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
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
temp_f = temp_c * 9.0/5.0 + 32.0
return (temp_c, temp_f,)
# while True: This loop commneted out to simply print the temperature once
# print(read_temp())
# time.sleep(1)
temp = read_temp()
print(temp)
if True:
fromaddr = USERNAME
toaddrs = EMAIL_LIST
# if temp[1] > WARN_TEMP_HIGH:
# msg = 'Subject: The temperature is too darn high!\n\nIt is %d degrees in here!' % temp[1]
# else:
# msg = 'Subject: The temperature is too darn low!\n\nIt is %d degrees in here!' % temp[1]
uername = USERNAME
password = PASSWORD
# server = smtplib.SMTP('smtp.gmail.com:587')
# server.starttls()
# server.login(username, password)
# server.sendmail(fromaddr, toaddrs, msg)
# server.quit()
# Prepare the dictionary to write
dict = {}
dict['date'] = time.strftime('%m/%d/%Y')
dict['time'] = time.strftime('%H:%M:%S')
dict['tempcentigrade'] = str(temp[0])
dict['tempfahrenheit'] = str(temp[1])
dict['localip'] = local_ip
dict['globalip'] = global_ip
print dict
jsonstring = json.dumps(dict)
# tempstring =time.strftime("%Y-%m-%d %H:%M:%S", gmtime())+ ' ' + str(temp[0]) +' '+str(temp[1])
newstr = jsonstring
fromaddr = USERNAME
toaddrs = EMAIL_LIST
username = USERNAME
password = PASSWORD
emailsubj = 'Subject: Lakehouse temperature for %s' % dict['date']
emailbody = 'Temperature recorded at %s UTC, temperature is %s' % (dict['time'], dict['tempfahrenheit'],)
emailmsg = emailsubj + '\n\n' + emailbody
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username, password)
server.sendmail(fromaddr, toaddrs, emailmsg)
server.quit()
#entry = spr_client.InsertRow(dict, SPREADSHEETKEY, WORKSHEETID)
#if isinstance(entry, gdata.spreadsheet.SpreadsheetsList):
# print "Insert row succeeded."
#else:
# print "Insert row failed."
# GPIO.output(RED_LED, True)
# time.sleep(5)
# GPIO.output(RED_LED, False)
GPIO.output(GREEN_LED, False)