forked from switchdoclabs/SDL_Pi_SkyWeather2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DustSensor.py
147 lines (108 loc) · 3.5 KB
/
DustSensor.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
#!/usr/bin/env python3
#SwithchDoc Labs September 2018
# Public Domain
from __future__ import print_function
from builtins import str
import sys
sys.path.append('./SDL_Pi_HM3301')
import time
import pigpio
import SDL_Pi_HM3301
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
import config
#print("config.DustSensorSCL=", config.DustSensorSCL)
#print("config.DustSensorSDA=", config.DustSensorSDA)
import state
GPIO.setup(config.DustSensorPowerPin, GPIO.OUT)
GPIO.output(config.DustSensorPowerPin, True)
def powerOnDustSensor():
GPIO.setup(config.DustSensorPowerPin, GPIO.OUT)
GPIO.output(config.DustSensorPowerPin, False)
GPIO.output(config.DustSensorPowerPin, True)
time.sleep(1)
def powerOffDustSensor():
GPIO.setup(config.DustSensorPowerPin, GPIO.OUT)
GPIO.output(config.DustSensorPowerPin, True)
GPIO.output(config.DustSensorPowerPin, False)
time.sleep(1)
myPi = pigpio.pi()
try:
hm3301 = SDL_Pi_HM3301.SDL_Pi_HM3301(SDA= config.DustSensorSDA, SCL = config.DustSensorSCL, pi=myPi)
except:
myPi.bb_i2c_close(config.DustSensorSDA)
myPi.stop()
hm3301 = SDL_Pi_HM3301.SDL_Pi_HM3301(SDA= config.DustSensorSDA, SCL = config.DustSensorSCL, pi=myPi)
def read_AQI():
if (config.SWDEBUG):
print ("###############")
print ("Reading AQI")
print ("###############")
if (config.SWDEBUG):
print ("Turning Dust Power On")
powerOnDustSensor()
# delay for 30 seconds for calibrated reading
time.sleep(30)
time.sleep(0.1)
try:
myData = hm3301.get_data()
except Exception as e:
print('=================================')
print(datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'))
print(e)
print('=================================')
return 0
if (config.SWDEBUG):
print ("data=",myData)
if (hm3301.checksum() != True):
if (config.SWDEBUG):
print("Checksum Error!")
myData = hm3301.get_data()
if (hm3301.checksum() != True):
if (config.SWDEBUG):
print("2 Checksum Errors!")
return 0
myAQI = hm3301.get_aqi()
if (config.SWDEBUG):
hm3301.print_data()
print ("AQI=", myAQI)
#hm3301.close()
powerOffDustSensor()
state.AQI = myAQI
def old_read_AQI():
if (config.SWDEBUG):
print ("###############")
print ("Reading AQI")
print ("###############")
if (config.SWDEBUG):
print ("Turning Dust Power On")
powerOnDustSensor()
# delay for 30 seconds for calibrated reading
time.sleep(30)
time.sleep(0.1)
myData = hm3301.get_data()
if (config.SWDEBUG):
print ("data=",myData)
if (hm3301.checksum() != True):
if (config.SWDEBUG):
print("Checksum Error!")
myData = hm3301.get_data()
if (hm3301.checksum() != True):
if (config.SWDEBUG):
print("2 Checksum Errors!")
return 0
myAQI = hm3301.get_aqi()
if (config.SWDEBUG):
hm3301.print_data()
print ("AQI=", myAQI)
#hm3301.close()
powerOffDustSensor()
state.AQI = myAQI
def print_data():
hm3301.print_data()
def get_aqi():
myAQI = hm3301.get_aqi()
return myAQI
def get_data():
myData = hm3301.get_data()
return myData