forked from aqicn/sds-sensor-reader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsdsreader.py
43 lines (39 loc) · 1.51 KB
/
sdsreader.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
import arrow, logging, time
from pmuploader import PmDataUploader
from sdserror import SdsError, SdsNoPacketError
from sdssensor import SdsSensor
LOGLEVEL = logging.INFO
PORT = '/dev/serial0'
# Time delta between measurements, in seconds:
SAMPLING_PERIOD = 15
URL = "http://dusty.pythonanywhere.com/pm/save/"
def loop():
while True:
try:
measurement = pmSensor.getMeasurement()
measurement['time'] = arrow.now()
uploader.sendMeasurement(measurement)
time.sleep(pmSensor.samplingPeriod)
except SdsError as e:
logging.error("SDS error: %s %s", type(e), e.args)
if __name__ == "__main__":
logging.basicConfig(format = '%(asctime)s [%(levelname)s] %(message)s',
level = LOGLEVEL)
print("Starting reading SDS PM sensor on port", PORT)
try:
pmSensor = SdsSensor(PORT, SAMPLING_PERIOD)
pmSensor.setId()
print("Sensor ID:", pmSensor.id)
print("Sampling period:", pmSensor.samplingPeriod, "s")
uploader = PmDataUploader(URL)
loop()
except SdsNoPacketError as e:
print(e.message)
print("---> SDS to RasPi connection:", "\n",
" - RED wire <-> pin 4", "\n",
" - BLACK wire <-> pin 6", "\n",
" - YELLOW wire <-> pin 8", "\n",
" - BLUE wire <-> pin 10")
except Exception as e:
logging.error("%s %s", type(e), e.args)
raise