forked from Ankita46/GPS_ACCELEROMETER_CAM_PI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
aditi_poorva_multithreaded.py
275 lines (248 loc) · 9.37 KB
/
aditi_poorva_multithreaded.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import sys
import os
from gps import *
import time
import threading
import IMU
import zlib, base64
from firebase import firebase
from picamera import PiCamera
import urllib.request, urllib.parse, urllib.error, http.client
from threading import Thread, Event
import requests
import errno
from socket import error as SocketError
import difflib
import pigpio
import serial
rx=26
try:
import struct
except ImportError:
import ustruct as struct
pi=pigpio.pi()
if not pi.connected:
exit(0)
pi.set_mode(rx,pigpio.INPUT)
pi.bb_serial_read_open(rx,9600,8)
buffer=[]
stop_event = Event()
#when internet is not connected it will retry sending data
def do_actions(dic,dt):
retry_on = (requests.exceptions.Timeout,requests.exceptions.ConnectionError,requests.exceptions.HTTPError)
time_out=3
while True:
try:
firebase.post(dic,dt)
except retry_on:
time.sleep(time_out)
continue
else:
break
#checks it internet connection is available
def internet_on():
exc = (urllib.error.URLError, urllib.error.HTTPError)
try:
urllib.request.urlopen('http://216.58.192.142',timeout=3)
return True
except exc:
return False
except socket.timeout:
return False
except SocketError as e:
if e.errno !=errno.ECONNRESET:
#print(3)
raise
pass
# thread to take accelerometer readings and writing it in a file and firebase
def accel():
while True:
#print(count)
print(1)
local = time.asctime(time.localtime(time.time()))
string = '/home/pi/Documents/LOG/accel.txt'
#acc='/accel/acc%s'%local
fa=open(string,"a+")
#collecting the value of accelerometer every .5 sec So 60 values in 30sec
ACCx=IMU.readACCx()
ACCy=IMU.readACCy()
ACCz=IMU.readACCz()
x=((ACCx*0.244)/100)
y=((ACCy*0.244)/100)
z=((ACCz*0.244)/100)
print(("X = %f\t"%x), end=' ')
print(("Y = %f\t"%y), end=' ')
print(("Z = %f\t"%z))
#ts=time.asctime(time.localtime(time.time()))
str2 = "X = %f\tY = %f\tZ=%f"%(x,y,z)
str1 = str(local)
s=("%s\t\t%s\n"%(str2,str1))
fa.write("%s\t\t%s\n"%(str2,str1))
fa.close()
time.sleep(0.5)
print('Checking Internet')
#writing data to file and sending it on firebase
result=internet_on()
if result:
print('Internet is on')
if os.path.exists("/home/pi/Documents/LOG/dataofaccel.txt"):
print('It exists')
f_noc=open("/home/pi/Documents/LOG/dataofaccel.txt","a+")
f_noc.seek(0,0)
lines=f_noc.readlines()
if lines!='':
for x in lines:
action_thread=Thread(target=do_actions,args=('\AccFile',x,))
action_thread.start()
os.remove("/home/pi/Documents/LOG/dataofaccel.txt")
s=("%s\t\t%s\n"%(str2,str1))
action_thread_4 = Thread(target=do_actions,args=('\AccFile',s,))
action_thread_4.start()
else:
print('Internet is not there')
f_noc=open("/home/pi/Documents/LOG/dataofaccel.txt","a+")
f_noc.write(s)
f_noc.close()
time.sleep(2)
def writeLog(f):
while True:
print(1)
global buffer
flag = 0
localtime = time.asctime(time.localtime(time.time()))
print(('latitude ' , gpsd.fix.latitude))
print(('longitude ' , gpsd.fix.longitude))
print(('Time ' , localtime))
string1 = '/home/pi/Documents/LOG/images/img%s.jpg'%(localtime)
camera=PiCamera()
camera.capture(string1)
camera.close()
time.sleep(0.5)
print(string1)
imag=string1
#encoding jpeg to text and compressing the text
with open(imag,"rb") as imageFile:
image_64= base64.b64encode(zlib.compress(bytes(imag,'utf-8'),9))
avpg=0
while avpg==0:
#time.sleep(1.5)
(count,data1)=pi.bb_serial_read(rx)
buffer += data1
#print(buffer)
#print(1)
while buffer and buffer[0] != 0x42:
buffer.pop(0)
#print(2)
if len(buffer) > 200:
buffer = [] # avoid an overrun if all bad data
continue
#print(3)
if len(buffer)<33:
continue
#print(4)
if buffer[1] != 0x4d:
buffer.pop(0)
continue
#print(5)
frame_len = struct.unpack(">H", bytes(buffer[2:4]))[0]
if frame_len != 28:
buffer = []
continue
#if len(buffer)<33:
# continue
frame = struct.unpack(">HHHHHHHHHHHHHH", bytes(buffer[4:32]))
pm10_standard, pm25_standard, pm100_standard, pm10_env, \
pm25_env, pm100_env, particles_03um, particles_05um, particles_10um, \
particles_25um, particles_50um, particles_100um, skip, checksum = frame
check = sum(buffer[0:30])
if check != checksum:
buffer = []
continue
print("Concentration Units (standard)")
print("---------------------------------------")
print("PM 1.0: %d\tPM2.5: %d\tPM10: %d" %
(pm10_standard, pm25_standard, pm100_standard))
print("Concentration Units (environmental)")
print("---------------------------------------")
print("PM 1.0: %d\tPM2.5: %d\tPM10: %d" % (pm10_env, pm25_env, pm100_env))
print("---------------------------------------")
print("Particles > 0.3um / 0.1L air:", particles_03um)
print("Particles > 0.5um / 0.1L air:", particles_05um)
print("Particles > 1.0um / 0.1L air:", particles_10um)
print("Particles > 2.5um / 0.1L air:", particles_25um)
print("Particles > 5.0um / 0.1L air:", particles_50um)
print("Particles > 10 um / 0.1L air:", particles_100um)
print("---------------------------------------")
gf=open("Pol_data.txt","a+")
ltime=time.asctime(time.localtime(time.time()))
#f.write("Time: %s\tPM 1.0: %d\tPM2.5: %d\tPM10: %d" %str(ltime) %pm10_standard %pm25_standard %pm100_standard)
gf.write("Time:%s " %str(ltime))
gf.write("PM1.0:%d " %pm10_standard)
gf.write("PM2.5:%d " %pm25_standard)
gf.write("PM10:%d " %pm100_standard)
gf.write("\n")
gf.close()
s=("PM 1.0: %d\tPM2.5: %d\tPM10: %d" %(pm10_standard, pm25_standard, pm100_standard))
pol_data=str(ltime)+s
#fb1.post('Pol_data',data)
buffer = buffer[32:]
avpg=1
data="Time:%s\tLat:%f\tLong:%f\tImage:%s\tImgAsTxt:%s" %(localtime,gpsd.fix.latitude,gpsd.fix.longitude,string1,image_64)
f.write("\n%s\n" %data)
result=internet_on()
if result:
print('Internet is there')
if os.path.exists("/home/pi/Documents/LOG/data.txt"):
print('It exists')
f_noc_p=open("/home/pi/Documents/LOG/data.txt","a+")
f_noc_p.seek(0,0)
lines=f_noc_p.readlines()
if lines!='':
for x in lines:
if flag==0:
action_thread_10 = Thread(target=do_actions,args=('\LOG',x,))
action_thread_10.start()
flag=1
else:
action_thread_11=Thread(target=do_actions,args=('Pol_data',x,))
action_thread_11.start()
flag=0
os.remove("/home/pi/Documents/LOG/data.txt")
action_thread_12 = Thread(target=do_actions,args=('\LOG',data,))
action_thread_12.start()
action_thread_13 = Thread(target=do_actions,args=('Pol_data',pol_data,))
action_thread_13.start()
else:
print('Internet is not there:')
f_noc_p=open("/home/pi/Documents/LOG/data.txt","a+")
datafile_1='%s\n%s\n'%(data,pol_data)
f_noc_p.write("%s"%datafile_1)
f_noc_p.close()
time.sleep(2)
#Beginning of the program
IMU.detectIMU()
IMU.initIMU()
gpsd = None
f=open("/home/pi/Documents/LOG/LOG.txt","a+")
#Your database name in firebase
firebase=firebase.FirebaseApplication('https://sensor-with-avpg.firebaseio.com/', None)
os.system('clear')
class GpsPoller(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
global gpsd
gpsd = gps(mode=WATCH_ENABLE)
self.current_value = None
self.running = True
def run(self):
global gpsd
while gpsp.running:
next(gpsd)
if __name__ == '__main__':
gpsp = GpsPoller()
gpsp.start()
count=0
action_thread_1=Thread(target=accel,args=())
action_thread_1.start()
writeLog(f)
f.close()