-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall2.py
244 lines (206 loc) · 7.29 KB
/
all2.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
#!/isr/bin/env python3
import os
import traceback
import random, string
import base64
import json
import paho.mqtt.client as mqtt
from time import sleep
from time import gmtime, strftime
import time
import sys
from PIL import Image
import ST7735 as ST7735
import sys
import datetime
import subprocess
import base64
import uuid
import datetime
import traceback
import math
import random, string
import socket
import base64
import json
import cv2
import time
import psutil
import socket
from time import gmtime, strftime
from max30105 import MAX30105, HeartRate
client = mqtt.Client()
client.connect("localhost", 1883, 60)
try:
from smbus2 import SMBus
except ImportError:
from smbus import SMBus
from bmp280 import BMP280
try:
from smbus2 import SMBus
except ImportError:
from smbus import SMBus
from bmp280 import BMP280
bus = SMBus(1)
bmp280 = BMP280(i2c_dev=bus)
# Create ST7735 LCD display class.
disp = ST7735.ST7735(
port=0,
cs=ST7735.BG_SPI_CS_BACK, # BG_SPI_CSB_BACK or BG_SPI_CS_FRONT
dc=9,
backlight=18, # 18 for back BG slot, 19 for front BG slot.
rotation=90,
spi_speed_hz=4000000
)
WIDTH = disp.width
HEIGHT = disp.height
# Initialize display.
disp.begin()
bus = SMBus(1)
bmp280 = BMP280(i2c_dev=bus)
baseline_values = []
baseline_size = 100
for i in range(baseline_size):
pressure = bmp280.get_pressure()
baseline_values.append(pressure)
time.sleep(1)
baseline = sum(baseline_values[:-25]) / len(baseline_values[:-25])
max30105 = MAX30105()
max30105.setup(leds_enable=3)
max30105.set_led_pulse_amplitude(1, 0.0)
max30105.set_led_pulse_amplitude(2, 0.0)
max30105.set_led_pulse_amplitude(3, 12.5)
max30105.set_slot_mode(1, 'red')
max30105.set_slot_mode(2, 'ir')
max30105.set_slot_mode(3, 'green')
max30105.set_slot_mode(4, 'off')
hr = HeartRate(max30105)
# Smooths wobbly data. Increase to increase smoothing.
mean_size = 20
# Compares current smoothed value to smoothed value x
# readings ago. Decrease this to increase detection
# speed.
delta_size = 10
# The delta threshold at which a change is detected.
# Decrease to make the detection more sensitive to
# fluctuations, increase to make detection less
# sensitive to fluctuations.
threshold = 10
data = []
means = []
def do_nothing(obj):
pass
def IP_address():
try:
s = socket.socket(socket_family, socket.SOCK_DGRAM)
s.connect(external_IP_and_port)
answer = s.getsockname()
s.close()
return answer[0] if answer else None
except socket.error:
return None
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
return(res.replace("temp=","").replace("'C\n",""))
# - start timing
starttime = datetime.datetime.now().strftime('%m/%d/%Y %H:%M:%S')
start = time.time()
external_IP_and_port = ('198.41.0.4', 53) # a.root-servers.net
socket_family = socket.AF_INET
# Ip address
ipaddress = IP_address()
# start camera
time.sleep(0.5)
cap = cv2.VideoCapture(0)
time.sleep(3)
# loop forever
#try:
while True:
row = { }
max30105timestamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S-%f")
samples = max30105.get_samples()
if samples is not None:
r = samples[2] & 0xff
d = hr.low_pass_fir(r)
data.append(d)
if len(data) > mean_size:
data.pop(0)
mean = sum(data) / float(len(data))
means.append(mean)
if len(means) > delta_size:
delta = means[-1] - means[-delta_size]
else:
delta = 0
if delta > threshold:
detected = True
else:
detected = False
#print("Value: {:.2f} // Mean: {:.2f} // Delta: {:.2f} // Change detected: {}".format(d, mean, delta, detected))
tempmax30105 = max30105.get_temperature()
row['max30105_temp'] = '{:.2f}'.format(tempmax30105)
row['max30105_value'] = '{:.2f}'.format(d)
row['max30105_mean'] = '{:.2f}'.format(mean)
row['max30105_delta'] = '{:.2f}'.format(delta)
row['max30105_detected'] = '{}'.format(detected)
row['max30105timestamp'] = max30105timestamp
altitude = bmp280.get_altitude(qnh=baseline)
#print('Relative altitude: {:05.2f} metres'.format(altitude))
row['bme280_altitude'] = '{0:05.2f}'.format(altitude)
row['bme280_altitude_feet'] = '{0:05.2f}'.format((altitude / .3048))
bmp280temperature = bmp280.get_temperature()
bmp280pressure = bmp280.get_pressure()
#print('{:05.2f}*C {:05.2f}hPa'.format(bmp280temperature, bmp280pressure))
row['bme280_tempc'] = '{0:05.2f}'.format(bmp280temperature)
row['bme280_tempf'] = '{0:05.2f}'.format((bmp280temperature * 1.8) + 32)
row['bme280_pressure'] = '{0:05.2f}'.format(bmp280pressure)
# ret, frame = cap.read()
uuid2 = '{0}_{1}'.format(strftime("%Y%m%d%H%M%S",gmtime()),uuid.uuid4())
filename = 'images/bog_image_{0}.jpg'.format(uuid2)
filename2 = 'images/bog_image_p_{0}.jpg'.format(uuid2)
# cv2.imwrite(filename, frame)
cpuTemp=int(float(getCPUtemperature()))
end = time.time()
row['imgname'] = filename
row['imgnamep'] = filename2
row['host'] = os.uname()[1]
row['cputemp'] = round(cpuTemp,2)
row['ipaddress'] = ipaddress
row['end'] = '{0}'.format( str(end ))
row['te'] = '{0}'.format(str(end-start))
row['systemtime'] = datetime.datetime.now().strftime('%m/%d/%Y %H:%M:%S')
row['starttime'] = starttime
usage = psutil.disk_usage("/")
row['diskusage'] = "{:.1f}".format(float(usage.free) / 1024 / 1024)
row['memory'] = psutil.virtual_memory().percent
row['uuid'] = str(uuid2)
json_string = json.dumps(row)
json_string += str("\n")
# MQTT
client.connect("192.168.1.251", 1883, 60)
client.publish("garden2", payload=json_string, qos=0, retain=True)
fa=open("/opt/demo/logs/garden2.log", "a+")
fa.write(json_string)
fa.close()
# print("sent:" + json_string)
# image = Image.open(image_file)
# Resize the image image = image.resize((WIDTH, HEIGHT))
# disp.display(image)
json_string = ""
time.sleep(5)
# with canvas(oled) as draw:
# draw.rectangle(oled.bounding_box, outline="white", fill="black")
# draw.text((0, 0), "- Apache NiFi MiniFi -", fill="white")
# draw.text((0, 10), ipaddress, fill="white")
# draw.text((0, 20), starttime, fill="white")
# draw.text((0, 30), 'Temp: {}'.format( sensor.data.temperature ), fill="white")
# draw.text((0, 40), 'Humidity: {}'.format( sensor.data.humidity ), fill="white")
# draw.text((0, 50), 'Pressure: {}'.format( sensor.data.pressure ), fill="white")
# draw.text((0, 60), 'Distance: {}'.format(str(distance_in_mm)), fill="white")
# draw.text((0, 70), 'CPUTemp: {}'.format( cpuTemp ), fill="white")
# draw.text((0, 80), 'TempF: {}'.format( row['bme680_tempf'] ), fill="white")
# draw.text((0, 90), 'A: {}'.format(row['lsm303d_accelerometer']), fill="white")
# draw.text((0, 100), 'M: {}'.format(row['lsm303d_magnetometer']), fill="white")
# draw.text((0, 110), 'DU: {}'.format(row['diskusage']), fill="white")
# time.sleep(0.5)
#except:
# print("Fail to send.")