-
Notifications
You must be signed in to change notification settings - Fork 7
/
APP_Harvard_TX.py
202 lines (168 loc) · 7.65 KB
/
APP_Harvard_TX.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
#import mraa
import time
import string
import os
import subprocess
from datetime import datetime
import APP_Harvard_TX_config as Conf
fields = Conf.fields
values = Conf.values
def upload_data():
CSV_items = ['device_id','date','time','s_t0','s_h0','s_d0','s_d1','s_d2','s_gg','s_g8e']
timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
pairs = timestamp.split(" ")
values["device_id"] = Conf.DEVICE_ID
values["ver_app"] = Conf.Version
values["date"] = pairs[0]
values["time"] = pairs[1]
values["tick"] = 0
try:
with open('/proc/uptime', 'r') as f:
values["tick"] = float(f.readline().split()[0])
except:
print "Error: reading /proc/uptime"
msg = ""
for item in values:
if Conf.num_re_pattern.match(str(values[item])):
msg = msg + "|" + item + "=" + str(values[item]) + ""
else:
tq = values[item]
tq = tq.replace('"','')
msg = msg + "|" + item + "=" + tq
restful_str = "wget -O /tmp/last_upload.log \"" + Conf.Restful_URL + "topic=" + Conf.APP_ID + "&device_id=" + Conf.DEVICE_ID + "&key=" + Conf.SecureKey + "&msg=" + msg + "\""
os.system(restful_str)
msg = ""
for item in CSV_items:
if item in values:
msg = msg + str(values[item]) + '\t'
else:
msg = msg + "N/A" + '\t'
try:
with open(Conf.FS_SD + "/" + values["date"] + ".txt", "a") as f:
f.write(msg + "\n")
except:
print "Error: writing to SD"
def display_data(disp):
global connection_flag
#Timer(5, display_data, {disp}).start()
timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
pairs = timestamp.split(" ")
disp.setCursor(0,0)
temp = '{:16}'.format("ID: " + Conf.DEVICE_ID)
disp.write(temp)
disp.setCursor(1,0)
temp = '{:16}'.format("Date: " + pairs[0])
disp.write(temp)
disp.setCursor(2,0)
temp = '{:16}'.format("Time: " + pairs[1])
disp.write(temp)
disp.setCursor(3,0)
temp = (values["s_t0"]*1.8)+32
temp = '{:16}'.format('Temp: %.2fF' % temp)
disp.write(temp)
disp.setCursor(4,0)
temp = '{:16}'.format(' RH: %.2f%%' % values["s_h0"])
disp.write(temp)
disp.setCursor(5,0)
temp = '{:16}'.format('PM2.5: %dug/m3' % values["s_d0"])
disp.write(temp)
disp.setCursor(6,0)
temp = '{:16}'.format('TVOC: %dppb' % values["s_gg"])
disp.write(temp)
disp.setCursor(7,0)
temp = '{:16}'.format(Conf.DEVICE_IP)
disp.write(temp)
disp.setCursor(7,15)
temp = connection_flag
disp.write(temp)
def reboot_system():
process = subprocess.Popen(['uptime'], stdout = subprocess.PIPE)
k = process.communicate()[0]
items = k.split(",")
k = items[-3]
items = k.split(" ")
k = float(items[-1])
if k>1.5:
os.system("echo b > /proc/sysrq-trigger")
def check_connection():
global connection_flag
if(os.system('ping www.google.com -q -c 1 > /dev/null')):
connection_flag = "X"
else:
connection_flag = "@"
if __name__ == '__main__':
if Conf.Sense_PM==1:
pm_data = '1'
pm = Conf.pm_sensor.sensor(Conf.pm_q)
pm.start()
if Conf.Sense_Tmp==1:
tmp_data = '2'
tmp = Conf.tmp_sensor.sensor(Conf.tmp_q)
tmp.start()
tmp_data = {'Tmp':0.0, 'RH':0}
if Conf.Sense_Light==1:
light_data = '3'
light = Conf.light_sensor.sensor(Conf.light_q)
light.start()
if Conf.Sense_Gas==1:
gas_data = '4'
gas = Conf.gas_sensor.sensor(Conf.gas_q)
gas.start()
disp = Conf.upmLCD.SSD1306(0, 0x3C)
disp.clear()
count = 0
values["s_d0"] = 0
values["s_d1"] = 0
values["s_gg"] = 0
values["s_g8e"] = 0
values["s_t0"] = 0
values["s_h0"] = 0
while True:
reboot_system()
check_connection()
if Conf.Sense_PM==1 and not Conf.pm_q.empty():
while not Conf.pm_q.empty():
pm_data = Conf.pm_q.get()
for item in pm_data:
if item in fields:
values[fields[item]] = pm_data[item]
if Conf.float_re_pattern.match(str(values[fields[item]])):
values[fields[item]] = round(float(values[fields[item]]),2)
else:
values[item] = pm_data[item]
if Conf.Sense_Tmp==1 and not Conf.tmp_q.empty():
while not Conf.tmp_q.empty():
tmp_data = Conf.tmp_q.get()
for item in tmp_data:
if item in fields:
values[fields[item]] = tmp_data[item]
if Conf.float_re_pattern.match(str(values[fields[item]])):
values[fields[item]] = round(float(values[fields[item]]),2)
else:
values[item] = tmp_data[item]
if Conf.Sense_Light==1 and not Conf.light_q.empty():
while not Conf.light_q.empty():
light_data = Conf.light_q.get()
for item in light_data:
if item in fields:
values[fields[item]] = light_data[item]
if Conf.float_re_pattern.match(str(values[fields[item]])):
values[fields[item]] = round(float(values[fields[item]]),2)
else:
values[item] = light_data[item]
if Conf.Sense_Gas==1 and not Conf.gas_q.empty():
while not Conf.gas_q.empty():
gas_data = Conf.gas_q.get()
for item in gas_data:
if item in fields:
values[fields[item]] = gas_data[item]
if Conf.float_re_pattern.match(str(values[fields[item]])):
values[fields[item]] = round(float(values[fields[item]]),2)
else:
values[item] = gas_data[item]
display_data(disp)
if count == 6:
upload_data()
count = count + 1
count = count % (Conf.Restful_interval / Conf.Interval_LCD)
time.sleep(Conf.Interval_LCD)