Mercurial > code > home > repos > homeauto
comparison service/air_particles/air_particles.py @ 1177:025ad7fef554
pms5003 -> ttyUSB -> influxdb
Ignore-this: d0c8851b557a88361a1755b93af4fc58
darcs-hash:e94133a9ca5c05d2fd73c3da3c94818f0b8be5b2
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Wed, 21 Nov 2018 22:44:08 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1176:b3041caa2dc7 | 1177:025ad7fef554 |
---|---|
1 from __future__ import print_function | |
2 import sys | |
3 import logging | |
4 import serial | |
5 import time | |
6 from influxdb import InfluxDBClient | |
7 | |
8 logging.basicConfig(level=logging.INFO) | |
9 log = logging.getLogger() | |
10 | |
11 location, = sys.argv[1:] | |
12 min_period = 5 | |
13 | |
14 ser_port = "/dev/ttyUSB0" | |
15 ser = serial.Serial(ser_port, baudrate=9600, stopbits=1, parity="N", timeout=2) | |
16 | |
17 influx = InfluxDBClient('bang6', 9060, 'root', 'root', 'main') | |
18 | |
19 last_write = 0 | |
20 while True: | |
21 ser.flushInput() | |
22 s = map(ord, ser.read(32)) | |
23 if s[:2] != [0x42, 0x4d]: | |
24 log.warn('unknown packet header: %s' % s) | |
25 continue | |
26 | |
27 cs = (s[30] * 256 + s[31]) # check sum | |
28 check = 0 | |
29 for i in range(30): | |
30 check += s[i] | |
31 if check != cs: | |
32 log.warn('checksum mismatch: %s' % s) | |
33 continue | |
34 | |
35 sample = { | |
36 # PM1, PM2.5 and PM10 values for standard particle in ug/m^3 | |
37 'pm1_0_std': s[4] * 256 + s[5], | |
38 'pm2_5_std': s[6] * 256 + s[7], | |
39 'pm10_0_std': s[8] * 256 + s[9], | |
40 | |
41 # PM1, PM2.5 and PM10 values for atmospheric conditions in ug/m^3 | |
42 'pm1_0_atm': s[10] * 256 + s[11], | |
43 'pm2_5_atm': s[12] * 256 + s[13], | |
44 'pm10_0_atm': s[14] * 256 + s[15], | |
45 | |
46 # Number of particles bigger than 0.3 um, 0.5 um, etc. in #/cm^3 | |
47 'part_0_3': s[16] * 256 + s[17], | |
48 'part_0_5': s[18] * 256 + s[19], | |
49 'part_1_0': s[20] * 256 + s[21], | |
50 'part_2_5': s[22] * 256 + s[23], | |
51 'part_5_0': s[24] * 256 + s[25], | |
52 'part_10_0': s[26] * 256 + s[27], | |
53 } | |
54 | |
55 now = int(time.time()) | |
56 if now < last_write + min_period: | |
57 continue | |
58 if last_write == 0: | |
59 log.info('sending first sample: %s', sample) | |
60 last_write = now | |
61 influx.write_points([{'measurement': 'air_particles', | |
62 "fields": sample, | |
63 "time": now, | |
64 }], | |
65 tags=dict(location=location), | |
66 time_precision='s') |