-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
120 lines (90 loc) · 3.58 KB
/
main.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
from sense_hat import SenseHat
import requests
import datetime
import thread
def getserial():
cpuserial = "0000000000000000"
try:
f = open('/proc/cpuinfo', 'r')
for line in f:
if line[0:6] == 'Serial':
cpuserial = line[10:26]
f.close()
except:
cpuserial = "ERROR000000000"
return cpuserial
ider = {'API-IDENTIFIER': "" + getserial(), 'Content-Type': 'application/json'}
print(getserial())
sense = SenseHat()
sense.clear()
thirty = datetime.timedelta(seconds=3) + datetime.datetime.now()
thirty2 = datetime.timedelta(seconds=0) + datetime.datetime.now()
thirty3 = datetime.timedelta(seconds=0) + datetime.datetime.now()
now = datetime.datetime.now()
summationOfTimeMoving = 0
steps = 0
def sendRequest(url, paramas):
r = requests.post(url, headers={'identifier': getserial()},
data=paramas)
print(r)
while 1:
o = sense.get_orientation()
temp = sense.get_temperature()
humidity = sense.get_humidity()
acceleration = sense.get_accelerometer_raw()
gyrox = sense.get_gyroscope_raw()
x1 = abs(gyrox['x'])
x2 = abs(gyrox['y'])
x3 = abs(gyrox['z'])
x = acceleration['x']
y = acceleration['y']
z = acceleration['z']
if datetime.datetime.now() >= thirty:
# print("request : {0}".format(g))
try:
thread.start_new_thread(sendRequest, (
'http://172.16.0.49:4242/informations', {'temperature': temp, 'humidity': humidity}))
except:
print ("Error: unable to start thread")
thirty = datetime.timedelta(seconds=3) + datetime.datetime.now()
if x > 1.2 or y > 1.2 or z > 1.2:
start = datetime.datetime.now()
try:
thread.start_new_thread(sendRequest, (
'http://172.16.0.49:4242/alert', {'accelx': x, 'accely': y, 'accelz': z}))
except:
print ("Error: unable to start thread")
if x1 >= 0.02 or x2 >= 0.02 or x3 >= 0.02:
steps = steps + 1
print("start moving")
while x1 >= 0.02 or x2 >= 0.02 or x3 >= 0.02:
gyrox = sense.get_gyroscope_raw()
x1 = abs(gyrox['x'])
x2 = abs(gyrox['y'])
x3 = abs(gyrox['z'])
if datetime.datetime.now() >= thirty3:
try:
thread.start_new_thread(sendRequest, (
'http://172.16.0.49:4242/updateGiroscope', {'x': x1, 'y': x2, 'z': x3, 'x1': x, 'x2': y, 'x3': z}))
except:
print ("Error: unable to start thread")
thirty3 = datetime.timedelta(seconds=0.5) + datetime.datetime.now()
end = datetime.datetime.now()
print("end moving")
differernceInTime = abs(end.second - start.second)
summationOfTimeMoving += differernceInTime
# sumOfTime = summationOfTimeMoving + differernceInTime
if differernceInTime >= 1:
try:
thread.start_new_thread(sendRequest, (
'http://172.16.0.49:4242/alertStop',
{'time': differernceInTime, 'sumOfTime': summationOfTimeMoving, 'steps': steps}))
except:
print ("Error: unable to start thread")
if datetime.datetime.now() >= thirty2:
try:
thread.start_new_thread(sendRequest, (
'http://172.16.0.49:4242/updateGiroscope', {'x': x1, 'y': x2, 'z': x3, 'x1': x, 'x2': y, 'x3': z}))
except:
print ("Error: unable to start thread")
thirty2 = datetime.timedelta(seconds=0.5) + datetime.datetime.now()