-
Notifications
You must be signed in to change notification settings - Fork 1
/
dfrobot-pm25.py
39 lines (34 loc) · 947 Bytes
/
dfrobot-pm25.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
import serial
import time
import input,output
import RPi.GPIO as GPIO
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1500 # set the Timeout to 1500ms, longer than the data transmission periodic time of the sensor
)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(26, GPIO.OUT)
print("Sensor on")
GPIO.output(26, GPIO.HIGH)
# take some readings
buf = []
readings = []
while len(readings) < 4:
buf.append(ser.read())
if buf[0].hex() != "42":
buf = []
if len(buf) > 31:
readings.append(input.parse_buf(buf))
buf = []
time.sleep(1)
print("Sensor off")
GPIO.output(26, GPIO.LOW)
meanReadings = input.meanReadings(readings)
publishedReadings = meanReadings + input.get_bme680_values()
output.mqtt_publish(publishedReadings)
output.luftdaten_publish(publishedReadings)