-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainCode.py
More file actions
233 lines (199 loc) · 6.36 KB
/
Copy pathMainCode.py
File metadata and controls
233 lines (199 loc) · 6.36 KB
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
#!/usr/bin/python3
import targetTemp
import duration
import time
import board
import busio
import adafruit_bmp3xx
import adafruit_dht
import csv
from picamera import PiCamera, Color
from subprocess import call
import datetime as dt
from gpiozero import Button, LED
import os
from pathlib import Path
from time import sleep
from gpiozero import LED
from w1thermsensor import W1ThermSensor
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
from adafruit_ads1x15.ads1x15 import Mode
# Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT22(board.D27)
sensor = W1ThermSensor()
fet = LED(22)
lled = LED(13)
button = Button(26)
start = dt.datetime.now()
lled.on()
time.sleep(1)
lled.off()
time.sleep(1)
lled.on()
time.sleep(1)
lled.off()
print('on')
temp = 50
heat = 0
tempInternal = 74
i2c = busio.I2C(board.SCL, board.SDA)
bmp = adafruit_bmp3xx.BMP3XX_I2C(i2c, address=0x76)
bmp.sea_level_pressure = int(bmp.pressure)
ads = ADS.ADS1115(i2c)
sensorValue = AnalogIn(ads, ADS.P0)
ads.mode = Mode.CONTINUOUS
humidity = 50
altitude = 5
pressure = 4
temp = 7
camera = PiCamera()
lled.on()
time.sleep(1)
lled.off()
def sensor_write(Saltitude, Spressure, Stemp, Shumidity, StempInternal, Sheat, Sco2, SrawCO2, SrawVolts, Sstart):
stime = (dt.datetime.now() - Sstart).seconds
with open ('Readings/alt.csv', 'a') as log:
dataAlt = [Saltitude, stime]
log_writer = csv.writer(log)
log_writer.writerow(dataAlt)
with open ('Readings/press.csv', 'a') as log:
dataPress = [Spressure, stime]
log_writer = csv.writer(log)
log_writer.writerow(dataPress)
with open ('Readings/temp.csv', 'a') as log:
dataTemp = [Stemp, stime]
log_writer = csv.writer(log)
log_writer.writerow(dataTemp)
with open ('Readings/humidity.csv', 'a') as log:
data = [Shumidity, stime]
log_writer = csv.writer(log)
log_writer.writerow(data)
with open ('Readings/tempDS.csv', 'a') as log:
tempData = [StempInternal, stime]
log_writer = csv.writer(log)
log_writer.writerow(tempData)
with open ('Readings/status.csv', 'a') as log:
statusData = [Sheat, stime]
log_writer = csv.writer(log)
log_writer.writerow(statusData)
with open('Readings/co2.csv', "a") as log:
# print('saving co2 value to CSV...')
data = [Sco2, SrawCO2, SrawVolts, stime]
log_writer = csv.writer(log)
log_writer.writerow(data)
# print('CSV start: {}'.format(Sstart))
# print('CSV time: {}'.format(stime))
# print('Saved values to CSV')
def afterButton():
file = open("Readings/alt.csv", "r+")
file.truncate(0)
file.close
file = open("Readings/press.csv", "r+")
file.truncate(0)
file.close
file = open("Readings/temp.csv", "r+")
file.truncate(0)
file.close
with open("Readings/humidity.csv", "r+") as file:
file.truncate(0)
file = open("Readings/status.csv", "r+")
file.truncate(0)
file.close
file = open("Readings/tempDS.csv", "r+")
file.truncate(0)
file.close
file = open("Readings/co2.csv", "r+")
file.truncate(0)
file.close
camera.resolution = (1920, 1080)
camera.framerate = 24
camera.awb_mode = 'sunlight'
camera.start_recording('Videos/dianeistheimpostor.h264', quality = 20, bitrate = 750000)
temp = 50
heat = 0
tempInternal = 74
humidity = 50
start = dt.datetime.now()
while (dt.datetime.now() - start).seconds < duration.time:
print("--------------")
#DHT22
try:
testhumidity = dhtDevice.humidity
if (testhumidity == None):
print("Humidity did not read")
else:
humidity = testhumidity
print(f"Humidity: {humidity}% ")
except RuntimeError as error:
# Errors happen fairly often, DHT's are hard to read, just keep going
print(error.args[0])
continue
except Exception as error:
#dhtDevice.exit()
# raise error
continue
try:
# print('before temp read')
tempInternal = (sensor.get_temperature()*9/5)+32
# print('temp declared')
#continue
except IndexError:
#time.sleep(0.1)
print ("Internal temp - tom servo timed out")
# for x in range(3):
# try:
# tempInternal = int((sensor.get_temperature() * 1.8) + 32)
# except IndexError:
# if x == 3:
# print ("Internal temp - TIME OUT")
# else:
# time.sleep(0.1)
#print('starting readings')
altitude = int(bmp.altitude)
pressure = int(bmp.pressure)
temp = int((bmp.temperature * 1.8) + 32)
rawCO2 = sensorValue.value
rawVolts = sensorValue.voltage
voltage_difference = (sensorValue.voltage*1000) - 400
concentration = voltage_difference * 50.0/16.0
#print('finished readingsssnake')
sensor_write(altitude, pressure, temp, humidity, tempInternal, heat, concentration, rawCO2, rawVolts, start)
#print('finished sensor write')
# camera.annotate_text = str(temp) + ' F' + ' ' + str(altitude) + ' m' + ' ' + str(pressure) + ' hpa'
# camera.annotate_foreground = Color('white')
# camera.annotate_text_size = 40
#print('starting heater control code')
#Heater Control
if(tempInternal < targetTemp.thresh):
fet.on()
heat = 1
else:
fet.off()
heat = 0
#print('finished heater control')
print('Internal temp: {}'.format(tempInternal))
print('Heater: {}'.format(heat))
print('Co2 is {} ppm'.format(concentration))
# print('Start: {}'.format(start))
print('Seconds since start {}'.format((dt.datetime.now() - start).seconds))
print('voltage is {} mv'.format(sensorValue.voltage))
print('raw value: {}'.format(sensorValue.value))
print('raw voltage: {}'.format(sensorValue.voltage))
camera.wait_recording(0.2)
camera.close()
print('Main code is done')
lled.on()
time.sleep(5)
lled.off()
# call('sudo shutdown -h now', shell = True)
quit()
while True:
if(button.is_pressed):
for x in range(3):
lled.on()
time.sleep(0.5)
lled.off()
time.sleep(0.5)
afterButton()
break