-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_sensors.py
88 lines (65 loc) · 1.9 KB
/
read_sensors.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
import numpy as np
import pandas as pd
import time
from picamera import PiCamera
import bme280
import spidev
# open connection to SPI bus
spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz=1000000
def read_mcp_all():
#read SPI data from MCP3008 analog to digital converter.
#Returns voltage of each channel and returns list
#assuming a 3.3V input.
all_output = []
#iterate through each channel and read voltage
for ch in range(8):
adc = spi.xfer2([1,(8+ch)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
volts = ((data*3.3)/float(1023))
all_output.append(volts)
return all_output
#set up camera
pic_folder = '/media/pi/KINGSTON/pic_dump/pic_'
cam = PiCamera()
cam.resolution = (2592, 1944)
cam.framerate = 15
#cam.rotation = 270
#number of data points to collect over time
loop_num = 100000
df = pd.DataFrame(
data=np.zeros((loop_num,7)),
columns=['time', 'temp', 'press',
'rh', 'mq2', 'mq3', 'mq5'])
save_df_path = '/media/pi/KINGSTON/sensor_df.csv'
for i in range(loop_num):
print('%i / %i' %(i+1, loop_num))
#read BME280
temp, press, rh = bme280.readBME280All()
temp_f = temp*(9/5) + 32
#read MCP3008 ADC
mcp_output = read_mcp_all()
mq2, mq3, mq5 = mcp_output[0], mcp_output[1], mcp_output[2]
#print results
print(time.ctime())
print('temp (C), (F): '+format(temp)+', '+format(temp_f))
print('pressure (hPa): '+format(press))
print('humidity (%): '+format(rh))
print('MQ-X sensors: '+format(
np.around([mq2, mq3, mq5], decimals=5)))
df.iloc[i] = [time.ctime(), temp, press, rh,
mq2, mq3, mq5]
#every nth point, take a picture
take_pics = True
if i%120==0 and take_pics:
pic_num = str(i).zfill(5)
cam.start_preview()
time.sleep(2)
cam.capture(pic_folder+pic_num+'.jpg')
cam.stop_preview()
time.sleep(12.5)
else:
time.sleep(15)
#remove empty rows before writing to file
df[df['time']!=0].to_csv(save_df_path, index=False)