Skip to content

Instantly share code, notes, and snippets.

@FLamparski
Created June 5, 2019 10:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FLamparski/6e5e673b165f19d3e9c5f4b88f0d1ab2 to your computer and use it in GitHub Desktop.
Save FLamparski/6e5e673b165f19d3e9c5f4b88f0d1ab2 to your computer and use it in GitHub Desktop.
Simplest SDS-011 Python program
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