-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_WSU_sensors
executable file
·103 lines (89 loc) · 2.92 KB
/
run_WSU_sensors
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
#!/usr/bin/python
# Script to access data from WSU Smart Cities sensors.
#
# <https://bitbucket.org/wsular/urbanova-aqnet-proto>
# redirect `print`s to HDMI display
console = open('/dev/tty1', 'w')
import sys
sys.stdout = console
sys.stderr = console
print('\n\nStarting air quality sensors...')
from datetime import datetime
# Create a unique filename
import os
savedir = '/var/log/aqnet'
try:
os.makedirs(savedir)
except OSError:
if not os.path.isdir(savedir):
raise
localFile = savedir+'/aq.'+datetime.now().strftime('%Y%m%d_%H%M%S')+'.csv'
# Write header to the local and remote files.
header = 'Time,Pressure,Temperature\n'
# ....LOCAL
f = open(localFile,'w')
f.write('Time,Pressure,Temperature,PM1,PM2_5,PM10\n')
f.close()
# Initialize the BMP280 temperature/pressure sensor.
sys.path.append('/home/pi/bmp280')
import BMP280 as BMP280
bmp280 = BMP280.BMP280()
print(' BMP280 connected!')
# RH/T sensor
sys.path.append('/home/pi/weather-station')
from HTU21D import HTU21D
htu21d = HTU21D()
print(' HTU21D connected!')
# Initialize the K-30 carbon dioxide sensor.
import serial
import time
ser = serial.Serial('/dev/ttyAMA0')
print(' K-30 Serial Connected!')
ser.flushInput()
time.sleep(1)
# Initialize the OPC-N2 particle monitor.
import spidev
import opc
spi = spidev.SpiDev()
spi.open(0,0)
spi.mode = 1
spi.max_speed_hz = 500000
alpha = opc.OPCN2(spi)
print(' OPC-N2 connected!')
# Turn sensor on.
time.sleep(1)
alpha.on()
import atexit
@atexit.register
def cleanup():
alpha.off()
# Read data from both sensors every 1 second.
while True:
try:
ser.write('\xFE\x44\x00\x08\x02\x9F\x25')
time.sleep(.01)
resp = ser.read(7)
high = ord(resp[3])
low = ord(resp[4])
co2 = (high*256) + low
time.sleep(1)
hist = alpha.histogram()
print('')
print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
print('K30 CO2 {:0.1f} ppm'.format(co2))
print('BMP280 tmpr {:0.2f} degC'.format(bmp280.read_temperature()))
print(' press {:0.1f} kPa'.format(bmp280.read_pressure()/1000.))
print('HTU21D tmpr {:0.2f} degC'.format(htu21d.read_temperature()))
print(' RH {:0.1f} %'.format(htu21d.read_humidity()))
print('OPC-N2 PM1 {:0.2f} ug/m^3'.format(hist['PM1']))
print(' PM2.5 {:0.2f} ug/m^3'.format(hist['PM2.5']))
print(' PM10 {:0.2f} ug/m^3'.format(hist['PM10']))
print(' samp freq {:0.2f} sec'.format(hist['Sampling Period']))
f = open(localFile,'a')
f.write(datetime.now().strftime('%Y-%m-%d %H:%M:%S')+','+str(co2)+','+str(bmp280.read_temperature())+','+str(bmp280.read_pressure())+','+str(hist['PM1'])+','+str(hist['PM2.5'])+','+str(hist['PM10'])+'\n')
f.close()
time.sleep(29)
except KeyboardInterrupt, SystemExit:
raise
except:
print('Exception encountered! Ignoring...')