-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwxJSON.py
232 lines (207 loc) · 7.13 KB
/
wxJSON.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/python
# -*- coding: utf-8 -*-
import serial
import time
import requests
from datetime import datetime,date
import csvinit
import datetime
import sys
'''Initialize highTemp and lowTemp variables with extreme values so they are set to current temp on first program pass'''
global highTemp
highTemp = -800
global lowTemp
lowTemp = 400
def processTime():
global currentHour
global dayOfMonth
currentTime = datetime.datetime.now()
currentMonth = currentTime.timetuple().tm_mon
dayOfMonth=currentTime.timetuple().tm_mday
dayOfYear = currentTime.timetuple().tm_yday
currentHour = currentTime.timetuple().tm_hour
processTime()
hourCheck = currentHour
dayCheck = dayOfMonth
today = datetime.date.today()
tomorrow = today + datetime.timedelta(1)
def processYesterday():
currentTime = datetime.datetime.now()
global prevHour
yesterday = currentTime - datetime.timedelta(1)
yesterhour = currentTime - datetime.timedelta(0,0,0,0,0,1,0)
prevMonth = currentTime.timetuple().tm_mon
if (prevMonth == 1): #Deal with case when current month is January
prevMonth = 12
else:
prevMonth = int(prevMonth) - 1
prevDayOfMonth=yesterday.timetuple().tm_mday
prevDayOfYear = yesterday.timetuple().tm_yday
prevHour = yesterhour.timetuple().tm_hour
processYesterday()
def hourChange(prainHourly):
global rainHourlyFile, currentHour, prevRain, prevHour
global dataArray
'''write, append rainHourly to hourlyrain.txt'''
rainLog = open(rainHourlyFile, 'a')
rainLogEntry = (str(prevHour) + "00" + " " + "{:.2f}".format(prainHourly) + "\n")
rainLog.write(rainLogEntry)
prevRain = dataArray[0]
def dayChange():
global today
global tomorrow
global rainTotal
global lowTemp
global highTemp
''' Append new date stamp to rain hourly file'''
rainLog = open(rainHourlyFile, 'a')
rainLogEntry = (str(tomorrow) + "\n")
rainLog.write(rainLogEntry)
'''Add daily entry to daily rain total file'''
dailylogEntry = (str(today) + " " + "{:.2f}".format(rainTotal) + "\n")
rainDailyLog = open(rainDailyFile, 'a')
rainDailyLog.write(dailylogEntry)
'''Add entry to daily high/low temps file'''
dailyTempsEntry = (str(today) + " " + str(highTemp) + "/" + str(lowTemp) + "\n")
tempsDailyLog = open(highLowDailyFile, 'a')
tempsDailyLog.write(dailyTempsEntry)
'''Reset daily high and low temps'''
lowTemp = float(outTempFahr)
highTemp = float(outTempFahr)
def tempHi(poutTempFahr):
global highTemp
if (poutTempFahr > highTemp):
highTemp = poutTempFahr
def tempLow(poutTempFahr):
global lowTemp
if (poutTempFahr < lowTemp):
lowTemp = poutTempFahr
'''--------Request WeatherBoard Data---------'''
port = "/dev/ttyACM0"
logFile = "current.txt" #enter name (and location if desired) of logfiles
rawFile = "raw.json"
rainHourlyFile = "hourlyrain.txt"
rainDailyFile = "/dailyrain.txt"
highLowDailyFile = "dailytemps.txt"
MBTOINCH = 0.00029530
ELEV = 0.0
ELEVMB = 146.555 #enter elevation correction for barometric pressure (elevation in ft/10)
prevRain = 0.0
'''Inititalize rainHourlyArray'''
rainHourlyArray =[0.0]*24
'''Initialize serial connection'''
ser = serial.Serial(port, 57600, timeout=.2)
ser.flushInput()
ser.flushOutput()
serBuffer = ''
nextChar = ''
'''Get serial data if it's available'''
while True:
if (ser.inWaiting() >= 0):
serBuffer = ser.readline()
bytesToRead = ser.inWaiting()
dataArray = serBuffer.split(",")
length = len(dataArray)
if (length > 10):
header = "-----------------------------------------"
print (header)
print ""
title = "K7BBR Weather Station, Woods Cross, Utah" #customize your name and location here
print (title)
print ""
timestamp = time.asctime(time.gmtime())
currentTime = ("Time: " + (timestamp))
print (currentTime)
processTime()
global outTempFahr
outTempCel = str(dataArray[5])
outTempFahr = float (dataArray[5])
outTempFahr = (outTempFahr * 9 / 5 ) + 32
outTempFahr = ("{:.2f}".format(outTempFahr))
outTemp = ("Outdoor Temperature: " + dataArray[5] + " (" +str(outTempFahr) + " deg F)")
print (outTemp)
humid = ("RH: " +dataArray[4] + "%")
print (humid)
global inTempCel
inTempCel = str(dataArray[6])
inTempFahr = float(dataArray[6])
inTempFahr = (inTempFahr * 9 / 5) +32
inTempStr = ("{:.2f}".format(inTempFahr))
inTemp = ("Indoor Temperature: " + dataArray[6] + " (" +(inTempStr) + " deg F)")
print (inTemp)
baroInch = dataArray[7]
baroInchRaw = str(dataArray[7])
baro = ("Barometric Pressure: " + dataArray[7])
print (baro)
windDir = ("Wind Dir: " + dataArray[3])
windDirRaw = str(dataArray[3])
print (windDir)
windSpeed = ("Wind Speed: " + dataArray[1])
windSpeedRaw=str(dataArray[1])
print (windSpeed)
windGust = ("Wind Gust: " + dataArray[2])
windGustRaw=str(dataArray[2])
print (windGust)
rainHourly = (float(dataArray[0]) - float(prevRain))
rainHourlyArray[currentHour] = rainHourly
rainHourlyRaw= str("{:.2f}".format(rainHourly))
rain = ("Hourly Rain Total: " + ("{:.2f}".format(rainHourly)))
print (rain)
rainTotal = sum(rainHourlyArray)
dayRainRaw = str(rainTotal)
dayrain = ("24 Hour Rain Total " + ("{:.2f}".format(rainTotal)))
print (dayrain)
tempHi(float(outTempFahr))
tempLow(float(outTempFahr)
highLow = ("Today's High/Low Temperatures(in deg F): " + ("{:.2f}".format(highTemp)) + "/" + ("{:.2f}".format(lowTemp)))
highLowRaw = ("High/Low (in deg F): " + ("{:.2f}".format(highTemp)) + "/" + ("{:.2f}".format(lowTemp)))
tempHighRaw = ("{:.2f}".format(highTemp))
tempLowRaw = ("{:.2f}".format(lowTemp))
print (highLow)
print ""
print (header)
log = open(logFile, 'w')
logentry = (header + "\n" + "\n" +
title + "\n" + "\n" +
currentTime + "\n" +
outTemp + "\n" +
humid + "\n" +
inTemp + "\n" +
baro + "\n" +
windDir + "\n" +
windSpeed + "\n" +
windGust + "\n" +
rain + "\n" +
dayrain + "\n" +
highLow + "\n" + "\n" +
header)
log.write(logentry)
'''------------------------------------Create JSON file-----------------------'''
raw = open(rawFile, 'w')
rawentry = ("{" + "\"time\"" + ":" + "\"" + currentTime + "\"" + "," +
"\"dir\"" + ":" + "\"" + windDirRaw + "\"" + "," +
"\"speed\"" + ":" + "\"" + windSpeedRaw + "\"" + "," +
"\"gust\"" + ":" + "\"" +windGustRaw + "\"" + "," +
"\"otempc\"" + ":" + "\"" +outTempCel + "\"" + "," +
"\"otempf\"" + ":" + "\"" + str(outTempFahr) + "\"" + "," +
"\"humid\"" + ":" + "\"" +humid + "\"" + "," +
"\"itempc\"" + ":" + "\"" +inTempCel + "\"" + "," +
"\"itempf\"" + ":" + "\"" +inTempStr + "\"" + "," +
"\"pressure\"" + ":" + "\"" +baroInchRaw + "\"" + "," +
"\"hrain\"" + ":" + "\"" +rainHourlyRaw+ "\"" + "," +
"\"dayrain\"" + ":" + "\"" +dayRainRaw + "\"" + "," +
"\"high\"" + ":" + "\"" +tempHighRaw + "\"" + "," +
"\"low\"" + ":" + "\"" +tempLowRaw + "\"" "}")
raw.write(rawentry)
print(rawentry)
'''-------------------------------------Check Time Passage---------------------'''
#hourCheck = 0 # for testing
if ((hourCheck) != (currentHour)):
processYesterday()
hourChange(rainHourly)
hourCheck = currentHour
#dayCheck = 1 #for testing
if ((dayCheck) != (dayOfMonth)):
dayChange()
dayCheck = dayOfMonth
time.sleep(2)