Created
June 5, 2019 10:22
-
-
Save FLamparski/6e5e673b165f19d3e9c5f4b88f0d1ab2 to your computer and use it in GitHub Desktop.
Simplest SDS-011 Python program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import serial | |
import struct | |
from datetime import datetime | |
# Change this to the right port - /dev/tty* on Linux and Mac and COM* on Windows | |
PORT = 'COM5' | |
UNPACK_PAT = '<ccHHHcc' | |
with serial.Serial(PORT, 9600, bytesize=8, parity='N', stopbits=1) as ser: | |
while True: | |
data = ser.read(10) | |
unpacked = struct.unpack(UNPACK_PAT, data) | |
ts = datetime.now() | |
pm25 = unpacked[2] / 10.0 | |
pm10 = unpacked[3] / 10.0 | |
print("{}: PM 2.5 = {}, PM 10 = {}".format(ts, pm25, pm10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment