forked from logic855/house
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhouse_middleware.py
211 lines (178 loc) · 7.27 KB
/
house_middleware.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# These modules are for system operations
# like returning size of variables (size optimizations) or
# scanning the serial receive buffer.
import os
import sys
import serial
import time
import datetime
# Modules hit WUNDERGROUND API
# and introduce real-time weather data
import urllib2
import json
# Log data to cloud with Xively
import xively
# Global variables => I hate using them, but some functionality was easily implemented using them,
# so I gave in.
global xively_success
global csv_success
global wuLastUpdate
global wuData
global iterations
global geolookup
#Sets the initial sucess booleans to False
xively_success = False
csv_success = False
wuLastUpdate = datetime.datetime.strptime("", "")
wuData =[]
# Initial successful iteration count
iterations = 0
def getWunderground(geolookup):
""" Geolookup = weather underground geolookup url: (http://www.wunderground.com/weather/api/d/docs?d=data/geolookup),
determines if program has hit the API within the last 30 minutes. If yes, returns values of last API call.
If no, it tries to call the API and returns a list of the current weather, current temp, rel humidity, and UV index.
If there is no connection to the internet/ wunderground API, function returns a list with all values populated with "no connection".
"""
dataFromWunderground = []
global wuLastUpdate
global wuData
timeDelta = datetime.datetime.now()-wuLastUpdate
print timeDelta
if timeDelta >= datetime.timedelta(minutes=30):
try:
f = urllib2.urlopen(geolookup)
json_string = f.read()
parsed_json = json.loads(json_string)
weather = parsed_json["current_observation"]["weather"]
temp_f = parsed_json["current_observation"]["temp_f"]
relative_humidity = parsed_json["current_observation"]["relative_humidity"]
uv = parsed_json["current_observation"]["UV"]
#pressure_in = parsed_json["current_observation"]["pressure_in"]
#wind_degrees = parsed_json["current_observation"]["wind_degrees"]
#wind_mph = parsed_json["current_observation"]["wind_mph"]
f.close()
dataFromWunderground = [str(weather), str(uv), str(temp_f), str(relative_humidity[:-1]) ]
wuData = dataFromWunderground
wuLastUpdate = datetime.datetime.now()
except:
for i in range(0,4):
dataFromWunderground.append("No connection")
wuData = dataFromWunderground
wuLastUpdate = datetime.datetime.now()
else:
dataFromWunderground = wuData
return dataFromWunderground
def getDateTime():
"""Function grabs current time and date, then returns values in a 2-element list. """
timeNow = time.strftime("%H:%M:%S")
dateToday = time.strftime("%m/%d/%y")
return [dateToday, timeNow]
def status(a, b):
""" a = Xively success status. b = CSV success status."""
if a and b:
return "Xively and CSV updated"
elif a:
return "Xively updated but CSV not updated"
elif b:
return "CSV updated but Xively not updated"
def printXively(dataList):
"""datalist = list of weather underground data, current date and time, and sensor values. Function
iterates over the list, and writes each element to the proper Xively data stream."""
global xively_success
XIVELY_API_KEY = YOUR_API_KEY_HERE
XIVELY_FEED_ID = YOUR_XIVELY_FEED_ID_HERE
api = xively.XivelyAPIClient(XIVELY_API_KEY)
feed = api.feeds.get(XIVELY_FEED_ID)
now = datetime.datetime.now()
feed.datastreams = [
xively.Datastream(id="uv", current_value=dataList[3], at=now),
xively.Datastream(id="weather", current_value=dataList[2], at=now),
xively.Datastream(id="exterior_temp", current_value=dataList[4], at=now),
xively.Datastream(id="exterior_humidity", current_value=dataList[5], at=now),
xively.Datastream(id="temp1", current_value=dataList[6][0], at=now),
xively.Datastream(id="humidity1", current_value=dataList[6][1], at=now),
xively.Datastream(id="temp2", current_value=dataList[7][0], at=now),
xively.Datastream(id="humidity2", current_value=dataList[7][1], at=now),
xively.Datastream(id="temp3", current_value=dataList[8][0], at=now),
xively.Datastream(id="humidity3", current_value=dataList[8][1], at=now),
xively.Datastream(id="temp4", current_value=dataList[9][0], at=now),
xively.Datastream(id="humidity4", current_value=dataList[9][1], at=now),
xively.Datastream(id="temp5", current_value=dataList[10][0], at=now),
xively.Datastream(id="humidity5", current_value=dataList[10][1], at=now),
xively.Datastream(id="temp6", current_value=dataList[11][0], at=now),
xively.Datastream(id="humidity6", current_value=dataList[11][1], at=now),
xively.Datastream(id="temp7", current_value=dataList[12][0], at=now),
xively.Datastream(id="humidity7", current_value=dataList[12][1], at=now),
]
feed.update()
xively_success = True
def writeToCsv(datalist):
""" function writes datalist values to a csv file. If daily csv file exists already,
list values are simply appended to end of file. If it does not, function creates the file,
then appends values.
"""
global csv_success
header = ["date", "time", "weather", "uv", "exterior temp", "exterior humidity", "temp1", "hum2", "temp2", "hum2", "temp3", "hum3", "temp4", "hum4", "temp5", "hum5", "temp6", "hum6", "temp7", "hum7", "iterations","\n"]
fileName = str(time.strftime("%m_%d_%y_")+ "log.csv")
if os.path.exists(fileName):
f = open(fileName, "a")
else:
f = open(fileName, "a+")
for element in header:
f.write(element + ",")
f.write("\n")
for element in datalist:
if type(element)==str:
f.write(element + ",")
if type(element) == list:
for i in element:
f.write(i + ",")
f.write("\n")
f.close()
csv_success = True
def mainLoop():
"""mainLoop checks the serial receive buffer for activity. If it sees anything, it grabs
current date/time and weather underground data. It grabs the semi-colon/colon separated values in the
serial buffer, appends them to the array already containing date/time/wunderground data, and tries to
write array to a CSV and Xively. It then increases the iteration count by 1, and puts the program to sleep
for 4.5 minutes (to save resources on host computer). The arduino sends data every 5 minutes, so a 4.5 minute
sleep cycle is plenty long enough to conserve resouces without mistiming data transfer. """
global xively_success
global csv_success
global iterations
global geolookup
data = []
ser = serial.Serial("/dev/tty.usbmodem1421", 9600)
print "Serial Initialized"
while 1:
if ser.inWaiting():
datetimeData = getDateTime()
weatherData = getWunderground()
for i in datetimeData:
data.append(i)
for i in weatherData:
data.append(i)
val = ser.readline().strip('\n\r').split(';')
for i in range(0, len(val)):
sensorData = val[i].split(':')
data.append(sensorData)
data.append(str(iterations))
try:
writeToCsv(data)
csv_success = True
except:
csv_success = False
try:
printXively(data)
xively_success = True
except:
try:
printXively(data)
xively_success = True
except:
xively_success = False
print status(xively_success, csv_success), ": ", data
iterations += 1
data = []
time.sleep(270)
mainLoop()